firstclass 0.0.1__tar.gz → 0.0.2__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,104 +1,104 @@
1
- Metadata-Version: 2.4
2
- Name: firstclass
3
- Version: 0.0.1
4
- Summary: dataclass with bytes, ctypes, C struct, JSON and XML serialization
5
- Requires-Python: >=3.10
6
- Description-Content-Type: text/markdown
7
-
8
- # firstclass
9
-
10
- `@firstclass` works like `@dataclass` (same keyword args) and adds conversion to and from
11
- packed bytes, `ctypes`, a C struct header, JSON and XML. Pure Python 3.10+, no dependencies.
12
-
13
- ```python
14
- from firstclass import firstclass, uint8_t, uint16_t, int32_t, float32_t, bool_t, char_array, bytes_t, array_t
15
-
16
- @firstclass
17
- class Point:
18
- x: int32_t
19
- y: int32_t
20
-
21
- @firstclass
22
- class Packet:
23
- kind: uint8_t
24
- ok: bool_t
25
- temp: float32_t
26
- name: char_array[8] # NUL-padded UTF-8
27
- mac: bytes_t[6] # exactly 6 bytes
28
- samples: array_t[uint16_t, 4]
29
- origin: Point # nested
30
-
31
- p = Packet(1, True, 21.5, "sensor", b"\x00\x11\x22\x33\x44\x55", [1, 2, 3, 4], Point(10, -20))
32
-
33
- raw = p.to_bytes() # packed, little-endian
34
- assert Packet.from_bytes(raw) == p
35
- Packet.sizeof() # 36
36
- ```
37
-
38
- ## Types
39
-
40
- | Marker | C | Bytes |
41
- |---|---|---|
42
- | `uint8_t` `uint16_t` `uint32_t` `uint64_t` | `uintN_t` | 1/2/4/8 |
43
- | `int8_t` `int16_t` `int32_t` `int64_t` | `intN_t` | 1/2/4/8 |
44
- | `float32_t` / `float64_t` | `float` / `double` | 4 / 8 |
45
- | `bool_t` | `bool` | 1 |
46
- | `char_t` | `char` | 1 |
47
- | `char_array[N]` | `char x[N]` | N (UTF-8, NUL-padded) |
48
- | `bytes_t[N]` | `uint8_t x[N]` | N |
49
- | `array_t[T, N]` | `T x[N]` | N * size(T) |
50
- | another `@firstclass` class | `struct Name` | its size |
51
-
52
- Values are checked: out of range ints, over-long strings and wrong lengths raise
53
- `ValueError`; wrong Python types raise `TypeError`. Nothing is truncated or coerced silently.
54
-
55
- Raw `ctypes` types work as annotations too (packed, same layout as a `_pack_ = 1` Structure):
56
- `ctypes.c_int`, `c_uint64`, `c_size_t`, `c_bool`, `c_float`, `c_double`, `c_char`, `c_uint16 * 4`,
57
- `c_char * 8`. Pointers (`c_void_p`, `POINTER(T)`, `c_char_p`) are stored as the address (an `int`,
58
- 0 = NULL) at pointer size; `to_ctypes()` gives a real pointer field you can dereference. The
59
- address only means something inside the same process. Sizes follow the platform
60
- (`c_long` is 4 bytes on Windows, 8 on Linux).
61
-
62
- ## Methods added to every class
63
-
64
- | Method | |
65
- |---|---|
66
- | `obj.to_bytes()` / `Cls.from_bytes(data)` | binary; `from_bytes` needs exactly `sizeof()` bytes |
67
- | `Cls.sizeof()` | size in bytes |
68
- | `obj.to_dict()` / `Cls.from_dict(d)` | JSON-compatible dict (bytes as hex) |
69
- | `obj.to_json(**kw)` / `Cls.from_json(s)` | `kw` goes to `json.dumps` |
70
- | `obj.to_xml(indent=None)` / `Cls.from_xml(s)` | `<Packet><kind>1</kind>...</Packet>`, arrays as `<item>` |
71
- | `obj.to_ctypes()` / `Cls.from_ctypes(c)` / `Cls.ctypes_type()` | `ctypes.Structure` with matching layout |
72
- | `Cls.c_header()` | C source for the struct(s), nested ones first |
73
-
74
- ## Layout options
75
-
76
- ```python
77
- @firstclass(byteorder="big") # default "little"
78
- @firstclass(align=True) # default False = packed, no padding (like __attribute__((packed)))
79
- @firstclass(frozen=True, slots=True) # any dataclass option
80
- ```
81
-
82
- `align=True` inserts natural-alignment padding like a normal C struct (assumes the x86-64 style
83
- rule: alignment equals size). Padding bytes are written as zero and ignored on read.
84
- C headers describe the struct only; C compilers use native byte order, so `byteorder` is noted
85
- in a comment.
86
-
87
- ## Notes
88
-
89
- - Fields with plain types (`str`, `int`) work for JSON/XML only; `to_bytes()` raises
90
- `TypeError` naming the field.
91
- - Use `from __future__ import annotations` freely; annotations are resolved lazily. Classes
92
- defined inside functions that refer to local names are not resolvable.
93
- - Don't parse untrusted XML with `from_xml` without extra care (stdlib `xml.etree`).
94
- - Field names that collide with the generated methods (`to_bytes`, ...) raise `TypeError`.
95
-
96
- ## Example
97
-
98
- `python examples/example.py` shows bytes, JSON, XML, ctypes and the C header from one class.
99
-
100
- ## Develop
101
-
102
- ```
103
- python -m unittest discover -s tests
104
- ```
1
+ Metadata-Version: 2.4
2
+ Name: firstclass
3
+ Version: 0.0.2
4
+ Summary: dataclass with bytes, ctypes, C struct, JSON and XML serialization
5
+ Requires-Python: >=3.10
6
+ Description-Content-Type: text/markdown
7
+
8
+ # firstclass
9
+
10
+ `@firstclass` works like `@dataclass` (same keyword args) and adds conversion to and from
11
+ packed bytes, `ctypes`, a C struct header, JSON and XML. Pure Python 3.10+, no dependencies.
12
+
13
+ ```python
14
+ from firstclass import firstclass, uint8_t, uint16_t, int32_t, float32_t, bool_t, char_array, bytes_t, array_t
15
+
16
+ @firstclass
17
+ class Point:
18
+ x: int32_t
19
+ y: int32_t
20
+
21
+ @firstclass
22
+ class Packet:
23
+ kind: uint8_t
24
+ ok: bool_t
25
+ temp: float32_t
26
+ name: char_array[8] # NUL-padded UTF-8
27
+ mac: bytes_t[6] # exactly 6 bytes
28
+ samples: array_t[uint16_t, 4]
29
+ origin: Point # nested
30
+
31
+ p = Packet(1, True, 21.5, "sensor", b"\x00\x11\x22\x33\x44\x55", [1, 2, 3, 4], Point(10, -20))
32
+
33
+ raw = p.to_bytes() # packed, little-endian
34
+ assert Packet.from_bytes(raw) == p
35
+ Packet.sizeof() # 36
36
+ ```
37
+
38
+ ## Types
39
+
40
+ | Marker | C | Bytes |
41
+ |---|---|---|
42
+ | `uint8_t` `uint16_t` `uint32_t` `uint64_t` | `uintN_t` | 1/2/4/8 |
43
+ | `int8_t` `int16_t` `int32_t` `int64_t` | `intN_t` | 1/2/4/8 |
44
+ | `float32_t` / `float64_t` | `float` / `double` | 4 / 8 |
45
+ | `bool_t` | `bool` | 1 |
46
+ | `char_t` | `char` | 1 |
47
+ | `char_array[N]` | `char x[N]` | N (UTF-8, NUL-padded) |
48
+ | `bytes_t[N]` | `uint8_t x[N]` | N |
49
+ | `array_t[T, N]` | `T x[N]` | N * size(T) |
50
+ | another `@firstclass` class | `struct Name` | its size |
51
+
52
+ Values are checked: out of range ints, over-long strings and wrong lengths raise
53
+ `ValueError`; wrong Python types raise `TypeError`. Nothing is truncated or coerced silently.
54
+
55
+ Raw `ctypes` types work as annotations too (packed, same layout as a `_pack_ = 1` Structure):
56
+ `ctypes.c_int`, `c_uint64`, `c_size_t`, `c_bool`, `c_float`, `c_double`, `c_char`, `c_uint16 * 4`,
57
+ `c_char * 8`. Pointers (`c_void_p`, `POINTER(T)`, `c_char_p`) are stored as the address (an `int`,
58
+ 0 = NULL) at pointer size; `to_ctypes()` gives a real pointer field you can dereference. The
59
+ address only means something inside the same process. Sizes follow the platform
60
+ (`c_long` is 4 bytes on Windows, 8 on Linux).
61
+
62
+ ## Methods added to every class
63
+
64
+ | Method | |
65
+ |---|---|
66
+ | `obj.to_bytes()` / `Cls.from_bytes(data)` | binary; `from_bytes` needs exactly `sizeof()` bytes |
67
+ | `Cls.sizeof()` | size in bytes |
68
+ | `obj.to_dict()` / `Cls.from_dict(d)` | JSON-compatible dict (bytes as hex) |
69
+ | `obj.to_json(**kw)` / `Cls.from_json(s)` | `kw` goes to `json.dumps` |
70
+ | `obj.to_xml(indent=None)` / `Cls.from_xml(s)` | `<Packet><kind>1</kind>...</Packet>`, arrays as `<item>` |
71
+ | `obj.to_ctypes()` / `Cls.from_ctypes(c)` / `Cls.ctypes_type()` | `ctypes.Structure` with matching layout |
72
+ | `Cls.c_header()` | C source for the struct(s), nested ones first |
73
+
74
+ ## Layout options
75
+
76
+ ```python
77
+ @firstclass(byteorder="big") # default "little"
78
+ @firstclass(align=True) # default False = packed, no padding (like __attribute__((packed)))
79
+ @firstclass(frozen=True, slots=True) # any dataclass option
80
+ ```
81
+
82
+ `align=True` inserts natural-alignment padding like a normal C struct (assumes the x86-64 style
83
+ rule: alignment equals size). Padding bytes are written as zero and ignored on read.
84
+ C headers describe the struct only; C compilers use native byte order, so `byteorder` is noted
85
+ in a comment.
86
+
87
+ ## Notes
88
+
89
+ - Fields with plain types (`str`, `int`) work for JSON/XML only; `to_bytes()` raises
90
+ `TypeError` naming the field.
91
+ - Use `from __future__ import annotations` freely; annotations are resolved lazily. Classes
92
+ defined inside functions that refer to local names are not resolvable.
93
+ - Don't parse untrusted XML with `from_xml` without extra care (stdlib `xml.etree`).
94
+ - Field names that collide with the generated methods (`to_bytes`, ...) raise `TypeError`.
95
+
96
+ ## Example
97
+
98
+ `python examples/example.py` shows bytes, JSON, XML, ctypes and the C header from one class.
99
+
100
+ ## Develop
101
+
102
+ ```
103
+ python -m unittest discover -s tests
104
+ ```
@@ -1,97 +1,97 @@
1
- # firstclass
2
-
3
- `@firstclass` works like `@dataclass` (same keyword args) and adds conversion to and from
4
- packed bytes, `ctypes`, a C struct header, JSON and XML. Pure Python 3.10+, no dependencies.
5
-
6
- ```python
7
- from firstclass import firstclass, uint8_t, uint16_t, int32_t, float32_t, bool_t, char_array, bytes_t, array_t
8
-
9
- @firstclass
10
- class Point:
11
- x: int32_t
12
- y: int32_t
13
-
14
- @firstclass
15
- class Packet:
16
- kind: uint8_t
17
- ok: bool_t
18
- temp: float32_t
19
- name: char_array[8] # NUL-padded UTF-8
20
- mac: bytes_t[6] # exactly 6 bytes
21
- samples: array_t[uint16_t, 4]
22
- origin: Point # nested
23
-
24
- p = Packet(1, True, 21.5, "sensor", b"\x00\x11\x22\x33\x44\x55", [1, 2, 3, 4], Point(10, -20))
25
-
26
- raw = p.to_bytes() # packed, little-endian
27
- assert Packet.from_bytes(raw) == p
28
- Packet.sizeof() # 36
29
- ```
30
-
31
- ## Types
32
-
33
- | Marker | C | Bytes |
34
- |---|---|---|
35
- | `uint8_t` `uint16_t` `uint32_t` `uint64_t` | `uintN_t` | 1/2/4/8 |
36
- | `int8_t` `int16_t` `int32_t` `int64_t` | `intN_t` | 1/2/4/8 |
37
- | `float32_t` / `float64_t` | `float` / `double` | 4 / 8 |
38
- | `bool_t` | `bool` | 1 |
39
- | `char_t` | `char` | 1 |
40
- | `char_array[N]` | `char x[N]` | N (UTF-8, NUL-padded) |
41
- | `bytes_t[N]` | `uint8_t x[N]` | N |
42
- | `array_t[T, N]` | `T x[N]` | N * size(T) |
43
- | another `@firstclass` class | `struct Name` | its size |
44
-
45
- Values are checked: out of range ints, over-long strings and wrong lengths raise
46
- `ValueError`; wrong Python types raise `TypeError`. Nothing is truncated or coerced silently.
47
-
48
- Raw `ctypes` types work as annotations too (packed, same layout as a `_pack_ = 1` Structure):
49
- `ctypes.c_int`, `c_uint64`, `c_size_t`, `c_bool`, `c_float`, `c_double`, `c_char`, `c_uint16 * 4`,
50
- `c_char * 8`. Pointers (`c_void_p`, `POINTER(T)`, `c_char_p`) are stored as the address (an `int`,
51
- 0 = NULL) at pointer size; `to_ctypes()` gives a real pointer field you can dereference. The
52
- address only means something inside the same process. Sizes follow the platform
53
- (`c_long` is 4 bytes on Windows, 8 on Linux).
54
-
55
- ## Methods added to every class
56
-
57
- | Method | |
58
- |---|---|
59
- | `obj.to_bytes()` / `Cls.from_bytes(data)` | binary; `from_bytes` needs exactly `sizeof()` bytes |
60
- | `Cls.sizeof()` | size in bytes |
61
- | `obj.to_dict()` / `Cls.from_dict(d)` | JSON-compatible dict (bytes as hex) |
62
- | `obj.to_json(**kw)` / `Cls.from_json(s)` | `kw` goes to `json.dumps` |
63
- | `obj.to_xml(indent=None)` / `Cls.from_xml(s)` | `<Packet><kind>1</kind>...</Packet>`, arrays as `<item>` |
64
- | `obj.to_ctypes()` / `Cls.from_ctypes(c)` / `Cls.ctypes_type()` | `ctypes.Structure` with matching layout |
65
- | `Cls.c_header()` | C source for the struct(s), nested ones first |
66
-
67
- ## Layout options
68
-
69
- ```python
70
- @firstclass(byteorder="big") # default "little"
71
- @firstclass(align=True) # default False = packed, no padding (like __attribute__((packed)))
72
- @firstclass(frozen=True, slots=True) # any dataclass option
73
- ```
74
-
75
- `align=True` inserts natural-alignment padding like a normal C struct (assumes the x86-64 style
76
- rule: alignment equals size). Padding bytes are written as zero and ignored on read.
77
- C headers describe the struct only; C compilers use native byte order, so `byteorder` is noted
78
- in a comment.
79
-
80
- ## Notes
81
-
82
- - Fields with plain types (`str`, `int`) work for JSON/XML only; `to_bytes()` raises
83
- `TypeError` naming the field.
84
- - Use `from __future__ import annotations` freely; annotations are resolved lazily. Classes
85
- defined inside functions that refer to local names are not resolvable.
86
- - Don't parse untrusted XML with `from_xml` without extra care (stdlib `xml.etree`).
87
- - Field names that collide with the generated methods (`to_bytes`, ...) raise `TypeError`.
88
-
89
- ## Example
90
-
91
- `python examples/example.py` shows bytes, JSON, XML, ctypes and the C header from one class.
92
-
93
- ## Develop
94
-
95
- ```
96
- python -m unittest discover -s tests
97
- ```
1
+ # firstclass
2
+
3
+ `@firstclass` works like `@dataclass` (same keyword args) and adds conversion to and from
4
+ packed bytes, `ctypes`, a C struct header, JSON and XML. Pure Python 3.10+, no dependencies.
5
+
6
+ ```python
7
+ from firstclass import firstclass, uint8_t, uint16_t, int32_t, float32_t, bool_t, char_array, bytes_t, array_t
8
+
9
+ @firstclass
10
+ class Point:
11
+ x: int32_t
12
+ y: int32_t
13
+
14
+ @firstclass
15
+ class Packet:
16
+ kind: uint8_t
17
+ ok: bool_t
18
+ temp: float32_t
19
+ name: char_array[8] # NUL-padded UTF-8
20
+ mac: bytes_t[6] # exactly 6 bytes
21
+ samples: array_t[uint16_t, 4]
22
+ origin: Point # nested
23
+
24
+ p = Packet(1, True, 21.5, "sensor", b"\x00\x11\x22\x33\x44\x55", [1, 2, 3, 4], Point(10, -20))
25
+
26
+ raw = p.to_bytes() # packed, little-endian
27
+ assert Packet.from_bytes(raw) == p
28
+ Packet.sizeof() # 36
29
+ ```
30
+
31
+ ## Types
32
+
33
+ | Marker | C | Bytes |
34
+ |---|---|---|
35
+ | `uint8_t` `uint16_t` `uint32_t` `uint64_t` | `uintN_t` | 1/2/4/8 |
36
+ | `int8_t` `int16_t` `int32_t` `int64_t` | `intN_t` | 1/2/4/8 |
37
+ | `float32_t` / `float64_t` | `float` / `double` | 4 / 8 |
38
+ | `bool_t` | `bool` | 1 |
39
+ | `char_t` | `char` | 1 |
40
+ | `char_array[N]` | `char x[N]` | N (UTF-8, NUL-padded) |
41
+ | `bytes_t[N]` | `uint8_t x[N]` | N |
42
+ | `array_t[T, N]` | `T x[N]` | N * size(T) |
43
+ | another `@firstclass` class | `struct Name` | its size |
44
+
45
+ Values are checked: out of range ints, over-long strings and wrong lengths raise
46
+ `ValueError`; wrong Python types raise `TypeError`. Nothing is truncated or coerced silently.
47
+
48
+ Raw `ctypes` types work as annotations too (packed, same layout as a `_pack_ = 1` Structure):
49
+ `ctypes.c_int`, `c_uint64`, `c_size_t`, `c_bool`, `c_float`, `c_double`, `c_char`, `c_uint16 * 4`,
50
+ `c_char * 8`. Pointers (`c_void_p`, `POINTER(T)`, `c_char_p`) are stored as the address (an `int`,
51
+ 0 = NULL) at pointer size; `to_ctypes()` gives a real pointer field you can dereference. The
52
+ address only means something inside the same process. Sizes follow the platform
53
+ (`c_long` is 4 bytes on Windows, 8 on Linux).
54
+
55
+ ## Methods added to every class
56
+
57
+ | Method | |
58
+ |---|---|
59
+ | `obj.to_bytes()` / `Cls.from_bytes(data)` | binary; `from_bytes` needs exactly `sizeof()` bytes |
60
+ | `Cls.sizeof()` | size in bytes |
61
+ | `obj.to_dict()` / `Cls.from_dict(d)` | JSON-compatible dict (bytes as hex) |
62
+ | `obj.to_json(**kw)` / `Cls.from_json(s)` | `kw` goes to `json.dumps` |
63
+ | `obj.to_xml(indent=None)` / `Cls.from_xml(s)` | `<Packet><kind>1</kind>...</Packet>`, arrays as `<item>` |
64
+ | `obj.to_ctypes()` / `Cls.from_ctypes(c)` / `Cls.ctypes_type()` | `ctypes.Structure` with matching layout |
65
+ | `Cls.c_header()` | C source for the struct(s), nested ones first |
66
+
67
+ ## Layout options
68
+
69
+ ```python
70
+ @firstclass(byteorder="big") # default "little"
71
+ @firstclass(align=True) # default False = packed, no padding (like __attribute__((packed)))
72
+ @firstclass(frozen=True, slots=True) # any dataclass option
73
+ ```
74
+
75
+ `align=True` inserts natural-alignment padding like a normal C struct (assumes the x86-64 style
76
+ rule: alignment equals size). Padding bytes are written as zero and ignored on read.
77
+ C headers describe the struct only; C compilers use native byte order, so `byteorder` is noted
78
+ in a comment.
79
+
80
+ ## Notes
81
+
82
+ - Fields with plain types (`str`, `int`) work for JSON/XML only; `to_bytes()` raises
83
+ `TypeError` naming the field.
84
+ - Use `from __future__ import annotations` freely; annotations are resolved lazily. Classes
85
+ defined inside functions that refer to local names are not resolvable.
86
+ - Don't parse untrusted XML with `from_xml` without extra care (stdlib `xml.etree`).
87
+ - Field names that collide with the generated methods (`to_bytes`, ...) raise `TypeError`.
88
+
89
+ ## Example
90
+
91
+ `python examples/example.py` shows bytes, JSON, XML, ctypes and the C header from one class.
92
+
93
+ ## Develop
94
+
95
+ ```
96
+ python -m unittest discover -s tests
97
+ ```