|
1 | 1 | # Caterpillar - 🐛 |
2 | 2 |
|
3 | 3 | [](https://www.python.org/downloads/) |
4 | | -](https://img.shields.io/github/v/release/MatrixEditor/caterpillar.svg?logo=github) |
| 4 | +](https://img.shields.io/github/v/release/MatrixEditor/caterpillar.svg?logo=github&label=Latest+Version) |
5 | 5 | [](https://github.com/MatrixEditor/caterpillar/actions/workflows/python-sphinx.yml) |
6 | 6 | [](https://github.com/MatrixEditor/caterpillar/actions/workflows/python-test.yml) |
7 | 7 |  |
@@ -40,35 +40,42 @@ options will be added in the future. Documentation is [here >](https://matrixedi |
40 | 40 | ## Give me some code! |
41 | 41 |
|
42 | 42 | ```python |
43 | | -from caterpillar.py import * |
| 43 | +@bitfield(order=LittleEndian) |
| 44 | +class Header: |
| 45 | + version : 4 # 4bit integer |
| 46 | + valid : 1 # 1bit flag (boolean) |
| 47 | + ident : (8, CharFactory) # 8bit char |
| 48 | + # automatic alignment to 16bits |
44 | 49 |
|
45 | 50 | @struct(order=LittleEndian) |
46 | 51 | class Format: |
47 | | - magic: b"ITS MAGIC" # Supports string and byte constants directly |
48 | | - a: uint8 # Primitive data types |
49 | | - b: int32 |
50 | | - length: uint8 # String fields with computed lengths |
51 | | - name: String(this.length) # -> you can also use Prefixed(uint8) |
| 52 | + magic : b"ITS MAGIC" # Supports string and byte constants directly |
| 53 | + header : Header |
| 54 | + a : uint8 # Primitive data types |
| 55 | + b : int32 |
| 56 | + length : uint8 # String fields with computed lengths |
| 57 | + name : String(this.length) # -> you can also use Prefixed(uint8) |
52 | 58 |
|
53 | | - # custom actions, e.g. for hashes |
54 | | - _hash_begin: DigestField.begin("hash", Md5_Algo) |
55 | | - |
56 | | - # Sequences with prefixed, computed lengths |
57 | | - names: CString[uint8::] |
58 | | - |
59 | | - # automatic hash creation and verification + default value |
60 | | - hash: Md5_Field("hash", verify=True) = None |
| 59 | + _hash_begin : DigestField.begin("hash", Md5_Algo) # custom actions, e.g. for hashes |
| 60 | + names : CString[uint8::] # Sequences with prefixed, computed lengths |
| 61 | + hash : Md5_Field("hash", verify=True) = None # automatic hash creation and verification + default value |
61 | 62 |
|
62 | 63 | # Instantiation (keyword-only arguments, magic is auto-inferred): |
63 | | -obj = Format(a=1, b=2, length=3, name="foo", names=["a", "b"]) |
| 64 | +obj = Format( |
| 65 | + header=Header(version=2, valid=True, ident="F"), |
| 66 | + a=1, |
| 67 | + b=2, |
| 68 | + length=3, |
| 69 | + name="foo", |
| 70 | + names=["a", "b"] |
| 71 | +) |
64 | 72 | # Packing the object, reads as 'PACK obj FROM Format' |
65 | 73 | # objects of struct classes can be packed right away |
66 | 74 | blob = pack(obj, Format) |
67 | | -# results in: b'ITS MAGIC\x01\x02\x00\x00\x00\x03foo\x02a\x00b\x00\xf55... |
| 75 | +# results in: b'ITS MAGIC0*\x01\x02\x00\x00\x00\x03foo\x02a\x00b\x00)\x9a...' |
68 | 76 |
|
69 | 77 | # Unpacking the binary data, reads as 'UNPACK Format FROM blob' |
70 | 78 | obj2 = unpack(Format, blob) |
71 | | -assert obj2.hash == obj.hash |
72 | 79 | ``` |
73 | 80 |
|
74 | 81 | This library offers extensive functionality beyond basic struct handling. For further details |
|
0 commit comments