https://github.com/ousttrue/tupleannot
binary format experiment
https://github.com/ousttrue/tupleannot
Last synced: 11 months ago
JSON representation
binary format experiment
- Host: GitHub
- URL: https://github.com/ousttrue/tupleannot
- Owner: ousttrue
- License: mit
- Created: 2019-06-03T19:18:48.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-06T20:02:37.000Z (about 7 years ago)
- Last Synced: 2025-03-14T04:32:44.732Z (over 1 year ago)
- Language: Python
- Size: 33.2 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# tupleannot
binary data annotation for static typing
* Primitives
* Int8, 16, 32, 64
* UInt8, 16, 32, 64
* Float, Double
* Collection
* Typed Array
* array length
* fixed
* lazy(length is stored in previous value)
* element length
* fixed
* variable(contain lazy length array)
* Tuple
* String
## Usage
### type definition
```python
# type definition
class Vec3(TypedTuple):
x: FloatLE
y: FloatLE
z: FloatLE
# binary data
data = struct.pack('ffffffd', 1, 2, 3, 4, 5, 6, 7)
# parse
vec3, remain = Vec3[2].parse(data) # consume bytes
# parsed
self.assertEqual(1, vec3[0]['x'])
self.assertEqual(2, vec3[0]['y'])
self.assertEqual(3, vec3[0]['z'])
self.assertEqual(4, vec3[1]['x'])
self.assertEqual(5, vec3[1]['y'])
self.assertEqual(6, vec3[1]['z'])
```
### lazy array length
```python
class Val(TypedTuple):
length: UInt8
values: UInt32LE[-1] # determinate array length by relative value
data = struct.pack('