diff options
author | Anthon van der Neut <anthon@mnt.org> | 2018-12-27 23:37:32 +0100 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2018-12-27 23:37:32 +0100 |
commit | 25b7008eb7721763fe0ea10cc23abeed2c1ef780 (patch) | |
tree | 6ea378f32bd05947a84bcdceedc1d90418aca13f /scalarfloat.py | |
parent | cdb3ca654eb8f6ef734b82bf9130045ce6d85e45 (diff) | |
download | ruamel.yaml-25b7008eb7721763fe0ea10cc23abeed2c1ef780.tar.gz |
roundtrip anchors/aliases on str, int, float
Diffstat (limited to 'scalarfloat.py')
-rw-r--r-- | scalarfloat.py | 27 |
1 files changed, 26 insertions, 1 deletions
diff --git a/scalarfloat.py b/scalarfloat.py index 65a1a28..0404df3 100644 --- a/scalarfloat.py +++ b/scalarfloat.py @@ -4,6 +4,7 @@ from __future__ import print_function, absolute_import, division, unicode_litera import sys from .compat import no_limit_int # NOQA +from ruamel.yaml.anchor import Anchor if False: # MYPY from typing import Text, Any, Dict, List # NOQA @@ -22,6 +23,7 @@ class ScalarFloat(float): e_width = kw.pop('e_width', None) # type: ignore e_sign = kw.pop('e_sign', None) # type: ignore underscore = kw.pop('underscore', None) # type: ignore + anchor = kw.pop('anchor', None) # type: ignore v = float.__new__(cls, *args, **kw) # type: ignore v._width = width v._prec = prec @@ -31,6 +33,8 @@ class ScalarFloat(float): v._e_width = e_width v._e_sign = e_sign v._underscore = underscore + if anchor is not None: + v.yaml_set_anchor(anchor, always_dump=True) return v def __iadd__(self, a): # type: ignore @@ -74,15 +78,36 @@ class ScalarFloat(float): x._underscore = self._underscore[:] if self._underscore is not None else None # NOQA return x + @property + def anchor(self): + # type: () -> Any + if not hasattr(self, Anchor.attrib): + setattr(self, Anchor.attrib, Anchor()) + return getattr(self, Anchor.attrib) + + def yaml_anchor(self, any=False): + # type: (bool) -> Any + if not hasattr(self, Anchor.attrib): + return None + if any or self.anchor.always_dump: + return self.anchor + return None + + def yaml_set_anchor(self, value, always_dump=False): + # type: (Any, bool) -> None + self.anchor.value = value + self.anchor.always_dump = always_dump + def dump(self, out=sys.stdout): # type: (Any) -> Any out.write( - 'ScalarFloat({}| w:{}, p:{}, s:{}, lz:{}|{}, w:{}, s:{})\n'.format( + 'ScalarFloat({}| w:{}, p:{}, s:{}, lz:{}, _:{}|{}, w:{}, s:{})\n'.format( self, self._width, # type: ignore self._prec, # type: ignore self._m_sign, # type: ignore self._m_lead0, # type: ignore + self._underscore, # type: ignore self._exp, # type: ignore self._e_width, # type: ignore self._e_sign, # type: ignore |