This repository has been archived by the owner on May 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rename listToAlignedText() list_to_aligned_text()
- Loading branch information
Showing
12 changed files
with
120 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
from .mkdir_p import mkdir_p | ||
from .listToAlignedText import listToAlignedText | ||
from .list_to_aligned_text import list_to_aligned_text | ||
from .quote_string import quote_string | ||
|
||
# to be deleted | ||
from .listToAlignedText import listToAlignedText |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,19 @@ | ||
# Tai Sakuma <tai.sakuma@cern.ch> | ||
|
||
from .quote_string import quote_string | ||
import logging | ||
from .list_to_aligned_text import list_to_aligned_text | ||
|
||
##__________________________________________________________________|| | ||
def listToAlignedText(src, formatDict = None, leftAlignLastColumn = False): | ||
|
||
# e.g., | ||
# src = [ | ||
# ('component', 'v1', 'nvar', 'n'), | ||
# ('data1', 100, 6.0, 40), | ||
# ('data1', 2, 9.0, 3.3), | ||
# ('data1', 3124, 3.0, 0.0000001), | ||
# ('data2', 333, 6.0, 300909234), | ||
# ('data2', 11, 2.0, 323432.2234), | ||
# ] | ||
|
||
if not src: return '' # src = [ ] | ||
if not src[0]: return '' # e.g., src = [(), (), ()] | ||
|
||
transposed = [[r[i] for r in src] for i in range(len(src[0]))] | ||
# e.g., | ||
# transposed = [ | ||
# ['component', 'data1', 'data1', 'data1', 'data2', 'data2'], | ||
# ['v1', 100, 2, 3124, 333, 11], | ||
# ['nvar', 6.0, 9.0, 3.0, 6.0, 2.0], | ||
# ['n', 40, 3.3, 1e-07, 300909234, 323432.2234], | ||
# ] | ||
|
||
if formatDict is not None: | ||
# formatDict = dict(n = '{:10.2f}') | ||
|
||
for column in transposed: | ||
|
||
colum_name = column[0] | ||
if colum_name not in formatDict: | ||
continue | ||
|
||
colum_format = formatDict[colum_name] | ||
column[:] = [column[0]] + [colum_format.format(c) for c in column[1:]] | ||
# e.g., .['n', '40.00', '3.30', '0.00', '300909234.00', '323432.22'] | ||
|
||
transposed = [[int(e) if isinstance(e, float) and e.is_integer() else e for e in r] for r in transposed] | ||
transposed = [[str(e) for e in r] for r in transposed] | ||
|
||
transposed = [[quote_string(e) for e in r] for r in transposed] | ||
|
||
columnWidths = [max([len(e) for e in r]) for r in transposed] | ||
# e.g., columnWidths = [9, 2, 4, 1] | ||
|
||
formatList = ['{:>' + str(e) + 's}' for e in columnWidths] | ||
# e.g., formatList = ['{:>9s}', '{:>4s}', '{:>4s}', '{:>11s}'] | ||
|
||
if leftAlignLastColumn: | ||
formatList[-1] = '{}' | ||
|
||
format = " " + " ".join(formatList) | ||
# e.g., format = "{:>9s} {:>4s} {:>4s} {:>11s}" | ||
|
||
ret = "\n".join([format.format(*row) for row in zip(*transposed)]) + "\n" | ||
# example ret | ||
# component v1 nvar n | ||
# data1 100 6 40 | ||
# data1 2 9 3.3 | ||
# data1 3124 3 1e-07 | ||
# data2 333 6 300909234 | ||
# data2 11 2 323432.2234 | ||
|
||
return ret | ||
logger = logging.getLogger(__name__) | ||
logger.warning('the function "{}" is renamed "{}". The arguments are renamed as well'.format( | ||
"listToAlignedText", "list_to_aligned_text") | ||
) | ||
|
||
return list_to_aligned_text( | ||
src = src, | ||
format_dict = formatDict, | ||
left_align_last_column = leftAlignLastColumn | ||
) | ||
##__________________________________________________________________|| |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
# Tai Sakuma <tai.sakuma@cern.ch> | ||
|
||
from .quote_string import quote_string | ||
|
||
##__________________________________________________________________|| | ||
def list_to_aligned_text(src, format_dict = None, left_align_last_column = False): | ||
|
||
# e.g., | ||
# src = [ | ||
# ('component', 'v1', 'nvar', 'n'), | ||
# ('data1', 100, 6.0, 40), | ||
# ('data1', 2, 9.0, 3.3), | ||
# ('data1', 3124, 3.0, 0.0000001), | ||
# ('data2', 333, 6.0, 300909234), | ||
# ('data2', 11, 2.0, 323432.2234), | ||
# ] | ||
|
||
if not src: return '' # src = [ ] | ||
if not src[0]: return '' # e.g., src = [(), (), ()] | ||
|
||
transposed = [[r[i] for r in src] for i in range(len(src[0]))] | ||
# e.g., | ||
# transposed = [ | ||
# ['component', 'data1', 'data1', 'data1', 'data2', 'data2'], | ||
# ['v1', 100, 2, 3124, 333, 11], | ||
# ['nvar', 6.0, 9.0, 3.0, 6.0, 2.0], | ||
# ['n', 40, 3.3, 1e-07, 300909234, 323432.2234], | ||
# ] | ||
|
||
if format_dict is not None: | ||
# format_dict = dict(n = '{:10.2f}') | ||
|
||
for column in transposed: | ||
|
||
colum_name = column[0] | ||
if colum_name not in format_dict: | ||
continue | ||
|
||
colum_format = format_dict[colum_name] | ||
column[:] = [column[0]] + [colum_format.format(c) for c in column[1:]] | ||
# e.g., .['n', '40.00', '3.30', '0.00', '300909234.00', '323432.22'] | ||
|
||
transposed = [[int(e) if isinstance(e, float) and e.is_integer() else e for e in r] for r in transposed] | ||
transposed = [[str(e) for e in r] for r in transposed] | ||
|
||
transposed = [[quote_string(e) for e in r] for r in transposed] | ||
|
||
columnWidths = [max([len(e) for e in r]) for r in transposed] | ||
# e.g., columnWidths = [9, 2, 4, 1] | ||
|
||
formatList = ['{:>' + str(e) + 's}' for e in columnWidths] | ||
# e.g., formatList = ['{:>9s}', '{:>4s}', '{:>4s}', '{:>11s}'] | ||
|
||
if left_align_last_column: | ||
formatList[-1] = '{}' | ||
|
||
format = " " + " ".join(formatList) | ||
# e.g., format = "{:>9s} {:>4s} {:>4s} {:>11s}" | ||
|
||
ret = "\n".join([format.format(*row) for row in zip(*transposed)]) + "\n" | ||
# example ret | ||
# component v1 nvar n | ||
# data1 100 6 40 | ||
# data1 2 9 3.3 | ||
# data1 3124 3 1e-07 | ||
# data2 333 6 300909234 | ||
# data2 11 2 323432.2234 | ||
|
||
return ret | ||
|
||
##__________________________________________________________________|| |
Oops, something went wrong.