diff options
author | Anthon van der Neut <anthon@mnt.org> | 2017-04-14 08:48:38 +0200 |
---|---|---|
committer | Anthon van der Neut <anthon@mnt.org> | 2017-04-14 08:48:38 +0200 |
commit | 784b4a6ad8a08b3f5a1ee653b80f13e1c3a30075 (patch) | |
tree | a4e536ce4a3bcf84b1a01c796483cc75e51891f1 /scalarint.py | |
parent | 00fc3a6a889286bd7e2a0c4fa43945ce1e6bf60c (diff) | |
download | ruamel.yaml-784b4a6ad8a08b3f5a1ee653b80f13e1c3a30075.tar.gz |
fix issue 112: hexadecimal not preserved0.14.6
please **close** this issue if fixed
Diffstat (limited to 'scalarint.py')
-rw-r--r-- | scalarint.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/scalarint.py b/scalarint.py new file mode 100644 index 0000000..e2028e3 --- /dev/null +++ b/scalarint.py @@ -0,0 +1,75 @@ +# coding: utf-8 + +from __future__ import print_function, absolute_import, division, unicode_literals + +import sys + +if sys.version_info >= (3, 5, 2): + from typing import Text, Any, Dict, List # NOQA + +__all__ = ["ScalarInt", "BinaryInt", "OctalInt", "HexInt", "HexCapsInt"] + + +class ScalarInt(int): + __slots__ = () + + def __new__(cls, *args, **kw): + # type: (Any, Any) -> Any + return int.__new__(cls, *args, **kw) # type: ignore + + def __iadd__(self, a): # type: ignore + # type: (Any) -> Any + return type(self)(self + a) + + def __ifloordiv__(self, a): # type: ignore + # type: (Any) -> Any + return type(self)(self // a) + + def __imul__(self, a): # type: ignore + # type: (Any) -> Any + return type(self)(self * a) + + def __ipow__(self, a): # type: ignore + # type: (Any) -> Any + return type(self)(self ** a) + + def __isub__(self, a): # type: ignore + # type: (Any) -> Any + return type(self)(self - a) + + +class BinaryInt(ScalarInt): + __slots__ = () + + def __new__(cls, value): + # type: (Text) -> Any + return ScalarInt.__new__(cls, value) + + +class OctalInt(ScalarInt): + __slots__ = () + + def __new__(cls, value): + # type: (Text) -> Any + return ScalarInt.__new__(cls, value) + + +# mixed casing of A-F is not supported, when loading the first non digit +# determines the case + +class HexInt(ScalarInt): + """uses lower case (a-f)""" + __slots__ = () + + def __new__(cls, value): + # type: (Text) -> Any + return ScalarInt.__new__(cls, value) + + +class HexCapsInt(ScalarInt): + """uses upper case (A-F)""" + __slots__ = () + + def __new__(cls, value): + # type: (Text) -> Any + return ScalarInt.__new__(cls, value) |