tsrkit-types 0.1.4__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/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 Uint(int, Codable, metaclass=IntCheckMeta):
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
- def __class_getitem__(cls, size: int):
57
- return type(f"U{size}" if size else "Int", (cls,), {"byte_size": size // 8})
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
- bits = 8 * cls.byte_size
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
- if not 0 <= value < 2 ** 64:
70
- raise ValueError(f"General Int: Value must be between 0 and 2**64 - 1, got {value}")
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) -> str:
127
+ def to_json(self) -> int:
109
128
  return int(self)
110
129
 
111
130
  @classmethod
112
- def from_json(cls, json_str: str) -> "Uint":
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
- if self < 2**7:
149
+ value = self.to_unsigned()
150
+ if value < 2**7:
128
151
  return 1
129
- elif self < 2 ** (7 * 9):
152
+ elif value < 2 ** (7 * 9):
130
153
  return 1 + self.l(self)
131
- elif self < 2**64:
154
+ elif value < 2**64:
132
155
  return 9
133
156
  else:
134
- raise ValueError("Value too large for encoding. General Uint support up to 2**64 - 1")
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.4
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
@@ -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=zNLvB1jSZERot6Qo9cxxkPcY84DAJss8o0ULyp9D--4,5196
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=w8WCbzzQP0YX3ky4iP-fbNeOBHsIydsU0Tady8iGQPQ,8026
11
- tsrkit_types/null.py,sha256=OwHVhWLdazaFLXXR8iHPaHe67NPmYHDCavBxUfcZ53s,1318
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.4.dist-info/licenses/LICENSE,sha256=TwnDvVCPwHadHWLUuY1sPx03XNw1jzh_ZmoDBNai9Uc,1072
18
- tsrkit_types-0.1.4.dist-info/METADATA,sha256=d2IfqIbH8KWzDFTNCi8sMUvneof17V3CSrsZTBEbsKo,22755
19
- tsrkit_types-0.1.4.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
20
- tsrkit_types-0.1.4.dist-info/top_level.txt,sha256=pnVhnUsnZ_A0FIj1zHwDw3suMGrfMJwusp-4GPVY1CM,13
21
- tsrkit_types-0.1.4.dist-info/RECORD,,
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,,