diff options
author | Anthon van der Neut <anthon@mnt.org> | 2022-03-30 08:07:38 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2022-03-30 08:07:38 +0200 |
commit | ecfac7286a5cca16b366c18cbab7ae8d9e330896 (patch) | |
tree | b259ceea1f96cae8794ed9866e15ad174ed04f06 | |
parent | abf19ad7fa7cba1625cb0e45256b420154caca09 (diff) | |
download | ruamel.yaml-ecfac7286a5cca16b366c18cbab7ae8d9e330896.tar.gz |
hanlde hg meld representer.pyfloat 42 # without decimal dot
-rw-r--r-- | README.rst | 2 | ||||
-rw-r--r-- | _test/test_float.py | 4 | ||||
-rw-r--r-- | constructor.py | 2 | ||||
-rw-r--r-- | representer.py | 11 |
4 files changed, 14 insertions, 5 deletions
@@ -77,6 +77,8 @@ NEXT: Issue reported by Jacob Floyd. - line numbers are now set on `CommentedKeySeq` and `CommentedKeyMap` (which are created if you have a sequence resp. mapping as the key in a mapping) + - fix loading of `!!float 42` (reported by Eric on + `Stack overflow <https://stackoverflow.com/a/71555107/1307905>`_) 0.17.21 (2022-02-12): - fix bug in calling `.compose()` method with `pathlib.Path` instance. diff --git a/_test/test_float.py b/_test/test_float.py index ac1a440..582ccf0 100644 --- a/_test/test_float.py +++ b/_test/test_float.py @@ -22,6 +22,8 @@ class TestFloat: - .5 - +.5 - -.5 + - !!float '42' + - !!float '-42' """) print(data) assert 0.999 < data[0] < 1.001 @@ -36,6 +38,8 @@ class TestFloat: assert .49 < data[9] < .51 assert .49 < data[10] < .51 assert -.51 < data[11] < -.49 + assert 41.99 < data[12] < 42.01 + assert 41.99 < -data[13] < 42.01 def test_round_trip_zeros_0(self) -> None: data = round_trip("""\ diff --git a/constructor.py b/constructor.py index fb15eb6..99d3295 100644 --- a/constructor.py +++ b/constructor.py @@ -1162,7 +1162,7 @@ class RoundTripConstructor(SafeConstructor): anchor=node.anchor, ) width = len(value_so) - prec = value_so.index('.') # you can use index, this would not be float without dot + prec = value_so.find('.') # you can't use index, !!float 42 would be a float without a dot lead0 = leading_zeros(value_so) return ScalarFloat( sign * float(value_s), diff --git a/representer.py b/representer.py index d2ee785..4083016 100644 --- a/representer.py +++ b/representer.py @@ -639,10 +639,13 @@ class RoundTripRepresenter(SafeRepresenter): # no exponent, "normal" dot prec = data._prec ms = data._m_sign if data._m_sign else "" - # -1 for the dot - value = f'{ms}{abs(data):0{data._width - len(ms)}.{data._width - prec - 1}f}' - if prec == 0 or (prec == 1 and ms != ""): - value = value.replace('0.', '.') + if prec < 0: + value = f'{ms}{abs(int(data)):0{data._width - len(ms)}d}' + else: + # -1 for the dot + value = f'{ms}{abs(data):0{data._width - len(ms)}.{data._width - prec - 1}f}' + if prec == 0 or (prec == 1 and ms != ""): + value = value.replace('0.', '.') while len(value) < data._width: value += '0' else: |