summaryrefslogtreecommitdiff
path: root/scalarint.py
blob: e2028e36ee87ea53a36e9a7150b9f72bc3283c50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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)