bitstring 4.4.0__tar.gz → 5.0.0__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.
- bitstring-5.0.0/PKG-INFO +165 -0
- bitstring-5.0.0/README.md +137 -0
- bitstring-5.0.0/bitstring/__init__.py +337 -0
- bitstring-5.0.0/bitstring/array_.py +1075 -0
- {bitstring-4.4.0 → bitstring-5.0.0}/bitstring/bitarray_.py +267 -164
- bitstring-5.0.0/bitstring/bits.py +2069 -0
- bitstring-5.0.0/bitstring/bitstore.py +573 -0
- bitstring-5.0.0/bitstring/bitstore_helpers.py +256 -0
- bitstring-5.0.0/bitstring/colour.py +22 -0
- {bitstring-4.4.0 → bitstring-5.0.0}/bitstring/dtypes.py +161 -140
- bitstring-5.0.0/bitstring/exceptions.py +2 -0
- bitstring-5.0.0/bitstring/helpers.py +24 -0
- bitstring-5.0.0/bitstring/methods.py +147 -0
- bitstring-5.0.0/bitstring/reader.py +472 -0
- {bitstring-4.4.0 → bitstring-5.0.0}/bitstring/utils.py +61 -50
- bitstring-5.0.0/bitstring.egg-info/PKG-INFO +165 -0
- {bitstring-4.4.0 → bitstring-5.0.0}/bitstring.egg-info/SOURCES.txt +9 -14
- bitstring-5.0.0/bitstring.egg-info/requires.txt +1 -0
- {bitstring-4.4.0 → bitstring-5.0.0}/pyproject.toml +21 -9
- {bitstring-4.4.0 → bitstring-5.0.0}/release_notes.md +259 -2
- {bitstring-4.4.0 → bitstring-5.0.0}/tests/test_array.py +389 -48
- {bitstring-4.4.0 → bitstring-5.0.0}/tests/test_benchmarks.py +7 -7
- bitstring-5.0.0/tests/test_bitarray.py +818 -0
- bitstring-5.0.0/tests/test_bitarray_legacy.py +2938 -0
- bitstring-5.0.0/tests/test_bits.py +1400 -0
- bitstring-5.0.0/tests/test_bitstore.py +29 -0
- bitstring-5.0.0/tests/test_bitstring.py +243 -0
- bitstring-5.0.0/tests/test_docs.py +18 -0
- bitstring-5.0.0/tests/test_dtypes.py +377 -0
- bitstring-5.0.0/tests/test_fp8.py +274 -0
- bitstring-5.0.0/tests/test_generated.py +1137 -0
- bitstring-5.0.0/tests/test_mxfp.py +373 -0
- bitstring-5.0.0/tests/test_reader.py +407 -0
- bitstring-5.0.0/tests/test_reader_legacy.py +230 -0
- bitstring-4.4.0/PKG-INFO +0 -143
- bitstring-4.4.0/README.md +0 -114
- bitstring-4.4.0/bitstring/__init__.py +0 -352
- bitstring-4.4.0/bitstring/__main__.py +0 -50
- bitstring-4.4.0/bitstring/array_.py +0 -778
- bitstring-4.4.0/bitstring/bits.py +0 -1742
- bitstring-4.4.0/bitstring/bitstore_bitarray.py +0 -328
- bitstring-4.4.0/bitstring/bitstore_bitarray_helpers.py +0 -80
- bitstring-4.4.0/bitstring/bitstore_common_helpers.py +0 -192
- bitstring-4.4.0/bitstring/bitstore_tibs.py +0 -454
- bitstring-4.4.0/bitstring/bitstore_tibs_helpers.py +0 -78
- bitstring-4.4.0/bitstring/bitstream.py +0 -726
- bitstring-4.4.0/bitstring/bitstring_options.py +0 -107
- bitstring-4.4.0/bitstring/exceptions.py +0 -23
- bitstring-4.4.0/bitstring/fp8.py +0 -97
- bitstring-4.4.0/bitstring/helpers.py +0 -40
- bitstring-4.4.0/bitstring/luts.py +0 -245
- bitstring-4.4.0/bitstring/methods.py +0 -97
- bitstring-4.4.0/bitstring/mxfp.py +0 -206
- bitstring-4.4.0/bitstring.egg-info/PKG-INFO +0 -143
- bitstring-4.4.0/bitstring.egg-info/requires.txt +0 -2
- bitstring-4.4.0/tests/test_bitarray.py +0 -1026
- bitstring-4.4.0/tests/test_bits.py +0 -1008
- bitstring-4.4.0/tests/test_bitstore.py +0 -95
- bitstring-4.4.0/tests/test_bitstream.py +0 -4210
- bitstring-4.4.0/tests/test_bitstring.py +0 -208
- bitstring-4.4.0/tests/test_constbitstream.py +0 -274
- bitstring-4.4.0/tests/test_dtypes.py +0 -154
- bitstring-4.4.0/tests/test_fp8.py +0 -335
- bitstring-4.4.0/tests/test_mxfp.py +0 -466
- bitstring-4.4.0/tests/test_scaled_dtypes.py +0 -13
- {bitstring-4.4.0 → bitstring-5.0.0}/LICENSE +0 -0
- {bitstring-4.4.0 → bitstring-5.0.0}/MANIFEST.in +0 -0
- {bitstring-4.4.0 → bitstring-5.0.0}/bitstring/py.typed +0 -0
- {bitstring-4.4.0 → bitstring-5.0.0}/bitstring.egg-info/dependency_links.txt +0 -0
- {bitstring-4.4.0 → bitstring-5.0.0}/bitstring.egg-info/top_level.txt +0 -0
- {bitstring-4.4.0 → bitstring-5.0.0}/setup.cfg +0 -0
- {bitstring-4.4.0 → bitstring-5.0.0}/tests/__init__.py +0 -0
- {bitstring-4.4.0 → bitstring-5.0.0}/tests/smalltestfile +0 -0
- {bitstring-4.4.0 → bitstring-5.0.0}/tests/test.m1v +0 -0
bitstring-5.0.0/PKG-INFO
ADDED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bitstring
|
|
3
|
+
Version: 5.0.0
|
|
4
|
+
Summary: Simple construction, analysis and modification of binary data: bit arrays, bitfields and unaligned binary formats.
|
|
5
|
+
Author-email: Scott Griffiths <dr.scottgriffiths@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: homepage, https://github.com/scott-griffiths/bitstring
|
|
8
|
+
Project-URL: documentation, https://bitstring.readthedocs.io/
|
|
9
|
+
Project-URL: source, https://github.com/scott-griffiths/bitstring
|
|
10
|
+
Project-URL: issues, https://github.com/scott-griffiths/bitstring/issues
|
|
11
|
+
Project-URL: changelog, https://github.com/scott-griffiths/bitstring/blob/main/release_notes.md
|
|
12
|
+
Keywords: binary,bits,bitstring,bit array,bitarray,bit vector,bitvector,bitfield,bitset,bit manipulation,binary parsing,unaligned,packet,struct,endianness,file format
|
|
13
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
21
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
22
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
23
|
+
Requires-Python: >=3.11
|
|
24
|
+
Description-Content-Type: text/markdown
|
|
25
|
+
License-File: LICENSE
|
|
26
|
+
Requires-Dist: tibs<3.0,>=2.0.2
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+

