Permalink
Browse files

make the output in python3 the same as in python 2

  • Loading branch information...
TaiSakuma committed Nov 25, 2018
1 parent 19262d0 commit cc85231e8da09e3db8d821e9a50913aedfdb1357
Showing with 74 additions and 1 deletion.
  1. +7 −1 alphatwirl/misc/list_to_aligned_text.py
  2. +67 −0 tests/unit/misc/test_list_to_aligned_text.py
@@ -40,7 +40,13 @@ def list_to_aligned_text(src, format_dict = None, left_align_last_column = False
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]
# https://stackoverflow.com/questions/25898733/why-does-strfloat-return-more-digits-in-python-3-than-python-2
# str(0.41999999999999993) is 0.42 in python2 but is 0.41999999999999993 in python3
# use this format to make the output in python 3 as in python 2
default_float_format = '{:.12g}'

transposed = [[str(int(e)) if isinstance(e, float) and e.is_integer() else e for e in r] for r in transposed]
transposed = [[default_float_format.format(e) if isinstance(e, float) 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]
@@ -130,4 +130,71 @@ def test_empty_three():
expected = ""
assert expected == actual

def test_same_flost_to_str_conversion_between_python2_and_3():

src = [
('component', 'htbin', 'njetbin', 'mhtbin', 'alphaT', 'n', 'nvar'),
('data1', 800, 2, 200, 0.41999999999999993, 2, 2),
('data1', 800, 2, 200, 0.43999999999999995, 2, 2),
('data1', 800, 2, 200, 0.45999999999999996, 0, 0),
('data1', 800, 2, 400, 0.41999999999999993, 1, 1),
('data1', 800, 2, 400, 0.43999999999999995, 0, 0),
('data1', 800, 2, 600, 0.41999999999999993, 0, 0),
('data1', 800, 2, 600, 0.54, 1, 1),
('data1', 800, 2, 600, 0.56, 0, 0),
('data1', 800, 3, 200, 0.3999999999999999, 1, 1),
]

actual = list_to_aligned_text(src)

expected = """\
component htbin njetbin mhtbin alphaT n nvar
data1 800 2 200 0.42 2 2
data1 800 2 200 0.44 2 2
data1 800 2 200 0.46 0 0
data1 800 2 400 0.42 1 1
data1 800 2 400 0.44 0 0
data1 800 2 600 0.42 0 0
data1 800 2 600 0.54 1 1
data1 800 2 600 0.56 0 0
data1 800 3 200 0.4 1 1
"""

assert expected == actual

def test_same_flost_to_str_conversion_between_python2_and_3_part2():

src = [
('component', 'met', 'n', 'nvar'),
('data1', 158.48931924611142, 16, 16),
('data1', 199.52623149688807, 33, 33),
('data1', 251.18864315095823, 43, 43),
('data1', 316.22776601683825, 33, 33),
('data1', 398.1071705534977, 25, 25),
('data1', 501.18723362727303, 15, 15),
('data1', 630.9573444801943, 7, 7),
('data1', 794.328234724283, 3, 3),
('data1', 1000.000000000002, 1, 1),
('data1', 1258.92541179417, 1, 1),
('data1', 1584.8931924611175, 0, 0),
]

actual = list_to_aligned_text(src)

expected = """\
component met n nvar
data1 158.489319246 16 16
data1 199.526231497 33 33
data1 251.188643151 43 43
data1 316.227766017 33 33
data1 398.107170553 25 25
data1 501.187233627 15 15
data1 630.95734448 7 7
data1 794.328234724 3 3
data1 1000 1 1
data1 1258.92541179 1 1
data1 1584.89319246 0 0
"""

assert expected == actual
##__________________________________________________________________||

0 comments on commit cc85231

Please sign in to comment.