Debian Codegraph @

src/bitstruct.egg-info/PKG-INFO

raw
1 Metadata-Version: 2.4
2 Name: bitstruct
3 Version: 8.23.0
4 Summary: This module performs conversions between Python values and C bit field structs represented as Python byte strings.
5 Author: Ilya Petukhov
6 Author-email: Erik Moqvist <[email protected]>
7 License-Expression: MIT
8 Project-URL: Documentation, https://bitstruct.readthedocs.io/en/latest/
9 Project-URL: Issues, https://github.com/eerimoq/bitstruct/issues
10 Project-URL: Source, https://github.com/eerimoq/bitstruct
11 Project-URL: Homepage, https://github.com/eerimoq/bitstruct
12 Keywords: bit field,bit parsing,bit unpack,bit pack
13 Classifier: Development Status :: 5 - Production/Stable
14 Classifier: Programming Language :: Python
15 Classifier: Programming Language :: Python :: 3
16 Classifier: Programming Language :: Python :: Implementation :: CPython
17 Requires-Python: >=3.9
18 Description-Content-Type: text/x-rst
19 License-File: LICENSE
20 Dynamic: license-file
21
22 About
23 =====
24
25 This module is intended to have a similar interface as the python
26 struct module, but working on bits instead of primitive data types
27 (char, int, ...).
28
29 Project homepage: https://github.com/eerimoq/bitstruct
30
31 Documentation: https://bitstruct.readthedocs.io
32
33 Installation
34 ============
35
36 .. code-block:: python
37
38 pip install bitstruct
39
40 Performance
41 ===========
42
43 Parts of this package has been re-implemented in C for faster pack and
44 unpack operations. There are two independent C implementations;
45 `bitstruct.c`, which is part of this package, and the standalone
46 package `cbitstruct`_. These implementations are only available in
47 CPython 3, and must be explicitly imported. By default the pure Python
48 implementation is used.
49
50 To use `bitstruct.c`, do ``import bitstruct.c as bitstruct``.
51
52 To use `cbitstruct`_, do ``import cbitstruct as bitstruct``.
53
54 `bitstruct.c` has a few limitations compared to the pure Python
55 implementation:
56
57 - Integers and booleans must be 64 bits or less.
58
59 - Text and raw must be a multiple of 8 bits.
60
61 - Bit endianness and byte order are not yet supported.
62
63 - ``byteswap()`` can only swap 1, 2, 4 and 8 bytes.
64
65 See `cbitstruct`_ for its limitations.
66
67 MicroPython
68 ===========
69
70 The C implementation has been ported to `MicroPython`_. See
71 `bitstruct-micropython`_ for more details.
72
73 Example usage
74 =============
75
76 A basic example of `packing`_ and `unpacking`_ four integers using the
77 format string ``'u1u3u4s16'``:
78
79 .. code-block:: python
80
81 >>> from bitstruct import *
82 >>> pack('u1u3u4s16', 1, 2, 3, -4)
83 b'\xa3\xff\xfc'
84 >>> unpack('u1u3u4s16', b'\xa3\xff\xfc')
85 (1, 2, 3, -4)
86 >>> calcsize('u1u3u4s16')
87 24
88
89 An example `compiling`_ the format string once, and use it to `pack`_
90 and `unpack`_ data:
91
92 .. code-block:: python
93
94 >>> import bitstruct
95 >>> cf = bitstruct.compile('u1u3u4s16')
96 >>> cf.pack(1, 2, 3, -4)
97 b'\xa3\xff\xfc'
98 >>> cf.unpack(b'\xa3\xff\xfc')
99 (1, 2, 3, -4)
100
101 Use the `pack into`_ and `unpack from`_ functions to pack/unpack
102 values at a bit offset into the data, in this example the bit offset
103 is 5:
104
105 .. code-block:: python
106
107 >>> from bitstruct import *
108 >>> data = bytearray(b'\x00\x00\x00\x00')
109 >>> pack_into('u1u3u4s16', data, 5, 1, 2, 3, -4)
110 >>> data
111 bytearray(b'\x05\x1f\xff\xe0')
112 >>> unpack_from('u1u3u4s16', data, 5)
113 (1, 2, 3, -4)
114
115 The unpacked values can be named by assigning them to variables or by
116 wrapping the result in a named tuple:
117
118 .. code-block:: python
119
120 >>> from bitstruct import *
121 >>> from collections import namedtuple
122 >>> MyName = namedtuple('myname', ['a', 'b', 'c', 'd'])
123 >>> unpacked = unpack('u1u3u4s16', b'\xa3\xff\xfc')
124 >>> myname = MyName(*unpacked)
125 >>> myname
126 myname(a=1, b=2, c=3, d=-4)
127 >>> myname.c
128 3
129
130 Use the `pack_dict`_ and `unpack_dict`_ functions to pack/unpack
131 values in dictionaries:
132
133 .. code-block:: python
134
135 >>> from bitstruct import *
136 >>> names = ['a', 'b', 'c', 'd']
137 >>> pack_dict('u1u3u4s16', names, {'a': 1, 'b': 2, 'c': 3, 'd': -4})
138 b'\xa3\xff\xfc'
139 >>> unpack_dict('u1u3u4s16', names, b'\xa3\xff\xfc')
140 {'a': 1, 'b': 2, 'c': 3, 'd': -4}
141
142 An example of `packing`_ and `unpacking`_ an unsigned integer, a
143 signed integer, a float, a boolean, a byte string and a string:
144
145 .. code-block:: python
146
147 >>> from bitstruct import *
148 >>> pack('u5s5f32b1r13t40', 1, -1, 3.75, True, b'\xff\xff', 'hello')
149 b'\x0f\xd0\x1c\x00\x00?\xffhello'
150 >>> unpack('u5s5f32b1r13t40', b'\x0f\xd0\x1c\x00\x00?\xffhello')
151 (1, -1, 3.75, True, b'\xff\xf8', 'hello')
152 >>> calcsize('u5s5f32b1r13t40')
153 96
154
155 The same format string and values as in the previous example, but
156 using LSB (Least Significant Bit) first instead of the default MSB
157 (Most Significant Bit) first:
158
159 .. code-block:: python
160
161 >>> from bitstruct import *
162 >>> pack('<u5s5f32b1r13t40', 1, -1, 3.75, True, b'\xff\xff', 'hello')
163 b'\x87\xc0\x00\x03\x80\xbf\xff\xf666\xa6\x16'
164 >>> unpack('<u5s5f32b1r13t40', b'\x87\xc0\x00\x03\x80\xbf\xff\xf666\xa6\x16')
165 (1, -1, 3.75, True, b'\xff\xf8', 'hello')
166 >>> calcsize('<u5s5f32b1r13t40')
167 96
168
169 An example of `unpacking`_ values from a hexstring and a binary file:
170
171 .. code-block:: python
172
173 >>> from bitstruct import *
174 >>> from binascii import unhexlify
175 >>> unpack('s17s13r24', unhexlify('0123456789abcdef'))
176 (582, -3751, b'\xe2j\xf3')
177 >>> with open("test.bin", "rb") as fin:
178 ... unpack('s17s13r24', fin.read(8))
179 ...
180 ...
181 (582, -3751, b'\xe2j\xf3')
182
183 Change endianness of the data with `byteswap`_, and then unpack the
184 values:
185
186 .. code-block:: python
187
188 >>> from bitstruct import *
189 >>> packed = pack('u1u3u4s16', 1, 2, 3, 1)
190 >>> unpack('u1u3u4s16', byteswap('12', packed))
191 (1, 2, 3, 256)
192
193 A basic example of `packing`_ and `unpacking`_ four integers using the
194 format string ``'u1u3u4s16'`` using the C implementation:
195
196 .. code-block:: python
197
198 >>> from bitstruct.c import *
199 >>> pack('u1u3u4s16', 1, 2, 3, -4)
200 b'\xa3\xff\xfc'
201 >>> unpack('u1u3u4s16', b'\xa3\xff\xfc')
202 (1, 2, 3, -4)
203
204 Contributing
205 ============
206
207 #. Fork the repository.
208
209 #. Install prerequisites.
210
211 .. code-block:: text
212
213 pip install -r requirements.txt
214
215 #. Implement the new feature or bug fix.
216
217 #. Implement test case(s) to ensure that future changes do not break
218 legacy.
219
220 #. Run the tests.
221
222 .. code-block:: text
223
224 make test
225
226 #. Create a pull request.
227
228 .. _packing: http://bitstruct.readthedocs.io/en/latest/#bitstruct.pack
229
230 .. _unpacking: http://bitstruct.readthedocs.io/en/latest/#bitstruct.unpack
231
232 .. _pack: http://bitstruct.readthedocs.io/en/latest/#bitstruct.CompiledFormat.pack
233
234 .. _unpack: http://bitstruct.readthedocs.io/en/latest/#bitstruct.CompiledFormat.unpack
235
236 .. _pack into: http://bitstruct.readthedocs.io/en/latest/#bitstruct.pack_into
237
238 .. _unpack from: http://bitstruct.readthedocs.io/en/latest/#bitstruct.unpack_from
239
240 .. _pack_dict: http://bitstruct.readthedocs.io/en/latest/#bitstruct.pack_dict
241
242 .. _unpack_dict: http://bitstruct.readthedocs.io/en/latest/#bitstruct.unpack_dict
243
244 .. _byteswap: http://bitstruct.readthedocs.io/en/latest/#bitstruct.byteswap
245
246 .. _compiling: http://bitstruct.readthedocs.io/en/latest/#bitstruct.compile
247
248 .. _cbitstruct: https://github.com/qchateau/cbitstruct
249
250 .. _MicroPython: https://github.com/micropython/micropython
251
252 .. _bitstruct-micropython: https://github.com/peterzuger/bitstruct-micropython