scalarfloat.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # coding: utf-8
  2. from __future__ import print_function, absolute_import, division, unicode_literals
  3. import sys
  4. from .compat import no_limit_int # NOQA
  5. from ruamel.yaml.anchor import Anchor
  6. if False: # MYPY
  7. from typing import Text, Any, Dict, List # NOQA
  8. __all__ = ['ScalarFloat', 'ExponentialFloat', 'ExponentialCapsFloat']
  9. class ScalarFloat(float):
  10. def __new__(cls, *args, **kw):
  11. # type: (Any, Any, Any) -> Any
  12. width = kw.pop('width', None) # type: ignore
  13. prec = kw.pop('prec', None) # type: ignore
  14. m_sign = kw.pop('m_sign', None) # type: ignore
  15. m_lead0 = kw.pop('m_lead0', 0) # type: ignore
  16. exp = kw.pop('exp', None) # type: ignore
  17. e_width = kw.pop('e_width', None) # type: ignore
  18. e_sign = kw.pop('e_sign', None) # type: ignore
  19. underscore = kw.pop('underscore', None) # type: ignore
  20. anchor = kw.pop('anchor', None) # type: ignore
  21. v = float.__new__(cls, *args, **kw) # type: ignore
  22. v._width = width
  23. v._prec = prec
  24. v._m_sign = m_sign
  25. v._m_lead0 = m_lead0
  26. v._exp = exp
  27. v._e_width = e_width
  28. v._e_sign = e_sign
  29. v._underscore = underscore
  30. if anchor is not None:
  31. v.yaml_set_anchor(anchor, always_dump=True)
  32. return v
  33. def __iadd__(self, a): # type: ignore
  34. # type: (Any) -> Any
  35. return float(self) + a
  36. x = type(self)(self + a)
  37. x._width = self._width
  38. x._underscore = self._underscore[:] if self._underscore is not None else None # NOQA
  39. return x
  40. def __ifloordiv__(self, a): # type: ignore
  41. # type: (Any) -> Any
  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. return x
  47. def __imul__(self, a): # type: ignore
  48. # type: (Any) -> Any
  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. x._prec = self._prec # check for others
  54. return x
  55. def __ipow__(self, a): # type: ignore
  56. # type: (Any) -> Any
  57. return float(self) ** a
  58. x = type(self)(self ** a)
  59. x._width = self._width
  60. x._underscore = self._underscore[:] if self._underscore is not None else None # NOQA
  61. return x
  62. def __isub__(self, a): # type: ignore
  63. # type: (Any) -> Any
  64. return float(self) - a
  65. x = type(self)(self - a)
  66. x._width = self._width
  67. x._underscore = self._underscore[:] if self._underscore is not None else None # NOQA
  68. return x
  69. @property
  70. def anchor(self):
  71. # type: () -> Any
  72. if not hasattr(self, Anchor.attrib):
  73. setattr(self, Anchor.attrib, Anchor())
  74. return getattr(self, Anchor.attrib)
  75. def yaml_anchor(self, any=False):
  76. # type: (bool) -> Any
  77. if not hasattr(self, Anchor.attrib):
  78. return None
  79. if any or self.anchor.always_dump:
  80. return self.anchor
  81. return None
  82. def yaml_set_anchor(self, value, always_dump=False):
  83. # type: (Any, bool) -> None
  84. self.anchor.value = value
  85. self.anchor.always_dump = always_dump
  86. def dump(self, out=sys.stdout):
  87. # type: (Any) -> Any
  88. out.write(
  89. 'ScalarFloat({}| w:{}, p:{}, s:{}, lz:{}, _:{}|{}, w:{}, s:{})\n'.format(
  90. self,
  91. self._width, # type: ignore
  92. self._prec, # type: ignore
  93. self._m_sign, # type: ignore
  94. self._m_lead0, # type: ignore
  95. self._underscore, # type: ignore
  96. self._exp, # type: ignore
  97. self._e_width, # type: ignore
  98. self._e_sign, # type: ignore
  99. )
  100. )
  101. class ExponentialFloat(ScalarFloat):
  102. def __new__(cls, value, width=None, underscore=None):
  103. # type: (Any, Any, Any) -> Any
  104. return ScalarFloat.__new__(cls, value, width=width, underscore=underscore)
  105. class ExponentialCapsFloat(ScalarFloat):
  106. def __new__(cls, value, width=None, underscore=None):
  107. # type: (Any, Any, Any) -> Any
  108. return ScalarFloat.__new__(cls, value, width=width, underscore=underscore)