hexio 0.0.2__tar.gz → 0.0.4__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hexio
3
- Version: 0.0.2
3
+ Version: 0.0.4
4
4
  Summary: Utility package to handle I/O of hex-compatible data.
5
5
  License-Expression: GPL-3.0-or-later
6
6
  Project-URL: Homepage, https://github.com/valbucci/hexio
@@ -141,14 +141,6 @@ class HexStr(str):
141
141
  logger.error(f"Tried to create {cls.__name__} from object: {value!r}")
142
142
  raise TypeError(f"Unsupported type: {type(value)}")
143
143
 
144
- def __len__(self) -> int:
145
- return len(str(self))
146
-
147
- def __str__(self) -> str:
148
- return super().__str__().upper()
149
-
150
- def __int__(self) -> int:
151
- return int(self, 16)
152
144
 
153
145
  def __xor__(self, other: HexValue) -> Self:
154
146
  if len(self) != len(HexStr(other)):
@@ -165,16 +157,30 @@ class HexStr(str):
165
157
  logger.debug(f"HexStr length mismatch: {len(self)} != {len(HexStr(other))}")
166
158
  return self.__class__(int(self) | int(HexStr(other)))
167
159
 
160
+
161
+ def __str__(self) -> str:
162
+ return super().__str__().upper()
163
+
164
+ def __repr__(self) -> str:
165
+ return f"{self.__class__.__name__}('{super().__str__()}')"
166
+
167
+ def __int__(self) -> int:
168
+ return int(self, 16)
169
+
168
170
  def __bytes__(self) -> bytes:
169
171
  if len(self) % 2 == 1:
170
172
  return bytes.fromhex(f"0{self}")
171
173
  return bytes.fromhex(self)
174
+
175
+ def __len__(self) -> int:
176
+ return len(self.__str__())
172
177
 
173
178
  def __index__(self) -> int:
174
179
  return self.__int__()
180
+
181
+ def __hash__(self) -> int:
182
+ return hash(self.__str__())
175
183
 
176
- def __repr__(self) -> str:
177
- return f"{self.__class__.__name__}('{super().__str__()}')"
178
184
 
179
185
  def __eq__(self, other: object) -> bool:
180
186
  if isinstance(other, HexStr):
@@ -219,9 +225,17 @@ class HexStr(str):
219
225
 
220
226
  def __ge__(self, other: object) -> bool:
221
227
  return not self.__lt__(other)
228
+
229
+
230
+ def __getitem__(self, key) -> Self:
231
+ return self.__class__(self.__str__()[key])
232
+
233
+ def __setitem__(self, key, value) -> None:
234
+ raise TypeError("'HexStr' object does not support item assignment")
235
+
236
+ def __delitem__(self, key) -> None:
237
+ raise TypeError("'HexStr' object does not support item deletion")
222
238
 
223
- def __hash__(self) -> int:
224
- return hash(self.__str__())
225
239
 
226
240
  @classmethod
227
241
  def from_bytes(cls, value: bytes) -> Self:
@@ -295,7 +309,16 @@ class HexInt(int):
295
309
  return hash(self.__int__())
296
310
 
297
311
  def __bytes__(self) -> bytes:
298
- return self.to_bytes()
312
+ bit_length = self.bit_length()
313
+ byte_length = bit_length // 8
314
+ if bit_length % 8 != 0:
315
+ byte_length = byte_length + 1
316
+
317
+ return self.to_bytes(
318
+ length=byte_length,
319
+ byteorder="big",
320
+ signed=False
321
+ )
299
322
 
300
323
  def __str__(self) -> str:
301
324
  return f"0x{self:x}"
@@ -337,6 +360,13 @@ class HexInt(int):
337
360
  def __sub__(self, other: HexValue) -> Self:
338
361
  other = hexvalue_to_int(other)
339
362
  return self.__class__(int(self) - other)
363
+
364
+ def __mul__(self, other: HexValue) -> Self:
365
+ other = hexvalue_to_int(other)
366
+ return self.__class__(int(self) * other)
367
+
368
+ def __rmul__(self, other: HexValue) -> Self:
369
+ return self.__mul__(other)
340
370
 
341
371
  def __xor__(self, other: HexValue) -> Self:
342
372
  other = hexvalue_to_int(other)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: hexio
3
- Version: 0.0.2
3
+ Version: 0.0.4
4
4
  Summary: Utility package to handle I/O of hex-compatible data.
5
5
  License-Expression: GPL-3.0-or-later
6
6
  Project-URL: Homepage, https://github.com/valbucci/hexio
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
4
4
 
5
5
  [project]
6
6
  name = "hexio"
7
- version = "0.0.2"
7
+ version = "0.0.4"
8
8
  description = "Utility package to handle I/O of hex-compatible data."
9
9
  requires-python = ">=3.9"
10
10
  dependencies = [
@@ -131,6 +131,11 @@ class TestHexStr:
131
131
  assert HexStr("0x1234") | HexStr("01234") == HexStr("0x1234")
132
132
 
133
133
  assert HexStr("0xffffff") | 0xAA0011 == HexStr("0xffffff")
134
+
135
+ def test_slice(self):
136
+ example = HexStr("1234")
137
+ assert example[2:] == "34"
138
+ assert example[2:] == HexStr("34")
134
139
 
135
140
 
136
141
  class TestNibble:
@@ -178,6 +183,23 @@ class TestNibble:
178
183
 
179
184
 
180
185
  class TestHexInt:
186
+
187
+ def test_bytes(self):
188
+ big_int = HexInt(0xffffffffff)
189
+ big_bytes = bytes(big_int)
190
+ big_hex = hex(HexInt(big_bytes))
191
+ assert big_hex == "0xffffffffff"
192
+
193
+ # Make sure alignment works
194
+ big_int2 = big_int * 2 # this should add one bit
195
+ assert isinstance(big_int2, HexInt)
196
+ assert big_int2.bit_length() == big_int.bit_length() + 1
197
+ assert big_int2.bit_length() % 8 == 1
198
+ big_bytes2 = bytes(big_int2)
199
+ big_hex2 = hex(HexInt(big_bytes2))
200
+ assert big_hex2 == "0x1fffffffffe"
201
+ assert len(big_bytes2) == len(big_bytes) + 1
202
+
181
203
  def test_xor(self):
182
204
  assert HexInt("0x1234") ^ HexInt("0x1235") == HexInt("0x0001")
183
205
  assert HexInt("0x1234") ^ HexInt("0x1233") == HexInt("0x0007")
@@ -204,7 +226,7 @@ class TestHexOffset:
204
226
  assert HexOffset("1234") == HexOffset(HexStr("0x00000000000001234"))
205
227
  assert HexOffset("1234") == HexOffset(b"\x12\x34")
206
228
 
207
- with pytest.raises(ValueError):
229
+ with pytest.raises(TypeError):
208
230
  _ = HexOffset(None) # type: ignore
209
231
  with pytest.raises(ValueError):
210
232
  _ = HexOffset("g")
File without changes
File without changes
File without changes
File without changes
File without changes