scalarfloat.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # coding: utf-8
  2. import sys
  3. from ruamel.yaml.anchor import Anchor
  4. from typing import Text, Any, Dict, List # NOQA
  5. __all__ = ['ScalarFloat', 'ExponentialFloat', 'ExponentialCapsFloat']
  6. class ScalarFloat(float):
  7. def __new__(cls: Any, *args: Any, **kw: Any) -> Any:
  8. width = kw.pop('width', None)
  9. prec = kw.pop('prec', None)
  10. m_sign = kw.pop('m_sign', None)
  11. m_lead0 = kw.pop('m_lead0', 0)
  12. exp = kw.pop('exp', None)
  13. e_width = kw.pop('e_width', None)
  14. e_sign = kw.pop('e_sign', None)
  15. underscore = kw.pop('underscore', None)
  16. anchor = kw.pop('anchor', None)
  17. v = float.__new__(cls, *args, **kw)
  18. v._width = width
  19. v._prec = prec
  20. v._m_sign = m_sign
  21. v._m_lead0 = m_lead0
  22. v._exp = exp
  23. v._e_width = e_width
  24. v._e_sign = e_sign
  25. v._underscore = underscore
  26. if anchor is not None:
  27. v.yaml_set_anchor(anchor, always_dump=True)
  28. return v
  29. def __iadd__(self, a: Any) -> Any: # type: ignore
  30. return float(self) + a
  31. x = type(self)(self + a)
  32. x._width = self._width
  33. x._underscore = self._underscore[:] if self._underscore is not None else None # NOQA
  34. return x
  35. def __ifloordiv__(self, a: Any) -> Any: # type: ignore
  36. return float(self) // a
  37. x = type(self)(self // a)
  38. x._width = self._width
  39. x._underscore = self._underscore[:] if self._underscore is not None else None # NOQA
  40. return x
  41. def __imul__(self, a: Any) -> Any: # type: ignore
  42. return float(self) * a
  43. x = type(self)(self * a)
  44. x._width = self._width
  45. x._underscore = self._underscore[:] if self._underscore is not None else None # NOQA
  46. x._prec = self._prec # check for others
  47. return x
  48. def __ipow__(self, a: Any) -> Any: # type: ignore
  49. return float(self) ** a
  50. x = type(self)(self ** a)
  51. x._width = self._width
  52. x._underscore = self._underscore[:] if self._underscore is not None else None # NOQA
  53. return x
  54. def __isub__(self, a: Any) -> Any: # type: ignore
  55. return float(self) - a
  56. x = type(self)(self - a)
  57. x._width = self._width
  58. x._underscore = self._underscore[:] if self._underscore is not None else None # NOQA
  59. return x
  60. @property
  61. def anchor(self) -> Any:
  62. if not hasattr(self, Anchor.attrib):
  63. setattr(self, Anchor.attrib, Anchor())
  64. return getattr(self, Anchor.attrib)
  65. def yaml_anchor(self, any: bool = False) -> Any:
  66. if not hasattr(self, Anchor.attrib):
  67. return None
  68. if any or self.anchor.always_dump:
  69. return self.anchor
  70. return None
  71. def yaml_set_anchor(self, value: Any, always_dump: bool = False) -> None:
  72. self.anchor.value = value
  73. self.anchor.always_dump = always_dump
  74. def dump(self, out: Any = sys.stdout) -> None:
  75. out.write(
  76. f'ScalarFloat({self}| w:{self._width}, p:{self._prec}, ' # type: ignore
  77. f's:{self._m_sign}, lz:{self._m_lead0}, _:{self._underscore}|{self._exp}'
  78. f', w:{self._e_width}, s:{self._e_sign})\n',
  79. )
  80. class ExponentialFloat(ScalarFloat):
  81. def __new__(cls, value: Any, width: Any = None, underscore: Any = None) -> Any:
  82. return ScalarFloat.__new__(cls, value, width=width, underscore=underscore)
  83. class ExponentialCapsFloat(ScalarFloat):
  84. def __new__(cls, value: Any, width: Any = None, underscore: Any = None) -> Any:
  85. return ScalarFloat.__new__(cls, value, width=width, underscore=underscore)