tsrkit-types 0.1.1__tar.gz → 0.1.3__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.
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/CHANGELOG.md +16 -1
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/PKG-INFO +102 -23
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/README.md +100 -20
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/pyproject.toml +2 -3
- tsrkit_types-0.1.3/tests/test_bits.py +402 -0
- tsrkit_types-0.1.3/tests/test_bytearray.py +433 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tests/test_bytes.py +5 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tests/test_int.py +5 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tests/test_structs.py +20 -4
- tsrkit_types-0.1.3/tests/type_hints/test_struct_serde.py +22 -0
- tsrkit_types-0.1.3/tsrkit_types/__init__.py +95 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types/bits.py +39 -20
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types/bool.py +4 -8
- tsrkit_types-0.1.3/tsrkit_types/bytearray.py +37 -0
- tsrkit_types-0.1.3/tsrkit_types/bytes.py +70 -0
- tsrkit_types-0.1.3/tsrkit_types/bytes_common.py +68 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types/choice.py +4 -4
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types/dictionary.py +1 -1
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types/enum.py +3 -3
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types/integers.py +17 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types/itf/codable.py +2 -1
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types/null.py +3 -3
- tsrkit_types-0.1.3/tsrkit_types/option.py +48 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types/sequences.py +4 -4
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types/struct.py +16 -10
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types.egg-info/PKG-INFO +102 -23
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types.egg-info/SOURCES.txt +5 -0
- tsrkit_types-0.1.1/tsrkit_types/__init__.py +0 -76
- tsrkit_types-0.1.1/tsrkit_types/bytes.py +0 -102
- tsrkit_types-0.1.1/tsrkit_types/option.py +0 -30
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/CONTRIBUTING.md +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/LICENSE +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/MANIFEST.in +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/pytest.ini +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/setup.cfg +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/setup.py +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tests/test_choices.py +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tests/test_containers.py +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tests/test_enums.py +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tests/test_integers.py +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tests/test_network.py +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tests/test_seq.py +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tests/test_strings.py +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tests/test_struct.py +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types/string.py +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types.egg-info/dependency_links.txt +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types.egg-info/requires.txt +0 -0
- {tsrkit_types-0.1.1 → tsrkit_types-0.1.3}/tsrkit_types.egg-info/top_level.txt +0 -0
|
@@ -35,7 +35,22 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
35
35
|
- Type validation at construction and assignment time
|
|
36
36
|
- Support for nested and complex type compositions
|
|
37
37
|
- Zero-dependency core library
|
|
38
|
-
- Python 3.
|
|
38
|
+
- Python 3.11+ support
|
|
39
|
+
|
|
40
|
+
## [0.1.3] - 2025-01-03
|
|
41
|
+
|
|
42
|
+
### Fixed
|
|
43
|
+
- **Option JSON handling**: Fixed `Option.from_json()` to properly handle `None` values by creating empty Options
|
|
44
|
+
- **Choice serialization**: Improved `Choice.encode_into()` to correctly identify choice variants using both key and type matching, fixing issues with duplicate keys (like in Option types)
|
|
45
|
+
- **Struct None handling**: Enhanced `struct.from_json()` to properly handle `None` values for Option and NullType fields without requiring explicit defaults
|
|
46
|
+
|
|
47
|
+
### Added
|
|
48
|
+
- **Option JSON methods**: Added dedicated `to_json()` and `from_json()` methods to Option class for cleaner JSON serialization
|
|
49
|
+
- **Improved type hints**: Enhanced struct decorator to provide better IDE support and type hints for Codable interface methods
|
|
50
|
+
|
|
51
|
+
### Changed
|
|
52
|
+
- **Choice from_json**: Simplified Choice.from_json() by moving Option-specific logic to Option class
|
|
53
|
+
- **Serialization robustness**: Made Choice encoding more reliable for complex type hierarchies
|
|
39
54
|
|
|
40
55
|
## [Unreleased]
|
|
41
56
|
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: tsrkit-types
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.3
|
|
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
|
|
@@ -15,7 +15,6 @@ Classifier: Intended Audience :: Developers
|
|
|
15
15
|
Classifier: Intended Audience :: System Administrators
|
|
16
16
|
Classifier: Operating System :: OS Independent
|
|
17
17
|
Classifier: Programming Language :: Python :: 3
|
|
18
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
19
18
|
Classifier: Programming Language :: Python :: 3.11
|
|
20
19
|
Classifier: Programming Language :: Python :: 3.12
|
|
21
20
|
Classifier: Programming Language :: Python :: 3 :: Only
|
|
@@ -28,7 +27,7 @@ Classifier: Topic :: Utilities
|
|
|
28
27
|
Classifier: Topic :: System :: Archiving
|
|
29
28
|
Classifier: Topic :: Communications
|
|
30
29
|
Classifier: Typing :: Typed
|
|
31
|
-
Requires-Python: >=3.
|
|
30
|
+
Requires-Python: >=3.11
|
|
32
31
|
Description-Content-Type: text/markdown
|
|
33
32
|
License-File: LICENSE
|
|
34
33
|
Provides-Extra: dev
|
|
@@ -300,7 +299,9 @@ restored = StringToInt.from_json(json_data)
|
|
|
300
299
|
|
|
301
300
|
### Bytes Types
|
|
302
301
|
|
|
303
|
-
|
|
302
|
+
The library provides two complementary byte array types: immutable `Bytes` (extending Python's built-in `bytes`) and mutable `ByteArray` (extending Python's `bytearray`). Both types share common functionality through a mixin architecture for bit conversion, JSON serialization, and binary encoding.
|
|
303
|
+
|
|
304
|
+
#### Bytes (Immutable - extension of Python bytes)
|
|
304
305
|
|
|
305
306
|
```python
|
|
306
307
|
from tsrkit_types.bytes import Bytes
|
|
@@ -308,47 +309,115 @@ from tsrkit_types.bytes import Bytes
|
|
|
308
309
|
# Creation
|
|
309
310
|
data = Bytes(b"Hello, binary world!")
|
|
310
311
|
data = Bytes([0x01, 0x02, 0x03, 0x04])
|
|
312
|
+
data = Bytes("48656c6c6f") # From hex string
|
|
311
313
|
|
|
312
|
-
#
|
|
313
|
-
data.to_bits()
|
|
314
|
-
Bytes.from_bits(
|
|
314
|
+
# Shared Operations (via BytesMixin)
|
|
315
|
+
bits = data.to_bits() # Convert to bit list [True, False, True, ...]
|
|
316
|
+
data2 = Bytes.from_bits(bits) # Create from bit list
|
|
315
317
|
|
|
316
|
-
#
|
|
318
|
+
# Properties
|
|
317
319
|
length = len(data) # Byte length
|
|
318
320
|
raw_bytes = bytes(data) # Convert to Python bytes
|
|
319
321
|
|
|
320
|
-
# Encoding
|
|
322
|
+
# Encoding/Decoding
|
|
321
323
|
encoded = data.encode() # [length][raw_bytes]
|
|
322
324
|
decoded = Bytes.decode(encoded)
|
|
323
325
|
|
|
324
326
|
# JSON serialization (hex encoded)
|
|
325
327
|
json_str = data.to_json() # "48656c6c6f2c2062696e61727920776f726c6421"
|
|
326
328
|
restored = Bytes.from_json(json_str)
|
|
329
|
+
restored2 = Bytes.from_json("0x48656c6c6f") # Supports 0x prefix
|
|
327
330
|
```
|
|
328
331
|
|
|
329
|
-
####
|
|
332
|
+
#### ByteArray (Mutable - extension of Python bytearray)
|
|
330
333
|
|
|
331
334
|
```python
|
|
332
|
-
from tsrkit_types.
|
|
335
|
+
from tsrkit_types.bytearray import ByteArray
|
|
333
336
|
|
|
334
337
|
# Creation
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
+
data = ByteArray(b"Hello, binary world!")
|
|
339
|
+
data = ByteArray([0x01, 0x02, 0x03, 0x04])
|
|
340
|
+
data = ByteArray("48656c6c6f") # From hex string
|
|
341
|
+
|
|
342
|
+
# Mutable Operations
|
|
343
|
+
data.append(0xFF) # Add single byte
|
|
344
|
+
data.extend([0xAB, 0xCD]) # Add multiple bytes
|
|
345
|
+
data.insert(0, 0x00) # Insert byte at position
|
|
346
|
+
data.pop() # Remove and return last byte
|
|
347
|
+
data.remove(0xFF) # Remove first occurrence
|
|
348
|
+
data.clear() # Remove all bytes
|
|
349
|
+
data.reverse() # Reverse in-place
|
|
350
|
+
|
|
351
|
+
# Indexing and Slicing (mutable)
|
|
352
|
+
data[0] = 0x42 # Set byte at index
|
|
353
|
+
data[1:3] = [0x43, 0x44] # Set slice
|
|
354
|
+
del data[0] # Delete byte at index
|
|
355
|
+
|
|
356
|
+
# Shared Operations (via BytesMixin) - same as Bytes
|
|
357
|
+
bits = data.to_bits() # Convert to bit list
|
|
358
|
+
data2 = ByteArray.from_bits(bits) # Create from bit list
|
|
359
|
+
|
|
360
|
+
# Properties and Conversion
|
|
361
|
+
length = len(data) # Byte length
|
|
362
|
+
raw_bytes = bytes(data) # Convert to immutable bytes
|
|
363
|
+
immutable = Bytes(data) # Convert to immutable Bytes
|
|
364
|
+
|
|
365
|
+
# Encoding/Decoding (same interface as Bytes)
|
|
366
|
+
encoded = data.encode() # [length][raw_bytes]
|
|
367
|
+
decoded = ByteArray.decode(encoded)
|
|
368
|
+
|
|
369
|
+
# JSON serialization (same interface as Bytes)
|
|
370
|
+
json_str = data.to_json() # "48656c6c6f..."
|
|
371
|
+
restored = ByteArray.from_json(json_str)
|
|
372
|
+
```
|
|
373
|
+
|
|
374
|
+
**Key Differences:**
|
|
375
|
+
- **Bytes**: Immutable, extends `bytes`, memory-efficient for read-only data
|
|
376
|
+
- **ByteArray**: Mutable, extends `bytearray`, suitable for dynamic byte manipulation
|
|
377
|
+
- **Shared Functionality**: Both support identical bit conversion, JSON serialization, and binary encoding through `BytesMixin`
|
|
378
|
+
|
|
379
|
+
**Common Features (Both Types):**
|
|
380
|
+
- Bit-level conversion with MSB/LSB support
|
|
381
|
+
- Hex string JSON serialization with 0x prefix support
|
|
382
|
+
- Efficient binary encoding with length prefix
|
|
383
|
+
- String representation and validation
|
|
384
|
+
- Memory-efficient operations
|
|
385
|
+
|
|
386
|
+
#### Bits (Bit Arrays - Sequence of bool)
|
|
387
|
+
|
|
388
|
+
```python
|
|
389
|
+
from tsrkit_types.bits import Bits
|
|
390
|
+
|
|
391
|
+
# Creation
|
|
392
|
+
bits = Bits([True, False, True, True, False])
|
|
393
|
+
bits = Bits.from_hex("1A3F") # From hex string
|
|
394
|
+
bits = Bits.from_int(42, 8) # From integer with bit width
|
|
395
|
+
|
|
396
|
+
# Fixed-size parameterized bits
|
|
397
|
+
FixedBits = Bits[8] # Exactly 8 bits
|
|
398
|
+
fixed = FixedBits([True, False, True, True, False, False, True, False])
|
|
338
399
|
|
|
339
400
|
# Operations
|
|
340
|
-
bit_val = bits[0]
|
|
341
|
-
bits[1] = True
|
|
342
|
-
bits.append(False)
|
|
343
|
-
bits.extend([True, False])
|
|
401
|
+
bit_val = bits[0] # Get bit at index
|
|
402
|
+
bits[1] = True # Set bit at index
|
|
403
|
+
bits.append(False) # Add bit
|
|
404
|
+
bits.extend([True, False]) # Add multiple bits
|
|
344
405
|
|
|
345
406
|
# Conversion
|
|
346
|
-
hex_str = bits.to_hex()
|
|
347
|
-
int_val = bits.to_int()
|
|
407
|
+
hex_str = bits.to_hex() # Convert to hex string
|
|
408
|
+
int_val = bits.to_int() # Convert to integer
|
|
409
|
+
|
|
410
|
+
# Bit order specification
|
|
411
|
+
bits_msb = Bits([True, False], bit_order="MSB") # Most significant bit first
|
|
412
|
+
bits_lsb = Bits([True, False], bit_order="LSB") # Least significant bit first
|
|
348
413
|
|
|
349
414
|
# Encoding
|
|
350
|
-
encoded = bits.encode()
|
|
351
|
-
decoded =
|
|
415
|
+
encoded = bits.encode() # [length][packed_bits]
|
|
416
|
+
decoded = Bits.decode(encoded)
|
|
417
|
+
|
|
418
|
+
# JSON serialization
|
|
419
|
+
json_str = bits.to_json() # Hex string representation
|
|
420
|
+
restored = Bits.from_json(json_str)
|
|
352
421
|
```
|
|
353
422
|
|
|
354
423
|
### Enum (Extension of Python Enum, with Codable + JSON support)
|
|
@@ -559,6 +628,16 @@ buffer = bytearray(1024)
|
|
|
559
628
|
offset = 0
|
|
560
629
|
offset += value1.encode_into(buffer, offset)
|
|
561
630
|
offset += value2.encode_into(buffer, offset)
|
|
631
|
+
|
|
632
|
+
# Choose appropriate byte type for your use case
|
|
633
|
+
# Use Bytes for immutable binary data (memory efficient, read-only)
|
|
634
|
+
config_data = Bytes(b"static configuration")
|
|
635
|
+
|
|
636
|
+
# Use ByteArray for dynamic binary buffers (mutable, growing data)
|
|
637
|
+
dynamic_buffer = ByteArray()
|
|
638
|
+
dynamic_buffer.extend([0x01, 0x02])
|
|
639
|
+
dynamic_buffer.append(0x03)
|
|
640
|
+
dynamic_buffer.insert(0, 0x00) # Result: [0x00, 0x01, 0x02, 0x03]
|
|
562
641
|
```
|
|
563
642
|
|
|
564
643
|
## Examples
|
|
@@ -735,6 +814,6 @@ Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for gui
|
|
|
735
814
|
|
|
736
815
|
## Requirements
|
|
737
816
|
|
|
738
|
-
- **Python**: >= 3.
|
|
817
|
+
- **Python**: >= 3.11
|
|
739
818
|
- **Runtime Dependencies**: None (zero dependencies!)
|
|
740
819
|
- **Development Dependencies**: pytest and plugins (see `pyproject.toml`)
|
|
@@ -258,7 +258,9 @@ restored = StringToInt.from_json(json_data)
|
|
|
258
258
|
|
|
259
259
|
### Bytes Types
|
|
260
260
|
|
|
261
|
-
|
|
261
|
+
The library provides two complementary byte array types: immutable `Bytes` (extending Python's built-in `bytes`) and mutable `ByteArray` (extending Python's `bytearray`). Both types share common functionality through a mixin architecture for bit conversion, JSON serialization, and binary encoding.
|
|
262
|
+
|
|
263
|
+
#### Bytes (Immutable - extension of Python bytes)
|
|
262
264
|
|
|
263
265
|
```python
|
|
264
266
|
from tsrkit_types.bytes import Bytes
|
|
@@ -266,47 +268,115 @@ from tsrkit_types.bytes import Bytes
|
|
|
266
268
|
# Creation
|
|
267
269
|
data = Bytes(b"Hello, binary world!")
|
|
268
270
|
data = Bytes([0x01, 0x02, 0x03, 0x04])
|
|
271
|
+
data = Bytes("48656c6c6f") # From hex string
|
|
269
272
|
|
|
270
|
-
#
|
|
271
|
-
data.to_bits()
|
|
272
|
-
Bytes.from_bits(
|
|
273
|
+
# Shared Operations (via BytesMixin)
|
|
274
|
+
bits = data.to_bits() # Convert to bit list [True, False, True, ...]
|
|
275
|
+
data2 = Bytes.from_bits(bits) # Create from bit list
|
|
273
276
|
|
|
274
|
-
#
|
|
277
|
+
# Properties
|
|
275
278
|
length = len(data) # Byte length
|
|
276
279
|
raw_bytes = bytes(data) # Convert to Python bytes
|
|
277
280
|
|
|
278
|
-
# Encoding
|
|
281
|
+
# Encoding/Decoding
|
|
279
282
|
encoded = data.encode() # [length][raw_bytes]
|
|
280
283
|
decoded = Bytes.decode(encoded)
|
|
281
284
|
|
|
282
285
|
# JSON serialization (hex encoded)
|
|
283
286
|
json_str = data.to_json() # "48656c6c6f2c2062696e61727920776f726c6421"
|
|
284
287
|
restored = Bytes.from_json(json_str)
|
|
288
|
+
restored2 = Bytes.from_json("0x48656c6c6f") # Supports 0x prefix
|
|
285
289
|
```
|
|
286
290
|
|
|
287
|
-
####
|
|
291
|
+
#### ByteArray (Mutable - extension of Python bytearray)
|
|
288
292
|
|
|
289
293
|
```python
|
|
290
|
-
from tsrkit_types.
|
|
294
|
+
from tsrkit_types.bytearray import ByteArray
|
|
291
295
|
|
|
292
296
|
# Creation
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
297
|
+
data = ByteArray(b"Hello, binary world!")
|
|
298
|
+
data = ByteArray([0x01, 0x02, 0x03, 0x04])
|
|
299
|
+
data = ByteArray("48656c6c6f") # From hex string
|
|
300
|
+
|
|
301
|
+
# Mutable Operations
|
|
302
|
+
data.append(0xFF) # Add single byte
|
|
303
|
+
data.extend([0xAB, 0xCD]) # Add multiple bytes
|
|
304
|
+
data.insert(0, 0x00) # Insert byte at position
|
|
305
|
+
data.pop() # Remove and return last byte
|
|
306
|
+
data.remove(0xFF) # Remove first occurrence
|
|
307
|
+
data.clear() # Remove all bytes
|
|
308
|
+
data.reverse() # Reverse in-place
|
|
309
|
+
|
|
310
|
+
# Indexing and Slicing (mutable)
|
|
311
|
+
data[0] = 0x42 # Set byte at index
|
|
312
|
+
data[1:3] = [0x43, 0x44] # Set slice
|
|
313
|
+
del data[0] # Delete byte at index
|
|
314
|
+
|
|
315
|
+
# Shared Operations (via BytesMixin) - same as Bytes
|
|
316
|
+
bits = data.to_bits() # Convert to bit list
|
|
317
|
+
data2 = ByteArray.from_bits(bits) # Create from bit list
|
|
318
|
+
|
|
319
|
+
# Properties and Conversion
|
|
320
|
+
length = len(data) # Byte length
|
|
321
|
+
raw_bytes = bytes(data) # Convert to immutable bytes
|
|
322
|
+
immutable = Bytes(data) # Convert to immutable Bytes
|
|
323
|
+
|
|
324
|
+
# Encoding/Decoding (same interface as Bytes)
|
|
325
|
+
encoded = data.encode() # [length][raw_bytes]
|
|
326
|
+
decoded = ByteArray.decode(encoded)
|
|
327
|
+
|
|
328
|
+
# JSON serialization (same interface as Bytes)
|
|
329
|
+
json_str = data.to_json() # "48656c6c6f..."
|
|
330
|
+
restored = ByteArray.from_json(json_str)
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
**Key Differences:**
|
|
334
|
+
- **Bytes**: Immutable, extends `bytes`, memory-efficient for read-only data
|
|
335
|
+
- **ByteArray**: Mutable, extends `bytearray`, suitable for dynamic byte manipulation
|
|
336
|
+
- **Shared Functionality**: Both support identical bit conversion, JSON serialization, and binary encoding through `BytesMixin`
|
|
337
|
+
|
|
338
|
+
**Common Features (Both Types):**
|
|
339
|
+
- Bit-level conversion with MSB/LSB support
|
|
340
|
+
- Hex string JSON serialization with 0x prefix support
|
|
341
|
+
- Efficient binary encoding with length prefix
|
|
342
|
+
- String representation and validation
|
|
343
|
+
- Memory-efficient operations
|
|
344
|
+
|
|
345
|
+
#### Bits (Bit Arrays - Sequence of bool)
|
|
346
|
+
|
|
347
|
+
```python
|
|
348
|
+
from tsrkit_types.bits import Bits
|
|
349
|
+
|
|
350
|
+
# Creation
|
|
351
|
+
bits = Bits([True, False, True, True, False])
|
|
352
|
+
bits = Bits.from_hex("1A3F") # From hex string
|
|
353
|
+
bits = Bits.from_int(42, 8) # From integer with bit width
|
|
354
|
+
|
|
355
|
+
# Fixed-size parameterized bits
|
|
356
|
+
FixedBits = Bits[8] # Exactly 8 bits
|
|
357
|
+
fixed = FixedBits([True, False, True, True, False, False, True, False])
|
|
296
358
|
|
|
297
359
|
# Operations
|
|
298
|
-
bit_val = bits[0]
|
|
299
|
-
bits[1] = True
|
|
300
|
-
bits.append(False)
|
|
301
|
-
bits.extend([True, False])
|
|
360
|
+
bit_val = bits[0] # Get bit at index
|
|
361
|
+
bits[1] = True # Set bit at index
|
|
362
|
+
bits.append(False) # Add bit
|
|
363
|
+
bits.extend([True, False]) # Add multiple bits
|
|
302
364
|
|
|
303
365
|
# Conversion
|
|
304
|
-
hex_str = bits.to_hex()
|
|
305
|
-
int_val = bits.to_int()
|
|
366
|
+
hex_str = bits.to_hex() # Convert to hex string
|
|
367
|
+
int_val = bits.to_int() # Convert to integer
|
|
368
|
+
|
|
369
|
+
# Bit order specification
|
|
370
|
+
bits_msb = Bits([True, False], bit_order="MSB") # Most significant bit first
|
|
371
|
+
bits_lsb = Bits([True, False], bit_order="LSB") # Least significant bit first
|
|
306
372
|
|
|
307
373
|
# Encoding
|
|
308
|
-
encoded = bits.encode()
|
|
309
|
-
decoded =
|
|
374
|
+
encoded = bits.encode() # [length][packed_bits]
|
|
375
|
+
decoded = Bits.decode(encoded)
|
|
376
|
+
|
|
377
|
+
# JSON serialization
|
|
378
|
+
json_str = bits.to_json() # Hex string representation
|
|
379
|
+
restored = Bits.from_json(json_str)
|
|
310
380
|
```
|
|
311
381
|
|
|
312
382
|
### Enum (Extension of Python Enum, with Codable + JSON support)
|
|
@@ -517,6 +587,16 @@ buffer = bytearray(1024)
|
|
|
517
587
|
offset = 0
|
|
518
588
|
offset += value1.encode_into(buffer, offset)
|
|
519
589
|
offset += value2.encode_into(buffer, offset)
|
|
590
|
+
|
|
591
|
+
# Choose appropriate byte type for your use case
|
|
592
|
+
# Use Bytes for immutable binary data (memory efficient, read-only)
|
|
593
|
+
config_data = Bytes(b"static configuration")
|
|
594
|
+
|
|
595
|
+
# Use ByteArray for dynamic binary buffers (mutable, growing data)
|
|
596
|
+
dynamic_buffer = ByteArray()
|
|
597
|
+
dynamic_buffer.extend([0x01, 0x02])
|
|
598
|
+
dynamic_buffer.append(0x03)
|
|
599
|
+
dynamic_buffer.insert(0, 0x00) # Result: [0x00, 0x01, 0x02, 0x03]
|
|
520
600
|
```
|
|
521
601
|
|
|
522
602
|
## Examples
|
|
@@ -693,6 +773,6 @@ Contributions are welcome! Please see [CONTRIBUTING.md](CONTRIBUTING.md) for gui
|
|
|
693
773
|
|
|
694
774
|
## Requirements
|
|
695
775
|
|
|
696
|
-
- **Python**: >= 3.
|
|
776
|
+
- **Python**: >= 3.11
|
|
697
777
|
- **Runtime Dependencies**: None (zero dependencies!)
|
|
698
778
|
- **Development Dependencies**: pytest and plugins (see `pyproject.toml`)
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "tsrkit-types"
|
|
3
|
-
version = "0.1.
|
|
3
|
+
version = "0.1.3"
|
|
4
4
|
description = "Performant Python Typings library for type-safe binary serialization, JSON encoding, and data validation with zero dependencies"
|
|
5
5
|
authors = [
|
|
6
6
|
{name = "chainscore-labs", email = "hello@chainscore.finance"},
|
|
@@ -8,7 +8,7 @@ authors = [
|
|
|
8
8
|
]
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "MIT"
|
|
11
|
-
requires-python = ">=3.
|
|
11
|
+
requires-python = ">=3.11"
|
|
12
12
|
keywords = [
|
|
13
13
|
"serialization", "binary", "encoding", "types", "codable", "json",
|
|
14
14
|
"validation", "data-types", "type-safety", "zero-copy", "protocol",
|
|
@@ -20,7 +20,6 @@ classifiers = [
|
|
|
20
20
|
"Intended Audience :: System Administrators",
|
|
21
21
|
"Operating System :: OS Independent",
|
|
22
22
|
"Programming Language :: Python :: 3",
|
|
23
|
-
"Programming Language :: Python :: 3.10",
|
|
24
23
|
"Programming Language :: Python :: 3.11",
|
|
25
24
|
"Programming Language :: Python :: 3.12",
|
|
26
25
|
"Programming Language :: Python :: 3 :: Only",
|