ruamel.yaml round trip loader messes up floats that start with a '.'
ruamel.yaml is a YAML 1.2 parser/emitter for Python
Brought to you by:
anthon
#!python
ruamel.yaml.round_trip_dump(ruamel.yaml.round_trip_load('.5'))
> "!!float '00'\n"
ruamel.yaml.round_trip_dump(ruamel.yaml.round_trip_load('.55'))
> '0.6\n...\n'
Looks like it's a problem with how the properties are interpreted on output:
#!python
obj = ruamel.yaml.round_trip_load("""
- .5
- 0.5
""")
obj[0].__dict__
> {'_m_lead0': 0, '_underscore': None, '_e_sign': None, '_exp': None, '_width': 2, '_e_width': None, '_prec': 0, '_m_sign': False}
obj[1].__dict__
> {'_m_lead0': 1, '_underscore': None, '_e_sign': None, '_exp': None, '_width': 3, '_e_width': None, '_prec': 1, '_m_sign': False}
obj[0]._width = 3
ruamel.yaml.round_trip_dump(obj)
> - 0.5
> - 0.5
I can't figure out how to make it output '.5', but I don't really care about that. I'd be alright with it adding a leading 0.
My workaround is:
#!python
def represent_float(self, value):
if value._prec == 0:
value._width += 1
value._prec += 1
return ruamel.yaml.representer.RoundTripRepresenter.represent_scalar_float(self, value)
ruamel.yaml.RoundTripRepresenter.add_representer(ruamel.yaml.scalarfloat.ScalarFloat, represent_float)
which adds a leading 0 when necessary.
(originally posted on 2018-08-30 at 17:43:44 by Harrison Gregg <HarrisonGregg@bitbucket>)
None
(originally posted on 2018-08-30 at 18:05:48 by Harrison Gregg <HarrisonGregg@bitbucket>)
Thanks for reporting and the analysis. Of course there was no test for this format...
Python's string method
.format()doesn't support floats that start with a dot which makes things a little more difficult(originally posted on 2018-08-30 at 18:41:49)
add failing test re #229
→ <<cset d9b4821ef1d6="">></cset>
(originally posted on 2018-08-30 at 18:42:10)
fix issue #229 enable round-trip of floats without number before the dot
[ .5, -.5, +.5 ] should work
When this change indeed resolves your problem, please Close this issue.
(You can do so using the WorkFlow pull-down (close to the top right of this page))
→ <<cset 31238c6c8ff7="">></cset>
(originally posted on 2018-08-30 at 19:00:24)
Thanks for the quick turnaround!
(originally posted on 2018-08-31 at 16:23:53 by Harrison Gregg <HarrisonGregg@bitbucket>)
Fixed!
(originally posted on 2018-08-31 at 16:24:07 by Harrison Gregg <HarrisonGregg@bitbucket>)
Issue #257 was marked as a duplicate of this issue.
(originally posted on 2018-11-06 at 18:34:06)