tsrkit-types 0.1.3__py3-none-any.whl → 0.1.5__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/__init__.py +2 -2
- tsrkit_types/bytes.py +7 -7
- tsrkit_types/choice.py +4 -1
- tsrkit_types/integers.py +45 -21
- tsrkit_types/null.py +3 -3
- {tsrkit_types-0.1.3.dist-info → tsrkit_types-0.1.5.dist-info}/METADATA +1 -1
- {tsrkit_types-0.1.3.dist-info → tsrkit_types-0.1.5.dist-info}/RECORD +10 -10
- {tsrkit_types-0.1.3.dist-info → tsrkit_types-0.1.5.dist-info}/WHEEL +0 -0
- {tsrkit_types-0.1.3.dist-info → tsrkit_types-0.1.5.dist-info}/licenses/LICENSE +0 -0
- {tsrkit_types-0.1.3.dist-info → tsrkit_types-0.1.5.dist-info}/top_level.txt +0 -0
tsrkit_types/__init__.py
CHANGED
|
@@ -35,7 +35,7 @@ from .sequences import (
|
|
|
35
35
|
from .dictionary import Dictionary
|
|
36
36
|
|
|
37
37
|
# Bytes types
|
|
38
|
-
from .bytes import Bytes,
|
|
38
|
+
from .bytes import Bytes, Bytes16, Bytes32, Bytes64, Bytes128, Bytes256, Bytes512, Bytes1024
|
|
39
39
|
from .bytearray import ByteArray
|
|
40
40
|
|
|
41
41
|
# Bit types
|
|
@@ -76,7 +76,7 @@ __all__ = [
|
|
|
76
76
|
"Dictionary",
|
|
77
77
|
|
|
78
78
|
# Bytes types
|
|
79
|
-
"Bytes", "
|
|
79
|
+
"Bytes", "Bytes16", "Bytes32", "Bytes64", "Bytes128", "Bytes256", "Bytes512", "Bytes1024",
|
|
80
80
|
"ByteArray",
|
|
81
81
|
|
|
82
82
|
# Bit types
|
tsrkit_types/bytes.py
CHANGED
|
@@ -61,10 +61,10 @@ class Bytes(bytes, Codable, BytesMixin, metaclass=BytesCheckMeta):
|
|
|
61
61
|
# ---------------------------------------------------------------------------- #
|
|
62
62
|
# JSON methods inherited from BytesMixin
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
64
|
+
Bytes16 = Bytes[16]
|
|
65
|
+
Bytes32 = Bytes[32]
|
|
66
|
+
Bytes64 = Bytes[64]
|
|
67
|
+
Bytes128 = Bytes[128]
|
|
68
|
+
Bytes256 = Bytes[256]
|
|
69
|
+
Bytes512 = Bytes[512]
|
|
70
|
+
Bytes1024 = Bytes[1024]
|
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,42 @@ 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
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
signed = False
|
|
56
|
+
_bound = 0
|
|
57
|
+
|
|
58
|
+
def __class_getitem__(cls, data: int | tuple):
|
|
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
|
|
73
|
+
return type(f"U{size}" if size else "Int", (cls,), {
|
|
74
|
+
"byte_size": size // 8,
|
|
75
|
+
"signed": signed,
|
|
76
|
+
"_bound": 1 << size if size > 0 else 1 << 64
|
|
77
|
+
})
|
|
58
78
|
|
|
59
79
|
def __new__(cls, value: Any):
|
|
60
80
|
value = int(value)
|
|
61
81
|
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}]")
|
|
82
|
+
max_v = cls._bound // 2 if cls.signed else cls._bound
|
|
83
|
+
min_v = -1 * cls._bound // 2 if cls.signed else 0
|
|
68
84
|
else:
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
85
|
+
min_v = -1 * cls._bound // 2 if cls.signed else 0
|
|
86
|
+
max_v = cls._bound // 2 if cls.signed else cls._bound - 1
|
|
87
|
+
|
|
88
|
+
if not (min_v <= value <= max_v):
|
|
89
|
+
raise ValueError(f"Int: {cls.__name__} out of range: {value!r} "
|
|
90
|
+
f"not in [{min_v}, {max_v}]")
|
|
72
91
|
return super().__new__(cls, value)
|
|
73
92
|
|
|
74
93
|
def __repr__(self):
|
|
@@ -105,11 +124,11 @@ class Uint(int, Codable, metaclass=IntCheckMeta):
|
|
|
105
124
|
# ---------------------------------------------------------------------------- #
|
|
106
125
|
# JSON Serde #
|
|
107
126
|
# ---------------------------------------------------------------------------- #
|
|
108
|
-
def to_json(self) ->
|
|
127
|
+
def to_json(self) -> int:
|
|
109
128
|
return int(self)
|
|
110
129
|
|
|
111
130
|
@classmethod
|
|
112
|
-
def from_json(cls, json_str: str) -> "
|
|
131
|
+
def from_json(cls, json_str: str) -> "Int":
|
|
113
132
|
return cls(int(json_str))
|
|
114
133
|
|
|
115
134
|
# ---------------------------------------------------------------------------- #
|
|
@@ -118,20 +137,24 @@ class Uint(int, Codable, metaclass=IntCheckMeta):
|
|
|
118
137
|
@staticmethod
|
|
119
138
|
def l(x):
|
|
120
139
|
return math.floor(Decimal(x).ln() / (Decimal(7) * Decimal(2).ln()))
|
|
121
|
-
|
|
140
|
+
|
|
141
|
+
def to_unsigned(self) -> "Int":
|
|
142
|
+
if not self.signed: return self
|
|
143
|
+
return int(self) + (self._bound // 2)
|
|
122
144
|
|
|
123
145
|
def encode_size(self) -> int:
|
|
124
146
|
if self.byte_size > 0:
|
|
125
147
|
return self.byte_size
|
|
126
148
|
else:
|
|
127
|
-
|
|
149
|
+
value = self.to_unsigned()
|
|
150
|
+
if value < 2**7:
|
|
128
151
|
return 1
|
|
129
|
-
elif
|
|
152
|
+
elif value < 2 ** (7 * 9):
|
|
130
153
|
return 1 + self.l(self)
|
|
131
|
-
elif
|
|
154
|
+
elif value < 2**64:
|
|
132
155
|
return 9
|
|
133
156
|
else:
|
|
134
|
-
raise ValueError("Value too large for encoding. General
|
|
157
|
+
raise ValueError("Value too large for encoding. General Int support up to 2**64 - 1")
|
|
135
158
|
|
|
136
159
|
def encode_into(self, buffer: bytearray, offset: int = 0) -> int:
|
|
137
160
|
if self.byte_size > 0:
|
|
@@ -214,7 +237,8 @@ class Uint(int, Codable, metaclass=IntCheckMeta):
|
|
|
214
237
|
return cls(int("".join(str(int(b)) for b in reversed(bits)), 2))
|
|
215
238
|
|
|
216
239
|
|
|
240
|
+
Uint = Int
|
|
217
241
|
U8 = Uint[8]
|
|
218
242
|
U16 = Uint[16]
|
|
219
243
|
U32 = Uint[32]
|
|
220
|
-
U64 = Uint[64]
|
|
244
|
+
U64 = Uint[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.5
|
|
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
|
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
tsrkit_types/__init__.py,sha256=
|
|
1
|
+
tsrkit_types/__init__.py,sha256=PlhqxNwjXaJU5vTIqJdLvC5BBfExLAoKQ57peNG2Opc,1900
|
|
2
2
|
tsrkit_types/bits.py,sha256=33WLfuUYdzGscoc-E3InTYdbJRIDeixoe_pG0XAv71c,4213
|
|
3
3
|
tsrkit_types/bool.py,sha256=Veu6KLIeiRFnIW1sbawJ0haF0veyaL-As5mBiypq-uE,1280
|
|
4
4
|
tsrkit_types/bytearray.py,sha256=DaMnACq-7pzEW-pRO2f_IAny_8ldhSt3G5D24Soywk4,1662
|
|
5
|
-
tsrkit_types/bytes.py,sha256=
|
|
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=_JkceknSupr42TaLEm8W31MsxLitoncJyartSI_FeiU,8840
|
|
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.5.dist-info/licenses/LICENSE,sha256=TwnDvVCPwHadHWLUuY1sPx03XNw1jzh_ZmoDBNai9Uc,1072
|
|
18
|
+
tsrkit_types-0.1.5.dist-info/METADATA,sha256=-chUqeENY3cHR3cBh6R-di6VT2ijAQ35Qh0nzjS3P68,22755
|
|
19
|
+
tsrkit_types-0.1.5.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
20
|
+
tsrkit_types-0.1.5.dist-info/top_level.txt,sha256=pnVhnUsnZ_A0FIj1zHwDw3suMGrfMJwusp-4GPVY1CM,13
|
|
21
|
+
tsrkit_types-0.1.5.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|