Skip to content

Commit a87b71b

Browse files
committed
New bitfield concept based in int.{from,to}_bytes
--- + BitField was renamed to Bitfield + Each Bitfield now stores a list of groups, which either represent a field or a list of entries + bitfield_example was updated
1 parent 1c27b4f commit a87b71b

8 files changed

Lines changed: 643 additions & 298 deletions

File tree

examples/bitfield_example.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# type: ignore
2+
from caterpillar.fields.common import uint8
23
from caterpillar.py import bitfield, char, int8, unpack, pack
34

45
try:
@@ -16,10 +17,10 @@ class Format:
1617
_1: 3 # unnamed padding to the rest of the byte
1718

1819

19-
print(Format.__struct__)
20+
print(Format.__struct__.groups)
2021
obj = unpack(Format, b"\x80\x80")
2122
print(obj)
22-
# prints: Format(b1=True, b2='2', b3=0, _1=0)
23+
# prints: Format(b1=1, b2='2', b3=0, _1=0)
2324
# real_pos: 0123456701234567
2425
# bit_pos: 7654321076543210
2526
# ---------------- # right to left

src/caterpillar/model/__init__.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,18 @@
2525
pack_file,
2626
sizeof,
2727
)
28-
from ._bitfield import BitField, bitfield, BitFieldGroup, issigned, getbits
28+
from ._bitfield import (
29+
Bitfield,
30+
bitfield,
31+
BitfieldEntry,
32+
BitfieldGroup,
33+
BitfieldValueFactory,
34+
issigned,
35+
getbits,
36+
NewGroup,
37+
EndGroup,
38+
SetAlignment,
39+
)
2940
from ._template import istemplate, template, TemplateTypeVar, derive
3041

3142
__all__ = [
@@ -40,13 +51,18 @@
4051
"pack_into",
4152
"pack_file",
4253
"sizeof",
43-
"BitField",
54+
"Bitfield",
4455
"bitfield",
45-
"BitFieldGroup",
56+
"BitfieldGroup",
4657
"issigned",
4758
"getbits",
4859
"istemplate",
4960
"template",
5061
"TemplateTypeVar",
5162
"derive",
63+
"NewGroup",
64+
"EndGroup",
65+
"SetAlignment",
66+
"BitfieldEntry",
67+
"BitfieldValueFactory",
5268
]

src/caterpillar/model/_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,7 @@ def pack_one(self, obj, context) -> None:
424424
if name in self._member_map_:
425425
value = self.get_value(obj, name, field)
426426
else:
427-
# REVISIT: this line might not be necessary if const fields alredy
427+
# REVISIT: this line might not be necessary if const fields already
428428
# use their internal value.
429429
value = field.default if field.default != INVALID_DEFAULT else None
430430
field.__pack__(value, context)

src/caterpillar/model/_base.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ class Sequence(FieldMixin, Generic[_SeqModelT], _StructLike[_SeqModelT, _SeqMode
3232
options: set[Flag]
3333
field_options: set[Flag]
3434
is_union: bool
35+
_member_map_: Dict[str, Field]
3536
def __init__(
3637
self,
3738
model: Optional[dict[str, Field]] = None,

0 commit comments

Comments
 (0)