scalarint.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. # coding: utf-8
  2. from ruamel.yaml.anchor import Anchor
  3. from typing import Text, Any, Dict, List # NOQA
  4. __all__ = ['ScalarInt', 'BinaryInt', 'OctalInt', 'HexInt', 'HexCapsInt', 'DecimalInt']
  5. class ScalarInt(int):
  6. def __new__(cls: Any, *args: Any, **kw: Any) -> Any:
  7. width = kw.pop('width', None)
  8. underscore = kw.pop('underscore', None)
  9. anchor = kw.pop('anchor', None)
  10. v = int.__new__(cls, *args, **kw)
  11. v._width = width
  12. v._underscore = underscore
  13. if anchor is not None:
  14. v.yaml_set_anchor(anchor, always_dump=True)
  15. return v
  16. def __iadd__(self, a: Any) -> Any: # type: ignore
  17. x = type(self)(self + a)
  18. x._width = self._width # type: ignore
  19. x._underscore = ( # type: ignore
  20. self._underscore[:] if self._underscore is not None else None # type: ignore
  21. ) # NOQA
  22. return x
  23. def __ifloordiv__(self, a: Any) -> Any: # type: ignore
  24. x = type(self)(self // a)
  25. x._width = self._width # type: ignore
  26. x._underscore = ( # type: ignore
  27. self._underscore[:] if self._underscore is not None else None # type: ignore
  28. ) # NOQA
  29. return x
  30. def __imul__(self, a: Any) -> Any: # type: ignore
  31. x = type(self)(self * a)
  32. x._width = self._width # type: ignore
  33. x._underscore = ( # type: ignore
  34. self._underscore[:] if self._underscore is not None else None # type: ignore
  35. ) # NOQA
  36. return x
  37. def __ipow__(self, a: Any) -> Any: # type: ignore
  38. x = type(self)(self ** a)
  39. x._width = self._width # type: ignore
  40. x._underscore = ( # type: ignore
  41. self._underscore[:] if self._underscore is not None else None # type: ignore
  42. ) # NOQA
  43. return x
  44. def __isub__(self, a: Any) -> Any: # type: ignore
  45. x = type(self)(self - a)
  46. x._width = self._width # type: ignore
  47. x._underscore = ( # type: ignore
  48. self._underscore[:] if self._underscore is not None else None # type: ignore
  49. ) # NOQA
  50. return x
  51. @property
  52. def anchor(self) -> Any:
  53. if not hasattr(self, Anchor.attrib):
  54. setattr(self, Anchor.attrib, Anchor())
  55. return getattr(self, Anchor.attrib)
  56. def yaml_anchor(self, any: bool = False) -> Any:
  57. if not hasattr(self, Anchor.attrib):
  58. return None
  59. if any or self.anchor.always_dump:
  60. return self.anchor
  61. return None
  62. def yaml_set_anchor(self, value: Any, always_dump: bool = False) -> None:
  63. self.anchor.value = value
  64. self.anchor.always_dump = always_dump
  65. class BinaryInt(ScalarInt):
  66. def __new__(
  67. cls, value: Any, width: Any = None, underscore: Any = None, anchor: Any = None,
  68. ) -> Any:
  69. return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)
  70. class OctalInt(ScalarInt):
  71. def __new__(
  72. cls, value: Any, width: Any = None, underscore: Any = None, anchor: Any = None,
  73. ) -> Any:
  74. return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)
  75. # mixed casing of A-F is not supported, when loading the first non digit
  76. # determines the case
  77. class HexInt(ScalarInt):
  78. """uses lower case (a-f)"""
  79. def __new__(
  80. cls, value: Any, width: Any = None, underscore: Any = None, anchor: Any = None,
  81. ) -> Any:
  82. return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)
  83. class HexCapsInt(ScalarInt):
  84. """uses upper case (A-F)"""
  85. def __new__(
  86. cls, value: Any, width: Any = None, underscore: Any = None, anchor: Any = None,
  87. ) -> Any:
  88. return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)
  89. class DecimalInt(ScalarInt):
  90. """needed if anchor"""
  91. def __new__(
  92. cls, value: Any, width: Any = None, underscore: Any = None, anchor: Any = None,
  93. ) -> Any:
  94. return ScalarInt.__new__(cls, value, width=width, underscore=underscore, anchor=anchor)