tsrkit-types 0.1.4__py3-none-any.whl → 0.1.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- tsrkit_types/choice.py +4 -1
- tsrkit_types/integers.py +48 -23
- tsrkit_types/null.py +3 -3
- {tsrkit_types-0.1.4.dist-info → tsrkit_types-0.1.6.dist-info}/METADATA +1 -1
- {tsrkit_types-0.1.4.dist-info → tsrkit_types-0.1.6.dist-info}/RECORD +8 -8
- {tsrkit_types-0.1.4.dist-info → tsrkit_types-0.1.6.dist-info}/WHEEL +0 -0
- {tsrkit_types-0.1.4.dist-info → tsrkit_types-0.1.6.dist-info}/licenses/LICENSE +0 -0
- {tsrkit_types-0.1.4.dist-info → tsrkit_types-0.1.6.dist-info}/top_level.txt +0 -0
tsrkit_types/choice.py
CHANGED
|
@@ -68,6 +68,9 @@ class Choice(Codable):
|
|
|
68
68
|
def unwrap(self) -> Any:
|
|
69
69
|
return self._value
|
|
70
70
|
|
|
71
|
+
def get_key(self):
|
|
72
|
+
return self._choice_key
|
|
73
|
+
|
|
71
74
|
def __repr__(self) -> str:
|
|
72
75
|
return f"{self.__class__.__name__}({self._value!r})"
|
|
73
76
|
|
|
@@ -129,4 +132,4 @@ class Choice(Codable):
|
|
|
129
132
|
tag, tag_size = Uint.decode_from(buffer, offset)
|
|
130
133
|
value, val_size = cls._opt_types[tag][1].decode_from(buffer, offset+tag_size)
|
|
131
134
|
|
|
132
|
-
return cls(value), tag_size+val_size
|
|
135
|
+
return cls(value), tag_size+val_size
|
tsrkit_types/integers.py
CHANGED
|
@@ -11,7 +11,7 @@ class IntCheckMeta(abc.ABCMeta):
|
|
|
11
11
|
return isinstance(instance, int) and getattr(instance, "byte_size", 0) == cls.byte_size
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
class
|
|
14
|
+
class Int(int, Codable, metaclass=IntCheckMeta):
|
|
15
15
|
"""
|
|
16
16
|
Unsigned integer type.
|
|
17
17
|
|
|
@@ -52,23 +52,43 @@ class Uint(int, Codable, metaclass=IntCheckMeta):
|
|
|
52
52
|
# If the byte_size is set, the integer is fixed size.
|
|
53
53
|
# Otherwise, the integer is General Integer (supports up to 2**64 - 1)
|
|
54
54
|
byte_size: int = 0
|
|
55
|
+
signed = False
|
|
56
|
+
_bound = 1 << 64
|
|
57
|
+
|
|
58
|
+
def __class_getitem__(cls, data: int | tuple | bool | None):
|
|
59
|
+
"""
|
|
60
|
+
Args:
|
|
61
|
+
data: either byte_size or (byte_size, signed)
|
|
62
|
+
"""
|
|
63
|
+
if data == None:
|
|
64
|
+
size, signed = 0, False
|
|
65
|
+
# If we have a single value arg - wither byte_size or signed
|
|
66
|
+
elif not isinstance(data, tuple):
|
|
67
|
+
if isinstance(data, int):
|
|
68
|
+
size, signed = data, False
|
|
69
|
+
else:
|
|
70
|
+
size, signed = 0, bool(data)
|
|
71
|
+
else:
|
|
72
|
+
size, signed = data
|
|
55
73
|
|
|
56
|
-
|
|
57
|
-
|
|
74
|
+
return type(f"U{size}" if size else "Int", (cls,), {
|
|
75
|
+
"byte_size": size // 8,
|
|
76
|
+
"signed": signed,
|
|
77
|
+
"_bound": 1 << size if size > 0 else 1 << 64
|
|
78
|
+
})
|
|
58
79
|
|
|
59
80
|
def __new__(cls, value: Any):
|
|
60
81
|
value = int(value)
|
|
61
82
|
if cls.byte_size > 0:
|
|
62
|
-
|
|
63
|
-
min_v = 0
|
|
64
|
-
max_v = (1 << bits) - 1
|
|
65
|
-
if not (min_v <= value <= max_v):
|
|
66
|
-
raise ValueError(f"Fixed Int: {cls.__name__} out of range: {value!r} "
|
|
67
|
-
f"not in [{min_v}, {max_v}]")
|
|
83
|
+
max_v = cls._bound // 2 if cls.signed else cls._bound
|
|
84
|
+
min_v = -1 * cls._bound // 2 if cls.signed else 0
|
|
68
85
|
else:
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
86
|
+
min_v = -1 * cls._bound // 2 if cls.signed else 0
|
|
87
|
+
max_v = cls._bound // 2 if cls.signed else cls._bound - 1
|
|
88
|
+
|
|
89
|
+
if not (min_v <= value <= max_v):
|
|
90
|
+
raise ValueError(f"Int: {cls.__name__} out of range: {value!r} "
|
|
91
|
+
f"not in [{min_v}, {max_v}]")
|
|
72
92
|
return super().__new__(cls, value)
|
|
73
93
|
|
|
74
94
|
def __repr__(self):
|
|
@@ -105,11 +125,11 @@ class Uint(int, Codable, metaclass=IntCheckMeta):
|
|
|
105
125
|
# ---------------------------------------------------------------------------- #
|
|
106
126
|
# JSON Serde #
|
|
107
127
|
# ---------------------------------------------------------------------------- #
|
|
108
|
-
def to_json(self) ->
|
|
128
|
+
def to_json(self) -> int:
|
|
109
129
|
return int(self)
|
|
110
130
|
|
|
111
131
|
@classmethod
|
|
112
|
-
def from_json(cls, json_str: str) -> "
|
|
132
|
+
def from_json(cls, json_str: str) -> "Int":
|
|
113
133
|
return cls(int(json_str))
|
|
114
134
|
|
|
115
135
|
# ---------------------------------------------------------------------------- #
|
|
@@ -118,20 +138,24 @@ class Uint(int, Codable, metaclass=IntCheckMeta):
|
|
|
118
138
|
@staticmethod
|
|
119
139
|
def l(x):
|
|
120
140
|
return math.floor(Decimal(x).ln() / (Decimal(7) * Decimal(2).ln()))
|
|
121
|
-
|
|
141
|
+
|
|
142
|
+
def to_unsigned(self) -> "Int":
|
|
143
|
+
if not self.signed: return self
|
|
144
|
+
return int(self) + (self._bound // 2)
|
|
122
145
|
|
|
123
146
|
def encode_size(self) -> int:
|
|
124
147
|
if self.byte_size > 0:
|
|
125
148
|
return self.byte_size
|
|
126
149
|
else:
|
|
127
|
-
|
|
150
|
+
value = self.to_unsigned()
|
|
151
|
+
if value < 2**7:
|
|
128
152
|
return 1
|
|
129
|
-
elif
|
|
153
|
+
elif value < 2 ** (7 * 9):
|
|
130
154
|
return 1 + self.l(self)
|
|
131
|
-
elif
|
|
155
|
+
elif value < 2**64:
|
|
132
156
|
return 9
|
|
133
157
|
else:
|
|
134
|
-
raise ValueError("Value too large for encoding. General
|
|
158
|
+
raise ValueError("Value too large for encoding. General Int support up to 2**64 - 1")
|
|
135
159
|
|
|
136
160
|
def encode_into(self, buffer: bytearray, offset: int = 0) -> int:
|
|
137
161
|
if self.byte_size > 0:
|
|
@@ -214,7 +238,8 @@ class Uint(int, Codable, metaclass=IntCheckMeta):
|
|
|
214
238
|
return cls(int("".join(str(int(b)) for b in reversed(bits)), 2))
|
|
215
239
|
|
|
216
240
|
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
241
|
+
Uint = Int
|
|
242
|
+
U8 = Int[8]
|
|
243
|
+
U16 = Int[16]
|
|
244
|
+
U32 = Int[32]
|
|
245
|
+
U64 = Int[64]
|
tsrkit_types/null.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from typing import Optional, Tuple, Union
|
|
2
|
+
from tsrkit_types.itf.codable import Codable
|
|
2
3
|
|
|
3
|
-
|
|
4
|
-
class NullType:
|
|
4
|
+
class NullType(Codable):
|
|
5
5
|
def __repr__(self):
|
|
6
6
|
return "Null"
|
|
7
7
|
|
|
@@ -40,4 +40,4 @@ class NullType:
|
|
|
40
40
|
|
|
41
41
|
|
|
42
42
|
|
|
43
|
-
Null = NullType()
|
|
43
|
+
Null = NullType()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tsrkit-types
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.6
|
|
4
4
|
Summary: Performant Python Typings library for type-safe binary serialization, JSON encoding, and data validation with zero dependencies
|
|
5
5
|
Author-email: chainscore-labs <hello@chainscore.finance>, prasad-kumkar <prasad@chainscore.finance>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -4,18 +4,18 @@ tsrkit_types/bool.py,sha256=Veu6KLIeiRFnIW1sbawJ0haF0veyaL-As5mBiypq-uE,1280
|
|
|
4
4
|
tsrkit_types/bytearray.py,sha256=DaMnACq-7pzEW-pRO2f_IAny_8ldhSt3G5D24Soywk4,1662
|
|
5
5
|
tsrkit_types/bytes.py,sha256=V_sFmpOsftGb3IPXqExOlDHbt4CpJWM6md-kQ3_CEmg,2734
|
|
6
6
|
tsrkit_types/bytes_common.py,sha256=b1Zqh_NhkCX718QaZC52J3nEzKAi1Fe8E0nefWVwwmo,2301
|
|
7
|
-
tsrkit_types/choice.py,sha256=
|
|
7
|
+
tsrkit_types/choice.py,sha256=OVHxIr2PcnaTpo5sAMjipCArc5oATNsyYkqZNRqU7bA,5253
|
|
8
8
|
tsrkit_types/dictionary.py,sha256=GcUvaHC1VOEc3OzUVAH0id--ncLqTJ6ZRz-lucMNRdY,5618
|
|
9
9
|
tsrkit_types/enum.py,sha256=3MyLW15_ToQQdctJjcMY8Xb3OsS_Ad997OOEo8FjVeA,4256
|
|
10
|
-
tsrkit_types/integers.py,sha256=
|
|
11
|
-
tsrkit_types/null.py,sha256=
|
|
10
|
+
tsrkit_types/integers.py,sha256=OARdkz7j06xCU2AZz4_RciR1hVexSnF1tAQ3HQGNZI8,8858
|
|
11
|
+
tsrkit_types/null.py,sha256=vzobz4-xJrUeky8pzbg-Qmz4N6b7GsCNf8TafbstlgY,1372
|
|
12
12
|
tsrkit_types/option.py,sha256=xwtaAFN20GjaGqzYZAAo1fZqqUZnCYHdy2bjshhY620,1631
|
|
13
13
|
tsrkit_types/sequences.py,sha256=KOT5-4mia5Lc0CMjIF7Yqe34Sei92ZQ2s6GaP13soUg,8090
|
|
14
14
|
tsrkit_types/string.py,sha256=8rvg0BwvyhQnbMW3qornmBFTQHeTQFUfwD7OC0eVgcY,2575
|
|
15
15
|
tsrkit_types/struct.py,sha256=GoXWFc1OqO9CXnxFS7Xkms7J-rtZe8h32LkAIBlzdSM,3497
|
|
16
16
|
tsrkit_types/itf/codable.py,sha256=agx8YSVWGBeEiLrU9eqh3ZoeTgpJsmmIlW622xIKqPI,2810
|
|
17
|
-
tsrkit_types-0.1.
|
|
18
|
-
tsrkit_types-0.1.
|
|
19
|
-
tsrkit_types-0.1.
|
|
20
|
-
tsrkit_types-0.1.
|
|
21
|
-
tsrkit_types-0.1.
|
|
17
|
+
tsrkit_types-0.1.6.dist-info/licenses/LICENSE,sha256=TwnDvVCPwHadHWLUuY1sPx03XNw1jzh_ZmoDBNai9Uc,1072
|
|
18
|
+
tsrkit_types-0.1.6.dist-info/METADATA,sha256=JLEqIyXYIju0AqWihQakM593qyyKNvdMjuTCVo9CKBI,22755
|
|
19
|
+
tsrkit_types-0.1.6.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
20
|
+
tsrkit_types-0.1.6.dist-info/top_level.txt,sha256=pnVhnUsnZ_A0FIj1zHwDw3suMGrfMJwusp-4GPVY1CM,13
|
|
21
|
+
tsrkit_types-0.1.6.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|