Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 

106 rader
3.6 KiB

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