|
|
32
|
+
|
|
33
|
+
**bitstring** is a Python library to help make the creation and analysis of all types of bit-level binary data as simple and efficient as possible. It has been actively maintained since 2006.
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
[](https://github.com/scott-griffiths/bitstring/actions/workflows/ci.yml)
|
|
37
|
+
[](https://bitstring.readthedocs.io/en/stable/)
|
|
38
|
+
[](https://libraries.io/pypi/bitstring)
|
|
39
|
+
|
|
40
|
+
[](https://www.pepy.tech/projects/bitstring)
|
|
41
|
+
[](https://pypistats.org/packages/bitstring)
|
|
42
|
+
|
|
43
|
+
----
|
|
44
|
+
|
|
45
|
+
> [!NOTE]
|
|
46
|
+
> To see what's been added, improved or fixed, see the [release notes](https://github.com/scott-griffiths/bitstring/blob/main/release_notes.md).
|
|
47
|
+
|
|
48
|
+
# Version 5.0
|
|
49
|
+
|
|
50
|
+
This is a major update with breaking changes.
|
|
51
|
+
|
|
52
|
+
* Significantly better performance using the new [tibs](https://github.com/scott-griffiths/tibs) Rust core - around 4x faster across the benchmark suite, and far more for bulk `Array` work, searching and packing.
|
|
53
|
+
* A simpler model for sequential reading: `Bits` and `BitArray` store data, while `Reader` stores the current bit position.
|
|
54
|
+
* A clearer, more explicit API for construction, conversion and dtype names.
|
|
55
|
+
* Some things have also been removed, including the `bitarray` dependency, LSB0 mode and the old stream classes.
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
## Upgrading from version 4
|
|
59
|
+
|
|
60
|
+
Version 5 is worth moving to if you need the improved performance, want the cleaner API and other new features.
|
|
61
|
+
The changes are quite broad, so if your existing code is stable and performance
|
|
62
|
+
is not a problem, there is no need to migrate immediately - pin your dependency to `bitstring<5` to stay on 4.x.
|
|
63
|
+
The 4.x line is maintained on the `4.x-maintenance` branch.
|
|
64
|
+
|
|
65
|
+
For details on the source changes you may need, see the [Upgrading to version 5](https://bitstring.readthedocs.io/en/stable/upgrading_to_version_5.html) guide.
|
|
66
|
+
|
|
67
|
+
## What's this 'tibs' thing that everyone is talking about?
|
|
68
|
+
|
|
69
|
+
bitstring 5 is built on [tibs](https://github.com/scott-griffiths/tibs), a more focussed Python library for
|
|
70
|
+
binary data, written in Rust and highly optimised for speed. It's by the same author as bitstring.
|
|
71
|
+
For new projects, starting with [tibs]([tibs](https://github.com/scott-griffiths/tibs)) could be a better choice in many cases.
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
Tibs has now reached its 2.0 release. Please do try it, as all feedback is welcome.
|
|
75
|
+
|
|
76
|
+
<a href="https://github.com/scott-griffiths/tibs">
|
|
77
|
+
<img src="https://raw.githubusercontent.com/scott-griffiths/tibs/main/doc/tibs.png" alt="tibs" width="30%">
|
|
78
|
+
</a>
|
|
79
|
+
|
|
80
|
+
A sleek Python library for binary data
|
|
81
|
+
|
|
82
|
+
----
|
|
83
|
+
|
|
84
|
+
# Bitstring Overview
|
|
85
|
+
|
|
86
|
+
* Efficiently store and manipulate binary data in idiomatic Python.
|
|
87
|
+
* Create bitstrings from hex, octal, binary, files, formatted strings, bytes, integers and floats of different endiannesses.
|
|
88
|
+
* Powerful binary packing and unpacking functions.
|
|
89
|
+
* Bit-level slicing, joining, searching, replacing and more.
|
|
90
|
+
* Create and manipulate arrays of fixed-length bitstrings.
|
|
91
|
+
* Use `Reader` to read from and interpret bitstrings sequentially.
|
|
92
|
+
* Rich API - chances are that whatever you want to do there's a simple and elegant way of doing it.
|
|
93
|
+
* Open source software, released under the MIT licence.
|
|
94
|
+
|
|
95
|
+
# Documentation
|
|
96
|
+
|
|
97
|
+
Extensive documentation for the bitstring library is available.
|
|
98
|
+
Some starting points:
|
|
99
|
+
|
|
100
|
+
* [Overview](https://bitstring.readthedocs.io/en/stable/index.html)
|
|
101
|
+
* [Quick reference](https://bitstring.readthedocs.io/en/stable/quick_reference.html)
|
|
102
|
+
* [Upgrading from 4.x to 5.0](https://bitstring.readthedocs.io/en/stable/upgrading_to_version_5.html)
|
|
103
|
+
* [Version 4.4 documentation](https://bitstring.readthedocs.io/en/4.4.0/)
|
|
104
|
+
|
|
105
|
+
There is also an introductory walkthrough notebook on [binder](https://mybinder.org/v2/gh/scott-griffiths/bitstring/main?labpath=doc%2Fwalkthrough.ipynb).
|
|
106
|
+
|
|
107
|
+
# Examples
|
|
108
|
+
|
|
109
|
+
### Creation
|
|
110
|
+
```pycon
|
|
111
|
+
>>> from bitstring import Bits, BitArray, Reader, pack
|
|
112
|
+
>>> a = BitArray(bin='00101')
|
|
113
|
+
>>> b = Bits.from_file(a_file_object)
|
|
114
|
+
>>> c = BitArray('0xff, 0b101, 0o65, u6=22')
|
|
115
|
+
>>> d = pack('ile16, hex=a, 0b1', 100, a='0x34f')
|
|
116
|
+
>>> e = pack('<16h', *range(16))
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### Different interpretations, slicing and concatenation
|
|
120
|
+
```pycon
|
|
121
|
+
>>> a = BitArray('0x3348')
|
|
122
|
+
>>> a.hex, a.bin, a.u, a.f, a.bytes
|
|
123
|
+
('3348', '0011001101001000', 13128, 0.2275390625, b'3H')
|
|
124
|
+
>>> a[10:3:-1].bin
|
|
125
|
+
'0101100'
|
|
126
|
+
>>> '0b100' + 3*a
|
|
127
|
+
BitArray('0x866906690669, 0b000')
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Reading data sequentially
|
|
131
|
+
```pycon
|
|
132
|
+
>>> b = Reader(Bits('0x160120f'))
|
|
133
|
+
>>> b.read_bits(12).hex
|
|
134
|
+
'160'
|
|
135
|
+
>>> b.pos = 0
|
|
136
|
+
>>> b.read_value('u12')
|
|
137
|
+
352
|
|
138
|
+
>>> b.read_list('u12, bin3')
|
|
139
|
+
[288, '111']
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### Searching, inserting and deleting
|
|
143
|
+
```pycon
|
|
144
|
+
>>> c = BitArray('0b00010010010010001111') # c.hex == '0x1248f'
|
|
145
|
+
>>> c.find('0x48')
|
|
146
|
+
8
|
|
147
|
+
>>> c.replace('0b001', '0xabc')
|
|
148
|
+
5
|
|
149
|
+
>>> c.insert(3, '0b0000')
|
|
150
|
+
>>> del c[12:16]
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Arrays of fixed-length formats
|
|
154
|
+
```pycon
|
|
155
|
+
>>> from bitstring import Array
|
|
156
|
+
>>> a = Array('u7', [9, 100, 3, 1])
|
|
157
|
+
>>> a.data
|
|
158
|
+
BitArray('0x1390181')
|
|
159
|
+
>>> a[::2] *= 5
|
|
160
|
+
>>> a
|
|
161
|
+
Array('u7', [45, 100, 15, 1])
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
<sub>Copyright (c) 2006 - 2026 Scott Griffiths</sub>
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
**bitstring** is a Python library to help make the creation and analysis of all types of bit-level binary data as simple and efficient as possible. It has been actively maintained since 2006.
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
[](https://github.com/scott-griffiths/bitstring/actions/workflows/ci.yml)
|
|
9
|
+
[](https://bitstring.readthedocs.io/en/stable/)
|
|
10
|
+
[](https://libraries.io/pypi/bitstring)
|
|
11
|
+
|
|
12
|
+
[](https://www.pepy.tech/projects/bitstring)
|
|
13
|
+
[](https://pypistats.org/packages/bitstring)
|
|
14
|
+
|
|
15
|
+
----
|
|
16
|
+
|
|
17
|
+
> [!NOTE]
|
|
18
|
+
> To see what's been added, improved or fixed, see the [release notes](https://github.com/scott-griffiths/bitstring/blob/main/release_notes.md).
|
|
19
|
+
|
|
20
|
+
# Version 5.0
|
|
21
|
+
|
|
22
|
+
This is a major update with breaking changes.
|
|
23
|
+
|
|
24
|
+
* Significantly better performance using the new [tibs](https://github.com/scott-griffiths/tibs) Rust core - around 4x faster across the benchmark suite, and far more for bulk `Array` work, searching and packing.
|
|
25
|
+
* A simpler model for sequential reading: `Bits` and `BitArray` store data, while `Reader` stores the current bit position.
|
|
26
|
+
* A clearer, more explicit API for construction, conversion and dtype names.
|
|
27
|
+
* Some things have also been removed, including the `bitarray` dependency, LSB0 mode and the old stream classes.
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
## Upgrading from version 4
|
|
31
|
+
|
|
32
|
+
Version 5 is worth moving to if you need the improved performance, want the cleaner API and other new features.
|
|
33
|
+
The changes are quite broad, so if your existing code is stable and performance
|
|
34
|
+
is not a problem, there is no need to migrate immediately - pin your dependency to `bitstring<5` to stay on 4.x.
|
|
35
|
+
The 4.x line is maintained on the `4.x-maintenance` branch.
|
|
36
|
+
|
|
37
|
+
For details on the source changes you may need, see the [Upgrading to version 5](https://bitstring.readthedocs.io/en/stable/upgrading_to_version_5.html) guide.
|
|
38
|
+
|
|
39
|
+
## What's this 'tibs' thing that everyone is talking about?
|
|
40
|
+
|
|
41
|
+
bitstring 5 is built on [tibs](https://github.com/scott-griffiths/tibs), a more focussed Python library for
|
|
42
|
+
binary data, written in Rust and highly optimised for speed. It's by the same author as bitstring.
|
|
43
|
+
For new projects, starting with [tibs]([tibs](https://github.com/scott-griffiths/tibs)) could be a better choice in many cases.
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
Tibs has now reached its 2.0 release. Please do try it, as all feedback is welcome.
|
|
47
|
+
|
|
48
|
+
<a href="https://github.com/scott-griffiths/tibs">
|
|
49
|
+
<img src="https://raw.githubusercontent.com/scott-griffiths/tibs/main/doc/tibs.png" alt="tibs" width="30%">
|
|
50
|
+
</a>
|
|
51
|
+
|
|
52
|
+
A sleek Python library for binary data
|
|
53
|
+
|
|
54
|
+
----
|
|
55
|
+
|
|
56
|
+
# Bitstring Overview
|
|
57
|
+
|
|
58
|
+
* Efficiently store and manipulate binary data in idiomatic Python.
|
|
59
|
+
* Create bitstrings from hex, octal, binary, files, formatted strings, bytes, integers and floats of different endiannesses.
|
|
60
|
+
* Powerful binary packing and unpacking functions.
|
|
61
|
+
* Bit-level slicing, joining, searching, replacing and more.
|
|
62
|
+
* Create and manipulate arrays of fixed-length bitstrings.
|
|
63
|
+
* Use `Reader` to read from and interpret bitstrings sequentially.
|
|
64
|
+
* Rich API - chances are that whatever you want to do there's a simple and elegant way of doing it.
|
|
65
|
+
* Open source software, released under the MIT licence.
|
|
66
|
+
|
|
67
|
+
# Documentation
|
|
68
|
+
|
|
69
|
+
Extensive documentation for the bitstring library is available.
|
|
70
|
+
Some starting points:
|
|
71
|
+
|
|
72
|
+
* [Overview](https://bitstring.readthedocs.io/en/stable/index.html)
|
|
73
|
+
* [Quick reference](https://bitstring.readthedocs.io/en/stable/quick_reference.html)
|
|
74
|
+
* [Upgrading from 4.x to 5.0](https://bitstring.readthedocs.io/en/stable/upgrading_to_version_5.html)
|
|
75
|
+
* [Version 4.4 documentation](https://bitstring.readthedocs.io/en/4.4.0/)
|
|
76
|
+
|
|
77
|
+
There is also an introductory walkthrough notebook on [binder](https://mybinder.org/v2/gh/scott-griffiths/bitstring/main?labpath=doc%2Fwalkthrough.ipynb).
|
|
78
|
+
|
|
79
|
+
# Examples
|
|
80
|
+
|
|
81
|
+
### Creation
|
|
82
|
+
```pycon
|
|
83
|
+
>>> from bitstring import Bits, BitArray, Reader, pack
|
|
84
|
+
>>> a = BitArray(bin='00101')
|
|
85
|
+
>>> b = Bits.from_file(a_file_object)
|
|
86
|
+
>>> c = BitArray('0xff, 0b101, 0o65, u6=22')
|
|
87
|
+
>>> d = pack('ile16, hex=a, 0b1', 100, a='0x34f')
|
|
88
|
+
>>> e = pack('<16h', *range(16))
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Different interpretations, slicing and concatenation
|
|
92
|
+
```pycon
|
|
93
|
+
>>> a = BitArray('0x3348')
|
|
94
|
+
>>> a.hex, a.bin, a.u, a.f, a.bytes
|
|
95
|
+
('3348', '0011001101001000', 13128, 0.2275390625, b'3H')
|
|
96
|
+
>>> a[10:3:-1].bin
|
|
97
|
+
'0101100'
|
|
98
|
+
>>> '0b100' + 3*a
|
|
99
|
+
BitArray('0x866906690669, 0b000')
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### Reading data sequentially
|
|
103
|
+
```pycon
|
|
104
|
+
>>> b = Reader(Bits('0x160120f'))
|
|
105
|
+
>>> b.read_bits(12).hex
|
|
106
|
+
'160'
|
|
107
|
+
>>> b.pos = 0
|
|
108
|
+
>>> b.read_value('u12')
|
|
109
|
+
352
|
|
110
|
+
>>> b.read_list('u12, bin3')
|
|
111
|
+
[288, '111']
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Searching, inserting and deleting
|
|
115
|
+
```pycon
|
|
116
|
+
>>> c = BitArray('0b00010010010010001111') # c.hex == '0x1248f'
|
|
117
|
+
>>> c.find('0x48')
|
|
118
|
+
8
|
|
119
|
+
>>> c.replace('0b001', '0xabc')
|
|
120
|
+
5
|
|
121
|
+
>>> c.insert(3, '0b0000')
|
|
122
|
+
>>> del c[12:16]
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### Arrays of fixed-length formats
|
|
126
|
+
```pycon
|
|
127
|
+
>>> from bitstring import Array
|
|
128
|
+
>>> a = Array('u7', [9, 100, 3, 1])
|
|
129
|
+
>>> a.data
|
|
130
|
+
BitArray('0x1390181')
|
|
131
|
+
>>> a[::2] *= 5
|
|
132
|
+
>>> a
|
|
133
|
+
Array('u7', [45, 100, 15, 1])
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
<sub>Copyright (c) 2006 - 2026 Scott Griffiths</sub>
|
|
@@ -0,0 +1,337 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
r"""
|
|
3
|
+
This package defines classes that simplify bit-wise creation, manipulation and
|
|
4
|
+
interpretation of data. Lengths and positions are in bits throughout, so fields
|
|
5
|
+
need not be byte aligned.
|
|
6
|
+
|
|
7
|
+
Classes:
|
|
8
|
+
|
|
9
|
+
Bits -- An immutable container for binary data.
|
|
10
|
+
BitArray -- A mutable container for binary data. Adds in-place methods such as
|
|
11
|
+
append, insert, overwrite, replace and set to those of Bits.
|
|
12
|
+
Reader -- Wraps a Bits or BitArray with a bit position for sequential reading.
|
|
13
|
+
Array -- An efficient list-like container where each item has a fixed-length binary format.
|
|
14
|
+
Dtype -- A data type such as 'u12', 'f32' or 'hex', used to create and interpret bits.
|
|
15
|
+
|
|
16
|
+
Functions:
|
|
17
|
+
|
|
18
|
+
pack -- Create a Bits object from a format string and values.
|
|
19
|
+
|
|
20
|
+
Exceptions:
|
|
21
|
+
|
|
22
|
+
ReadError -- Reading or peeking past the end of a bitstring. Subclasses ValueError.
|
|
23
|
+
|
|
24
|
+
Creation:
|
|
25
|
+
|
|
26
|
+
Bits('0x4f8e220') -- from a string of hex, '0b' binary or '0o' octal.
|
|
27
|
+
Bits('0b101, u12=100, f32=0.25') -- comma-separated tokens, each a literal or dtype=value.
|
|
28
|
+
Bits(u=100, length=12) -- from a single keyword value.
|
|
29
|
+
Bits.from_bytes(b), Bits.from_file(path), Bits.from_zeros(n), Bits.from_dtype('i8', -1)
|
|
30
|
+
pack('u12, f32, bool', 100, 0.25, True)
|
|
31
|
+
|
|
32
|
+
Interpretation:
|
|
33
|
+
|
|
34
|
+
Properties such as .hex, .bin, .bytes, .u, .i and .f interpret the whole
|
|
35
|
+
bitstring. Endian variants include .ule, .ibe and .fle. Use unpack() to
|
|
36
|
+
interpret it as several fields, or a Reader to read fields one at a time.
|
|
37
|
+
|
|
38
|
+
Example:
|
|
39
|
+
|
|
40
|
+
>>> from bitstring import Bits, BitArray, Reader, Array, pack
|
|
41
|
+
>>> s = Bits('0x4f8e220')
|
|
42
|
+
>>> s.unpack('u12, hex8, bin')
|
|
43
|
+
[1272, 'e2', '00100000']
|
|
44
|
+
>>> s[:12].u
|
|
45
|
+
1272
|
|
46
|
+
>>> r = Reader(pack('u12, f32, bool', 100, 0.25, True))
|
|
47
|
+
>>> r.read_value('u12'), r.read_value('f32'), r.read_value('bool')
|
|
48
|
+
(100, 0.25, True)
|
|
49
|
+
>>> b = BitArray('0x0f')
|
|
50
|
+
>>> b.insert(4, '0b11')
|
|
51
|
+
>>> b.bin
|
|
52
|
+
'0000111111'
|
|
53
|
+
>>> Array('u5', [1, 2, 31]).data
|
|
54
|
+
BitArray('0b000010001011111')
|
|
55
|
+
|
|
56
|
+
The core is the tibs Rust library (https://github.com/scott-griffiths/tibs),
|
|
57
|
+
which can also be used directly for performance-critical bit work.
|
|
58
|
+
|
|
59
|
+
Documentation: https://bitstring.readthedocs.io/
|
|
60
|
+
https://github.com/scott-griffiths/bitstring
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
__licence__ = """
|
|
64
|
+
The MIT License
|
|
65
|
+
|
|
66
|
+
Copyright (c) 2006 Scott Griffiths (dr.scottgriffiths@gmail.com)
|
|
67
|
+
|
|
68
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
69
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
70
|
+
in the Software without restriction, including without limitation the rights
|
|
71
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
72
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
73
|
+
furnished to do so, subject to the following conditions:
|
|
74
|
+
|
|
75
|
+
The above copyright notice and this permission notice shall be included in
|
|
76
|
+
all copies or substantial portions of the Software.
|
|
77
|
+
|
|
78
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
79
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
80
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
81
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
82
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
83
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
84
|
+
THE SOFTWARE.
|
|
85
|
+
"""
|
|
86
|
+
|
|
87
|
+
__version__ = "5.0.0"
|
|
88
|
+
|
|
89
|
+
__author__ = "Scott Griffiths"
|
|
90
|
+
|
|
91
|
+
import bitstring.bitstore as bitstore # noqa: F401 - the core must be initialised before the classes that use it.
|
|
92
|
+
|
|
93
|
+
from .bits import Bits
|
|
94
|
+
from .bitarray_ import BitArray
|
|
95
|
+
from .reader import Reader
|
|
96
|
+
from .methods import pack
|
|
97
|
+
from .array_ import Array
|
|
98
|
+
from .exceptions import ReadError
|
|
99
|
+
from .dtypes import DtypeDefinition as _DtypeDefinition, dtype_register as _dtype_register, Dtype
|
|
100
|
+
from typing import Literal as _Literal
|
|
101
|
+
|
|
102
|
+
# These methods convert a bit length to the number of characters needed to print it for different interpretations.
|
|
103
|
+
def _hex_bits2chars(bitlength: int):
|
|
104
|
+
# One character for every 4 bits
|
|
105
|
+
return bitlength // 4
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _oct_bits2chars(bitlength: int):
|
|
109
|
+
# One character for every 3 bits
|
|
110
|
+
return bitlength // 3
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
def _bin_bits2chars(bitlength: int):
|
|
114
|
+
# One character for each bit
|
|
115
|
+
return bitlength
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _bytes_bits2chars(bitlength: int):
|
|
119
|
+
# One character for every 8 bits
|
|
120
|
+
return bitlength // 8
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def _uint_bits2chars(bitlength: int):
|
|
124
|
+
# How many characters is largest possible int of this length?
|
|
125
|
+
return len(str((1 << bitlength) - 1))
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _int_bits2chars(bitlength: int):
|
|
129
|
+
# How many characters is largest negative int of this length? (To include minus sign).
|
|
130
|
+
return len(str(-1 << (bitlength - 1)))
|
|
131
|
+
|
|
132
|
+
|
|
133
|
+
def _float_bits2chars(bitlength: _Literal[16, 32, 64]):
|
|
134
|
+
# These bit lengths were found by looking at lots of possible values
|
|
135
|
+
if bitlength in [16, 32]:
|
|
136
|
+
return 23 # Empirical value
|
|
137
|
+
else:
|
|
138
|
+
return 24 # Empirical value
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def _p3binary_bits2chars(_: _Literal[8]):
|
|
142
|
+
return 19 # Empirical value
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _p4binary_bits2chars(_: _Literal[8]):
|
|
146
|
+
# Found by looking at all the possible values
|
|
147
|
+
return 13 # Empirical value
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
def _e4m3mxfp_bits2chars(_: _Literal[8]):
|
|
151
|
+
return 13
|
|
152
|
+
|
|
153
|
+
|
|
154
|
+
def _e5m2mxfp_bits2chars(_: _Literal[8]):
|
|
155
|
+
return 19
|
|
156
|
+
|
|
157
|
+
|
|
158
|
+
def _e3m2mxfp_bits2chars(_: _Literal[6]):
|
|
159
|
+
# Not sure what the best value is here.
|
|
160
|
+
return 7
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def _e2m3mxfp_bits2chars(_: _Literal[6]):
|
|
164
|
+
# Not sure what the best value is here.
|
|
165
|
+
return 7
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _e2m1mxfp_bits2chars(_: _Literal[4]):
|
|
169
|
+
# Not sure what the best value is here.
|
|
170
|
+
return 7
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
def _e8m0mxfp_bits2chars(_: _Literal[8]):
|
|
174
|
+
# Has same range as float32
|
|
175
|
+
return 23
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def _mxint_bits2chars(_: _Literal[8]):
|
|
179
|
+
# Not sure what the best value is here.
|
|
180
|
+
return 10
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _bfloat_bits2chars(_: _Literal[16]):
|
|
184
|
+
# Found by looking at all the possible values
|
|
185
|
+
return 23 # Empirical value
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _bits_bits2chars(bitlength: int):
|
|
189
|
+
# For bits type we can see how long it needs to be printed by trying any value
|
|
190
|
+
temp = Bits.from_zeros(bitlength)
|
|
191
|
+
return len(str(temp))
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def _bool_bits2chars(_: _Literal[1]):
|
|
195
|
+
# Bools are printed as 1 or 0, not True or False, so are one character each
|
|
196
|
+
return 1
|
|
197
|
+
|
|
198
|
+
_dtype_definitions = [
|
|
199
|
+
# Integer types
|
|
200
|
+
_DtypeDefinition('u', Bits._setuint, Bits._getuint, int, False, _uint_bits2chars,
|
|
201
|
+
read_fn=Bits._readuint,
|
|
202
|
+
allowed_lengths=(1, 2, ...), description="a two's complement unsigned int"),
|
|
203
|
+
_DtypeDefinition('ule', Bits._setuintle, Bits._getuintle, int, False, _uint_bits2chars,
|
|
204
|
+
read_fn=Bits._readuintle,
|
|
205
|
+
allowed_lengths=(8, 16, 24, ...), description="a two's complement little-endian unsigned int"),
|
|
206
|
+
_DtypeDefinition('ube', Bits._setuintbe, Bits._getuintbe, int, False, _uint_bits2chars,
|
|
207
|
+
read_fn=Bits._readuintbe,
|
|
208
|
+
allowed_lengths=(8, 16, 24, ...), description="a two's complement big-endian unsigned int"),
|
|
209
|
+
_DtypeDefinition('i', Bits._setint, Bits._getint, int, True, _int_bits2chars,
|
|
210
|
+
read_fn=Bits._readint,
|
|
211
|
+
allowed_lengths=(1, 2, ...), description="a two's complement signed int"),
|
|
212
|
+
_DtypeDefinition('ile', Bits._setintle, Bits._getintle, int, True, _int_bits2chars,
|
|
213
|
+
read_fn=Bits._readintle,
|
|
214
|
+
allowed_lengths=(8, 16, 24, ...), description="a two's complement little-endian signed int"),
|
|
215
|
+
_DtypeDefinition('ibe', Bits._setintbe, Bits._getintbe, int, True, _int_bits2chars,
|
|
216
|
+
read_fn=Bits._readintbe,
|
|
217
|
+
allowed_lengths=(8, 16, 24, ...), description="a two's complement big-endian signed int"),
|
|
218
|
+
# String types
|
|
219
|
+
_DtypeDefinition('hex', Bits._sethex, Bits._gethex, str, False, _hex_bits2chars,
|
|
220
|
+
read_fn=Bits._readhex,
|
|
221
|
+
allowed_lengths=(0, 4, 8, ...), description="a hexadecimal string"),
|
|
222
|
+
_DtypeDefinition('bin', Bits._setbin, Bits._getbin, str, False, _bin_bits2chars,
|
|
223
|
+
read_fn=Bits._readbin,
|
|
224
|
+
description="a binary string"),
|
|
225
|
+
_DtypeDefinition('oct', Bits._setoct, Bits._getoct, str, False, _oct_bits2chars,
|
|
226
|
+
read_fn=Bits._readoct,
|
|
227
|
+
allowed_lengths=(0, 3, 6, ...), description="an octal string"),
|
|
228
|
+
# Float types
|
|
229
|
+
_DtypeDefinition('f', Bits._setfloatbe, Bits._getfloatbe, float, True, _float_bits2chars,
|
|
230
|
+
read_fn=Bits._readfloatbe,
|
|
231
|
+
allowed_lengths=(16, 32, 64), description="a big-endian floating point number"),
|
|
232
|
+
_DtypeDefinition('fle', Bits._setfloatle, Bits._getfloatle, float, True, _float_bits2chars,
|
|
233
|
+
read_fn=Bits._readfloatle,
|
|
234
|
+
allowed_lengths=(16, 32, 64), description="a little-endian floating point number"),
|
|
235
|
+
_DtypeDefinition('bfloat', Bits._setbfloatbe, Bits._getbfloatbe, float, True, _bfloat_bits2chars,
|
|
236
|
+
read_fn=Bits._readbfloatbe,
|
|
237
|
+
allowed_lengths=(16,), description="a 16 bit big-endian bfloat floating point number"),
|
|
238
|
+
_DtypeDefinition('bfloatle', Bits._setbfloatle, Bits._getbfloatle, float, True, _bfloat_bits2chars,
|
|
239
|
+
read_fn=Bits._readbfloatle,
|
|
240
|
+
allowed_lengths=(16,), description="a 16 bit little-endian bfloat floating point number"),
|
|
241
|
+
# Other known length types
|
|
242
|
+
_DtypeDefinition('bits', Bits._setbits, Bits._getbits, Bits, False, _bits_bits2chars,
|
|
243
|
+
is_property=False, description="a bitstring object"),
|
|
244
|
+
_DtypeDefinition('bool', Bits._setbool, Bits._getbool, bool, False, _bool_bits2chars,
|
|
245
|
+
read_fn=Bits._readbool,
|
|
246
|
+
allowed_lengths=(1,), description="a bool (True or False)"),
|
|
247
|
+
_DtypeDefinition('bytes', Bits._setbytes, Bits._getbytes, bytes, False, _bytes_bits2chars,
|
|
248
|
+
read_fn=Bits._readbytes,
|
|
249
|
+
multiplier=8, description="a bytes object"),
|
|
250
|
+
# Unknown length types
|
|
251
|
+
_DtypeDefinition('se', Bits._setse, Bits._getse, int, True, None,
|
|
252
|
+
read_fn=Bits._readse, variable_length=True, description="a signed exponential-Golomb code"),
|
|
253
|
+
_DtypeDefinition('ue', Bits._setue, Bits._getue, int, False, None,
|
|
254
|
+
read_fn=Bits._readue, variable_length=True, description="an unsigned exponential-Golomb code"),
|
|
255
|
+
_DtypeDefinition('sie', Bits._setsie, Bits._getsie, int, True, None,
|
|
256
|
+
read_fn=Bits._readsie, variable_length=True, description="a signed interleaved exponential-Golomb code"),
|
|
257
|
+
_DtypeDefinition('uie', Bits._setuie, Bits._getuie, int, False, None,
|
|
258
|
+
read_fn=Bits._readuie, variable_length=True, description="an unsigned interleaved exponential-Golomb code"),
|
|
259
|
+
# Special case pad type
|
|
260
|
+
_DtypeDefinition('pad', Bits._setpad, Bits._getpad, None, False, None,
|
|
261
|
+
read_fn=Bits._readpad, is_property=False,
|
|
262
|
+
description="a skipped section of padding"),
|
|
263
|
+
|
|
264
|
+
# MXFP and IEEE 8-bit float types
|
|
265
|
+
_DtypeDefinition('p3binary', Bits._setp3binary, Bits._getp3binary, float, True, _p3binary_bits2chars,
|
|
266
|
+
read_fn=Bits._readp3binary,
|
|
267
|
+
allowed_lengths=(8,), description="an 8 bit float with binary8p3 format"),
|
|
268
|
+
_DtypeDefinition('p4binary', Bits._setp4binary, Bits._getp4binary, float, True, _p4binary_bits2chars,
|
|
269
|
+
read_fn=Bits._readp4binary,
|
|
270
|
+
allowed_lengths=(8,), description="an 8 bit float with binary8p4 format"),
|
|
271
|
+
_DtypeDefinition('e4m3mxfp_saturate', Bits._sete4m3mxfp_saturate, Bits._gete4m3mxfp, float, True, _e4m3mxfp_bits2chars,
|
|
272
|
+
read_fn=Bits._reade4m3mxfp,
|
|
273
|
+
allowed_lengths=(8,), description="an 8 bit float with MXFP E4M3 format, saturating out-of-range values"),
|
|
274
|
+
_DtypeDefinition('e4m3mxfp_overflow', Bits._sete4m3mxfp_overflow, Bits._gete4m3mxfp, float, True, _e4m3mxfp_bits2chars,
|
|
275
|
+
read_fn=Bits._reade4m3mxfp,
|
|
276
|
+
allowed_lengths=(8,), description="an 8 bit float with MXFP E4M3 format, overflowing out-of-range values to NaN"),
|
|
277
|
+
_DtypeDefinition('e5m2mxfp_saturate', Bits._sete5m2mxfp_saturate, Bits._gete5m2mxfp, float, True, _e5m2mxfp_bits2chars,
|
|
278
|
+
read_fn=Bits._reade5m2mxfp,
|
|
279
|
+
allowed_lengths=(8,), description="an 8 bit float with MXFP E5M2 format, saturating out-of-range values"),
|
|
280
|
+
_DtypeDefinition('e5m2mxfp_overflow', Bits._sete5m2mxfp_overflow, Bits._gete5m2mxfp, float, True, _e5m2mxfp_bits2chars,
|
|
281
|
+
read_fn=Bits._reade5m2mxfp,
|
|
282
|
+
allowed_lengths=(8,), description="an 8 bit float with MXFP E5M2 format, overflowing out-of-range values to ±inf"),
|
|
283
|
+
_DtypeDefinition('e3m2mxfp', Bits._sete3m2mxfp, Bits._gete3m2mxfp, float, True, _e3m2mxfp_bits2chars,
|
|
284
|
+
read_fn=Bits._reade3m2mxfp,
|
|
285
|
+
allowed_lengths=(6,), description="a 6 bit float with MXFP E3M2 format"),
|
|
286
|
+
_DtypeDefinition('e2m3mxfp', Bits._sete2m3mxfp, Bits._gete2m3mxfp, float, True, _e2m3mxfp_bits2chars,
|
|
287
|
+
read_fn=Bits._reade2m3mxfp,
|
|
288
|
+
allowed_lengths=(6,), description="a 6 bit float with MXFP E2M3 format"),
|
|
289
|
+
_DtypeDefinition('e2m1mxfp', Bits._sete2m1mxfp, Bits._gete2m1mxfp, float, True, _e2m1mxfp_bits2chars,
|
|
290
|
+
read_fn=Bits._reade2m1mxfp,
|
|
291
|
+
allowed_lengths=(4,), description="a 4 bit float with MXFP E2M1 format"),
|
|
292
|
+
_DtypeDefinition('e8m0mxfp', Bits._sete8m0mxfp, Bits._gete8m0mxfp, float, False, _e8m0mxfp_bits2chars,
|
|
293
|
+
read_fn=Bits._reade8m0mxfp,
|
|
294
|
+
allowed_lengths=(8,), description="an 8 bit float with MXFP E8M0 format"),
|
|
295
|
+
_DtypeDefinition('mxint', Bits._setmxint, Bits._getmxint, float, True, _mxint_bits2chars,
|
|
296
|
+
read_fn=Bits._readmxint,
|
|
297
|
+
allowed_lengths=(8,), description="an 8 bit float with MXFP INT8 format"),
|
|
298
|
+
]
|
|
299
|
+
|
|
300
|
+
|
|
301
|
+
_aliases: list[tuple[str, str]] = [
|
|
302
|
+
# Floats default to big endian
|
|
303
|
+
('f', 'float'),
|
|
304
|
+
('f', 'floatbe'),
|
|
305
|
+
('f', 'fbe'),
|
|
306
|
+
('bfloat', 'bfloatbe'),
|
|
307
|
+
|
|
308
|
+
# Longer compatibility aliases for the short core type names
|
|
309
|
+
('i', 'int'),
|
|
310
|
+
('u', 'uint'),
|
|
311
|
+
|
|
312
|
+
# Longer compatibility aliases for endian-specific type names
|
|
313
|
+
('ube', 'uintbe'),
|
|
314
|
+
('ule', 'uintle'),
|
|
315
|
+
('ibe', 'intbe'),
|
|
316
|
+
('ile', 'intle'),
|
|
317
|
+
('fle', 'floatle'),
|
|
318
|
+
]
|
|
319
|
+
|
|
320
|
+
|
|
321
|
+
for _dt in _dtype_definitions:
|
|
322
|
+
_dtype_register.add_dtype(_dt)
|
|
323
|
+
for _alias in _aliases:
|
|
324
|
+
_dtype_register.add_dtype_alias(_alias[0], _alias[1])
|
|
325
|
+
del _dt, _alias
|
|
326
|
+
|
|
327
|
+
_property_docstrings = [f'{name} -- Interpret as {_dtype_register[name].description}.'
|
|
328
|
+
for name in _dtype_register.names if _dtype_register[name].is_property]
|
|
329
|
+
_property_docstring = '\n '.join(_property_docstrings)
|
|
330
|
+
|
|
331
|
+
# We can't be sure the docstrings are present, as it might be compiled without docstrings.
|
|
332
|
+
if Bits.__doc__ is not None:
|
|
333
|
+
Bits.__doc__ = Bits.__doc__.replace('[GENERATED_PROPERTY_DESCRIPTIONS]', _property_docstring)
|
|
334
|
+
if BitArray.__doc__ is not None:
|
|
335
|
+
BitArray.__doc__ = BitArray.__doc__.replace('[GENERATED_PROPERTY_DESCRIPTIONS]', _property_docstring)
|
|
336
|
+
__all__ = ['Reader', 'BitArray', 'Array',
|
|
337
|
+
'Bits', 'pack', 'ReadError', 'Dtype']
|