bitstring 4.1.3__tar.gz → 4.2.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.
Files changed (60) hide show
  1. {bitstring-4.1.3/bitstring.egg-info → bitstring-4.2.0}/PKG-INFO +25 -32
  2. {bitstring-4.1.3 → bitstring-4.2.0}/README.md +21 -28
  3. bitstring-4.2.0/bitstring/__init__.py +313 -0
  4. {bitstring-4.1.3 → bitstring-4.2.0}/bitstring/__main__.py +2 -2
  5. {bitstring-4.1.3 → bitstring-4.2.0}/bitstring/array_.py +149 -114
  6. bitstring-4.1.3/bitstring/bitarray.py → bitstring-4.2.0/bitstring/bitarray_.py +48 -92
  7. bitstring-4.2.0/bitstring/bits.py +1790 -0
  8. bitstring-4.2.0/bitstring/bitstore.py +254 -0
  9. bitstring-4.2.0/bitstring/bitstore_helpers.py +260 -0
  10. {bitstring-4.1.3 → bitstring-4.2.0}/bitstring/bitstream.py +117 -100
  11. bitstring-4.2.0/bitstring/bitstring_options.py +94 -0
  12. bitstring-4.2.0/bitstring/dtypes.py +397 -0
  13. {bitstring-4.1.3 → bitstring-4.2.0}/bitstring/exceptions.py +5 -4
  14. bitstring-4.2.0/bitstring/fp8.py +97 -0
  15. bitstring-4.2.0/bitstring/luts.py +248 -0
  16. {bitstring-4.1.3 → bitstring-4.2.0}/bitstring/methods.py +9 -19
  17. bitstring-4.2.0/bitstring/mxfp.py +200 -0
  18. bitstring-4.2.0/bitstring/utils.py +231 -0
  19. {bitstring-4.1.3 → bitstring-4.2.0/bitstring.egg-info}/PKG-INFO +25 -32
  20. {bitstring-4.1.3 → bitstring-4.2.0}/bitstring.egg-info/SOURCES.txt +8 -3
  21. bitstring-4.2.0/bitstring.egg-info/requires.txt +1 -0
  22. {bitstring-4.1.3 → bitstring-4.2.0}/pyproject.toml +4 -4
  23. {bitstring-4.1.3 → bitstring-4.2.0}/release_notes.txt +62 -2
  24. bitstring-4.2.0/tests/test_array.py +929 -0
  25. bitstring-4.2.0/tests/test_bitarray.py +979 -0
  26. bitstring-4.2.0/tests/test_bits.py +981 -0
  27. bitstring-4.2.0/tests/test_bitstore.py +94 -0
  28. bitstring-4.2.0/tests/test_bitstream.py +4172 -0
  29. bitstring-4.2.0/tests/test_bitstring.py +200 -0
  30. bitstring-4.2.0/tests/test_constbitstream.py +265 -0
  31. bitstring-4.2.0/tests/test_dtypes.py +134 -0
  32. bitstring-4.2.0/tests/test_fp8.py +295 -0
  33. bitstring-4.2.0/tests/test_mxfp.py +441 -0
  34. bitstring-4.2.0/tests/test_scaled_dtypes.py +13 -0
  35. bitstring-4.1.3/bitstring/__init__.py +0 -199
  36. bitstring-4.1.3/bitstring/bits.py +0 -2091
  37. bitstring-4.1.3/bitstring/bitstore.py +0 -165
  38. bitstring-4.1.3/bitstring/bitstore_helpers.py +0 -333
  39. bitstring-4.1.3/bitstring/dtypes.py +0 -118
  40. bitstring-4.1.3/bitstring/fp8.py +0 -93
  41. bitstring-4.1.3/bitstring/options.py +0 -72
  42. bitstring-4.1.3/bitstring/utils.py +0 -273
  43. bitstring-4.1.3/bitstring.egg-info/requires.txt +0 -1
  44. bitstring-4.1.3/tests/test_array.py +0 -880
  45. bitstring-4.1.3/tests/test_bitarray.py +0 -973
  46. bitstring-4.1.3/tests/test_bits.py +0 -836
  47. bitstring-4.1.3/tests/test_bitstore.py +0 -94
  48. bitstring-4.1.3/tests/test_bitstream.py +0 -4181
  49. bitstring-4.1.3/tests/test_bitstring.py +0 -179
  50. bitstring-4.1.3/tests/test_constbitstream.py +0 -249
  51. bitstring-4.1.3/tests/test_fp8.py +0 -220
  52. {bitstring-4.1.3 → bitstring-4.2.0}/LICENSE +0 -0
  53. {bitstring-4.1.3 → bitstring-4.2.0}/MANIFEST.in +0 -0
  54. {bitstring-4.1.3 → bitstring-4.2.0}/bitstring/py.typed +0 -0
  55. {bitstring-4.1.3 → bitstring-4.2.0}/bitstring.egg-info/dependency_links.txt +0 -0
  56. {bitstring-4.1.3 → bitstring-4.2.0}/bitstring.egg-info/top_level.txt +0 -0
  57. {bitstring-4.1.3 → bitstring-4.2.0}/setup.cfg +0 -0
  58. {bitstring-4.1.3 → bitstring-4.2.0}/tests/__init__.py +0 -0
  59. {bitstring-4.1.3 → bitstring-4.2.0}/tests/smalltestfile +0 -0
  60. {bitstring-4.1.3 → bitstring-4.2.0}/tests/test.m1v +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: bitstring
3
- Version: 4.1.3
3
+ Version: 4.2.0
4
4
  Summary: Simple construction, analysis and modification of binary data.
5
5
  Author-email: Scott Griffiths <dr.scottgriffiths@gmail.com>
6
6
  Project-URL: homepage, https://github.com/scott-griffiths/bitstring
@@ -10,42 +10,50 @@ Classifier: Development Status :: 5 - Production/Stable
10
10
  Classifier: Intended Audience :: Developers
11
11
  Classifier: Operating System :: OS Independent
12
12
  Classifier: Programming Language :: Python :: 3
13
- Classifier: Programming Language :: Python :: 3.7
14
13
  Classifier: Programming Language :: Python :: 3.8
15
14
  Classifier: Programming Language :: Python :: 3.9
16
15
  Classifier: Programming Language :: Python :: 3.10
17
16
  Classifier: Programming Language :: Python :: 3.11
17
+ Classifier: Programming Language :: Python :: 3.12
18
18
  Classifier: License :: OSI Approved :: MIT License
19
19
  Classifier: Topic :: Software Development :: Libraries :: Python Modules
20
- Requires-Python: >=3.7
20
+ Requires-Python: >=3.8
21
21
  Description-Content-Type: text/markdown
22
22
  License-File: LICENSE
23
- Requires-Dist: bitarray<3.0.0,>=2.8.0
23
+ Requires-Dist: bitarray<3.0.0,>=2.9.0
24
24
 
25
25
 
26
26
 
27
27
  ![bitstring](https://raw.githubusercontent.com/scott-griffiths/bitstring/main/doc/bitstring_logo_small.png "bitstring")
28
28
 
29
- **bitstring** is a Python module to help make the creation and analysis of binary data as simple and efficient as possible.
29
+ **bitstring** is a Python module to help make the creation and analysis of all types of bit-level binary data as simple and efficient as possible.
30
30
 
31
-
32
- It has been maintained since 2006 and now has many millions of downloads per year.
31
+ It has been actively maintained since 2006.
33
32
 
34
33
 
35
34
 
36
35
  [![CI badge](https://github.com/scott-griffiths/bitstring/actions/workflows/.github/workflows/ci.yml/badge.svg)](https://github.com/scott-griffiths/bitstring/actions/workflows/ci.yml)
37
- [![Docs](https://img.shields.io/readthedocs/bitstring)](https://bitstring.readthedocs.io/en/latest/)
38
- [![Codacy Badge](https://img.shields.io/codacy/grade/8869499b2eed44548fa1a5149dd451f4)](https://app.codacy.com/gh/scott-griffiths/bitstring/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
39
- [![Downloads](https://img.shields.io/pypi/dm/bitstring?color=blue)](https://pypistats.org/packages/bitstring) &nbsp; &nbsp;
40
- [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/scott-griffiths/bitstring/main?labpath=doc%2Fwalkthrough.ipynb)
36
+ [![Docs](https://img.shields.io/readthedocs/bitstring?logo=readthedocs&logoColor=white)](https://bitstring.readthedocs.io/en/latest/)
37
+ [![Codacy Badge](https://img.shields.io/codacy/grade/8869499b2eed44548fa1a5149dd451f4?logo=codacy)](https://app.codacy.com/gh/scott-griffiths/bitstring/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
38
+ [![Dependents (via libraries.io)](https://img.shields.io/librariesio/dependents/pypi/bitstring?logo=libraries.io&logoColor=white)](https://libraries.io/pypi/bitstring)
39
+ &nbsp; &nbsp;
40
+ [![Pepy Total Downlods](https://img.shields.io/pepy/dt/bitstring?logo=python&logoColor=white&labelColor=blue&color=blue)](https://www.pepy.tech/projects/bitstring)
41
+ [![PyPI - Downloads](https://img.shields.io/pypi/dm/bitstring?label=%40&labelColor=blue&color=blue)](https://pypistats.org/packages/bitstring)
42
+
41
43
 
42
44
  News
43
45
  ----
44
- **November 2023**: bitstring 4.1.3 released. Version 4.1 is a large update in terms of how much of the code has changed.
46
+ **April 2024**: bitstring 4.2.0 released.
47
+
48
+ New in version 4.2:
45
49
 
46
- * Speed increased with bitarray dependency.
47
- * New Array class for homogeneous data.
48
- * Support for 8-bit floating point values.
50
+ * Dropped support for Python 3.7. Minimum version is now 3.8.
51
+ * A new `Dtype` class can be optionally used to specify types.
52
+ * The `bitstring.options` object is now the preferred method for changing module options.
53
+ * New `fromstring` method as another way to create bitstrings from formatted strings.
54
+ * More types can now be pretty printed.
55
+ * A range of 8-bit, 6-bit and even 4-bit floating point formats added (beta):
56
+ * Performance improvements.
49
57
 
50
58
  See the [release notes](https://github.com/scott-griffiths/bitstring/blob/main/release_notes.txt) for details. Please let me know if you encounter any problems.
51
59
 
@@ -71,7 +79,7 @@ Some starting points are given below:
71
79
  * [Quick Reference](https://bitstring.readthedocs.io/en/stable/quick_reference.html)
72
80
  * [Full Reference](https://bitstring.readthedocs.io/en/stable/reference.html)
73
81
 
74
- You can also try out the interactive walkthrough notebook on [binder](https://mybinder.org/v2/gh/scott-griffiths/bitstring/main?labpath=doc%2Fwalkthrough.ipynb).
82
+ There is also an introductory walkthrough notebook on [binder](https://mybinder.org/v2/gh/scott-griffiths/bitstring/main?labpath=doc%2Fwalkthrough.ipynb).
75
83
 
76
84
  Release Notes
77
85
  -------------
@@ -135,20 +143,5 @@ Examples
135
143
  Array('uint7', [45, 100, 15, 1])
136
144
 
137
145
 
138
- Unit Tests
139
- ----------
140
-
141
- The 700+ unit tests should all pass. They can be run from the root of the project with
142
-
143
- python -m unittest
144
-
145
-
146
- Credits
147
- -------
148
-
149
- Created by Scott Griffiths in 2006 to help with ad hoc parsing and creation of compressed video files.
150
- Maintained and expanded ever since as it became unexpectedly popular. Thanks to all those who have contributed ideas
151
- and code (and bug reports) over the years.
152
-
153
146
 
154
- <sub>Copyright (c) 2006 - 2023 Scott Griffiths</sub>
147
+ <sub>Copyright (c) 2006 - 2024 Scott Griffiths</sub>
@@ -2,26 +2,34 @@
2
2
 
3
3
  ![bitstring](https://raw.githubusercontent.com/scott-griffiths/bitstring/main/doc/bitstring_logo_small.png "bitstring")
4
4
 
5
- **bitstring** is a Python module to help make the creation and analysis of binary data as simple and efficient as possible.
5
+ **bitstring** is a Python module to help make the creation and analysis of all types of bit-level binary data as simple and efficient as possible.
6
6
 
7
-
8
- It has been maintained since 2006 and now has many millions of downloads per year.
7
+ It has been actively maintained since 2006.
9
8
 
10
9
 
11
10
 
12
11
  [![CI badge](https://github.com/scott-griffiths/bitstring/actions/workflows/.github/workflows/ci.yml/badge.svg)](https://github.com/scott-griffiths/bitstring/actions/workflows/ci.yml)
13
- [![Docs](https://img.shields.io/readthedocs/bitstring)](https://bitstring.readthedocs.io/en/latest/)
14
- [![Codacy Badge](https://img.shields.io/codacy/grade/8869499b2eed44548fa1a5149dd451f4)](https://app.codacy.com/gh/scott-griffiths/bitstring/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
15
- [![Downloads](https://img.shields.io/pypi/dm/bitstring?color=blue)](https://pypistats.org/packages/bitstring) &nbsp; &nbsp;
16
- [![Binder](https://mybinder.org/badge_logo.svg)](https://mybinder.org/v2/gh/scott-griffiths/bitstring/main?labpath=doc%2Fwalkthrough.ipynb)
12
+ [![Docs](https://img.shields.io/readthedocs/bitstring?logo=readthedocs&logoColor=white)](https://bitstring.readthedocs.io/en/latest/)
13
+ [![Codacy Badge](https://img.shields.io/codacy/grade/8869499b2eed44548fa1a5149dd451f4?logo=codacy)](https://app.codacy.com/gh/scott-griffiths/bitstring/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
14
+ [![Dependents (via libraries.io)](https://img.shields.io/librariesio/dependents/pypi/bitstring?logo=libraries.io&logoColor=white)](https://libraries.io/pypi/bitstring)
15
+ &nbsp; &nbsp;
16
+ [![Pepy Total Downlods](https://img.shields.io/pepy/dt/bitstring?logo=python&logoColor=white&labelColor=blue&color=blue)](https://www.pepy.tech/projects/bitstring)
17
+ [![PyPI - Downloads](https://img.shields.io/pypi/dm/bitstring?label=%40&labelColor=blue&color=blue)](https://pypistats.org/packages/bitstring)
18
+
17
19
 
18
20
  News
19
21
  ----
20
- **November 2023**: bitstring 4.1.3 released. Version 4.1 is a large update in terms of how much of the code has changed.
22
+ **April 2024**: bitstring 4.2.0 released.
23
+
24
+ New in version 4.2:
21
25
 
22
- * Speed increased with bitarray dependency.
23
- * New Array class for homogeneous data.
24
- * Support for 8-bit floating point values.
26
+ * Dropped support for Python 3.7. Minimum version is now 3.8.
27
+ * A new `Dtype` class can be optionally used to specify types.
28
+ * The `bitstring.options` object is now the preferred method for changing module options.
29
+ * New `fromstring` method as another way to create bitstrings from formatted strings.
30
+ * More types can now be pretty printed.
31
+ * A range of 8-bit, 6-bit and even 4-bit floating point formats added (beta):
32
+ * Performance improvements.
25
33
 
26
34
  See the [release notes](https://github.com/scott-griffiths/bitstring/blob/main/release_notes.txt) for details. Please let me know if you encounter any problems.
27
35
 
@@ -47,7 +55,7 @@ Some starting points are given below:
47
55
  * [Quick Reference](https://bitstring.readthedocs.io/en/stable/quick_reference.html)
48
56
  * [Full Reference](https://bitstring.readthedocs.io/en/stable/reference.html)
49
57
 
50
- You can also try out the interactive walkthrough notebook on [binder](https://mybinder.org/v2/gh/scott-griffiths/bitstring/main?labpath=doc%2Fwalkthrough.ipynb).
58
+ There is also an introductory walkthrough notebook on [binder](https://mybinder.org/v2/gh/scott-griffiths/bitstring/main?labpath=doc%2Fwalkthrough.ipynb).
51
59
 
52
60
  Release Notes
53
61
  -------------
@@ -111,20 +119,5 @@ Examples
111
119
  Array('uint7', [45, 100, 15, 1])
112
120
 
113
121
 
114
- Unit Tests
115
- ----------
116
-
117
- The 700+ unit tests should all pass. They can be run from the root of the project with
118
-
119
- python -m unittest
120
-
121
-
122
- Credits
123
- -------
124
-
125
- Created by Scott Griffiths in 2006 to help with ad hoc parsing and creation of compressed video files.
126
- Maintained and expanded ever since as it became unexpectedly popular. Thanks to all those who have contributed ideas
127
- and code (and bug reports) over the years.
128
-
129
122
 
130
- <sub>Copyright (c) 2006 - 2023 Scott Griffiths</sub>
123
+ <sub>Copyright (c) 2006 - 2024 Scott Griffiths</sub>
@@ -0,0 +1,313 @@
1
+ #!/usr/bin/env python
2
+ r"""
3
+ This package defines classes that simplify bit-wise creation, manipulation and
4
+ interpretation of data.
5
+
6
+ Classes:
7
+
8
+ Bits -- An immutable container for binary data.
9
+ BitArray -- A mutable container for binary data.
10
+ ConstBitStream -- An immutable container with streaming methods.
11
+ BitStream -- A mutable container with streaming methods.
12
+ Array -- An efficient list-like container where each item has a fixed-length binary format.
13
+ Dtype -- Encapsulate the data types used in the other classes.
14
+
15
+ Functions:
16
+
17
+ pack -- Create a BitStream from a format string.
18
+
19
+ Data:
20
+
21
+ options -- Module-wide options.
22
+
23
+ Exceptions:
24
+
25
+ Error -- Module exception base class.
26
+ CreationError -- Error during creation.
27
+ InterpretError -- Inappropriate interpretation of binary data.
28
+ ByteAlignError -- Whole byte position or length needed.
29
+ ReadError -- Reading or peeking past the end of a bitstring.
30
+
31
+ https://github.com/scott-griffiths/bitstring
32
+ """
33
+
34
+ __licence__ = """
35
+ The MIT License
36
+
37
+ Copyright (c) 2006 Scott Griffiths (dr.scottgriffiths@gmail.com)
38
+
39
+ Permission is hereby granted, free of charge, to any person obtaining a copy
40
+ of this software and associated documentation files (the "Software"), to deal
41
+ in the Software without restriction, including without limitation the rights
42
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
43
+ copies of the Software, and to permit persons to whom the Software is
44
+ furnished to do so, subject to the following conditions:
45
+
46
+ The above copyright notice and this permission notice shall be included in
47
+ all copies or substantial portions of the Software.
48
+
49
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
50
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
51
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
52
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
53
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
54
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
55
+ THE SOFTWARE.
56
+ """
57
+
58
+ __version__ = "4.2.0"
59
+
60
+ __author__ = "Scott Griffiths"
61
+
62
+ import sys
63
+
64
+ from .bits import Bits
65
+ from .bitstring_options import Options
66
+ from .bitarray_ import BitArray
67
+ from .bitstream import ConstBitStream, BitStream
68
+ from .methods import pack
69
+ from .array_ import Array
70
+ from .exceptions import Error, ReadError, InterpretError, ByteAlignError, CreationError
71
+ from .dtypes import DtypeDefinition, dtype_register, Dtype
72
+ import types
73
+ from typing import List, Tuple, Literal
74
+ from .mxfp import decompress_luts as mxfp_decompress_luts
75
+ from .fp8 import decompress_luts as binary8_decompress_luts
76
+
77
+ # Decompress the LUTs for the exotic floating point formats
78
+ mxfp_decompress_luts()
79
+ binary8_decompress_luts()
80
+
81
+ # The Options class returns a singleton.
82
+ options = Options()
83
+
84
+ # These get defined properly by the module magic below. This just stops mypy complaining about them.
85
+ bytealigned = lsb0 = None
86
+
87
+
88
+ # An opaque way of adding module level properties. Taken from https://peps.python.org/pep-0549/
89
+ # This is now deprecated. Use the options object directly instead.
90
+ class _MyModuleType(types.ModuleType):
91
+ @property
92
+ def bytealigned(self) -> bool:
93
+ """Determines whether a number of methods default to working only on byte boundaries."""
94
+ return options.bytealigned
95
+
96
+ @bytealigned.setter
97
+ def bytealigned(self, value: bool) -> None:
98
+ """Determines whether a number of methods default to working only on byte boundaries."""
99
+ options.bytealigned = value
100
+
101
+ @property
102
+ def lsb0(self) -> bool:
103
+ """If True, the least significant bit (the final bit) is indexed as bit zero."""
104
+ return options.lsb0
105
+
106
+ @lsb0.setter
107
+ def lsb0(self, value: bool) -> None:
108
+ """If True, the least significant bit (the final bit) is indexed as bit zero."""
109
+ options.lsb0 = value
110
+
111
+
112
+ sys.modules[__name__].__class__ = _MyModuleType
113
+
114
+ """These methods convert a bit length to the number of characters needed to print it for different interpretations."""
115
+ def hex_bits2chars(bitlength: int):
116
+ # One character for every 4 bits
117
+ return bitlength // 4
118
+
119
+ def oct_bits2chars(bitlength: int):
120
+ # One character for every 3 bits
121
+ return bitlength // 3
122
+
123
+ def bin_bits2chars(bitlength: int):
124
+ # One character for each bit
125
+ return bitlength
126
+
127
+ def bytes_bits2chars(bitlength: int):
128
+ # One character for every 8 bits
129
+ return bitlength // 8
130
+
131
+ def uint_bits2chars(bitlength: int):
132
+ # How many characters is largest possible int of this length?
133
+ return len(str((1 << bitlength) - 1))
134
+
135
+ def int_bits2chars(bitlength: int):
136
+ # How many characters is largest negative int of this length? (To include minus sign).
137
+ return len(str((-1 << (bitlength - 1))))
138
+
139
+ def float_bits2chars(bitlength: Literal[16, 32, 64]):
140
+ # These bit lengths were found by looking at lots of possible values
141
+ if bitlength in [16, 32]:
142
+ return 23 # Empirical value
143
+ else:
144
+ return 24 # Empirical value
145
+
146
+ def p3binary_bits2chars(bitlength: Literal[8]):
147
+ return 19 # Empirical value
148
+
149
+ def p4binary_bits2chars(bitlength: Literal[8]):
150
+ # Found by looking at all the possible values
151
+ return 13 # Empirical value
152
+
153
+ def e4m3mxfp_bits2chars(bitlength: Literal[8]):
154
+ return 13
155
+
156
+ def e5m2mxfp_bits2chars(bitlength: Literal[8]):
157
+ return 19
158
+
159
+ def e3m2mxfp_bits2chars(bitlength: Literal[6]):
160
+ # Not sure what the best value is here. It's 7 without considering the scale that could be applied.
161
+ return 7
162
+
163
+ def e2m3mxfp_bits2chars(bitlength: Literal[6]):
164
+ # Not sure what the best value is here.
165
+ return 7
166
+
167
+ def e2m1mxfp_bits2chars(bitlength: Literal[4]):
168
+ # Not sure what the best value is here.
169
+ return 7
170
+
171
+ def e8m0mxfp_bits2chars(bitlength: Literal[8]):
172
+ # Can range same as float32
173
+ return 23
174
+
175
+ def mxint_bits2chars(bitlength: Literal[8]):
176
+ # Not sure what the best value is here.
177
+ return 10
178
+
179
+
180
+ def bfloat_bits2chars(bitlength: Literal[16]):
181
+ # Found by looking at all the possible values
182
+ return 23 # Empirical value
183
+
184
+ def bits_bits2chars(bitlength: int):
185
+ # For bits type we can see how long it needs to be printed by trying any value
186
+ temp = Bits(bitlength)
187
+ return len(str(temp))
188
+
189
+ def bool_bits2chars(bitlength: Literal[1]):
190
+ # Bools are printed as 1 or 0, not True or False, so are one character each
191
+ return 1
192
+
193
+ dtype_definitions = [
194
+ # Integer types
195
+ DtypeDefinition('uint', Bits._setuint, Bits._getuint, int, False, uint_bits2chars,
196
+ description="a two's complement unsigned int"),
197
+ DtypeDefinition('uintle', Bits._setuintle, Bits._getuintle, int, False, uint_bits2chars,
198
+ allowed_lengths=(8, 16, 24, ...), description="a two's complement little-endian unsigned int"),
199
+ DtypeDefinition('uintbe', Bits._setuintbe, Bits._getuintbe, int, False, uint_bits2chars,
200
+ allowed_lengths=(8, 16, 24, ...), description="a two's complement big-endian unsigned int"),
201
+ DtypeDefinition('int', Bits._setint, Bits._getint, int, True, int_bits2chars,
202
+ description="a two's complement signed int"),
203
+ DtypeDefinition('intle', Bits._setintle, Bits._getintle, int, True, int_bits2chars,
204
+ allowed_lengths=(8, 16, 24, ...), description="a two's complement little-endian signed int"),
205
+ DtypeDefinition('intbe', Bits._setintbe, Bits._getintbe, int, True, int_bits2chars,
206
+ allowed_lengths=(8, 16, 24, ...), description="a two's complement big-endian signed int"),
207
+ # String types
208
+ DtypeDefinition('hex', Bits._sethex, Bits._gethex, str, False, hex_bits2chars,
209
+ allowed_lengths=(0, 4, 8, ...), description="a hexadecimal string"),
210
+ DtypeDefinition('bin', Bits._setbin_safe, Bits._getbin, str, False, bin_bits2chars,
211
+ description="a binary string"),
212
+ DtypeDefinition('oct', Bits._setoct, Bits._getoct, str, False, oct_bits2chars,
213
+ allowed_lengths=(0, 3, 6, ...), description="an octal string"),
214
+ # Float types
215
+ DtypeDefinition('float', Bits._setfloatbe, Bits._getfloatbe, float, True, float_bits2chars,
216
+ allowed_lengths=(16, 32, 64), description="a big-endian floating point number"),
217
+ DtypeDefinition('floatle', Bits._setfloatle, Bits._getfloatle, float, True, float_bits2chars,
218
+ allowed_lengths=(16, 32, 64), description="a little-endian floating point number"),
219
+ DtypeDefinition('bfloat', Bits._setbfloatbe, Bits._getbfloatbe, float, True, bfloat_bits2chars,
220
+ allowed_lengths=(16,), description="a 16 bit big-endian bfloat floating point number"),
221
+ DtypeDefinition('bfloatle', Bits._setbfloatle, Bits._getbfloatle, float, True, bfloat_bits2chars,
222
+ allowed_lengths=(16,), description="a 16 bit little-endian bfloat floating point number"),
223
+ # Other known length types
224
+ DtypeDefinition('bits', Bits._setbits, Bits._getbits, Bits, False, bits_bits2chars,
225
+ description="a bitstring object"),
226
+ DtypeDefinition('bool', Bits._setbool, Bits._getbool, bool, False, bool_bits2chars,
227
+ allowed_lengths=(1,), description="a bool (True or False)"),
228
+ DtypeDefinition('bytes', Bits._setbytes, Bits._getbytes, bytes, False, bytes_bits2chars,
229
+ multiplier=8, description="a bytes object"),
230
+ # Unknown length types
231
+ DtypeDefinition('se', Bits._setse, Bits._getse, int, True, None,
232
+ variable_length=True, description="a signed exponential-Golomb code"),
233
+ DtypeDefinition('ue', Bits._setue, Bits._getue, int, False, None,
234
+ variable_length=True, description="an unsigned exponential-Golomb code"),
235
+ DtypeDefinition('sie', Bits._setsie, Bits._getsie, int, True, None,
236
+ variable_length=True, description="a signed interleaved exponential-Golomb code"),
237
+ DtypeDefinition('uie', Bits._setuie, Bits._getuie, int, False, None,
238
+ variable_length=True, description="an unsigned interleaved exponential-Golomb code"),
239
+ # Special case pad type
240
+ DtypeDefinition('pad', Bits._setpad, Bits._getpad, None, False, None,
241
+ description="a skipped section of padding"),
242
+
243
+ # MXFP and IEEE 8-bit float types
244
+ DtypeDefinition('p3binary', Bits._setp3binary, Bits._getp3binary, float, True, p3binary_bits2chars,
245
+ allowed_lengths=(8,), description="an 8 bit float with binary8p3 format"),
246
+ DtypeDefinition('p4binary', Bits._setp4binary, Bits._getp4binary, float, True, p4binary_bits2chars,
247
+ allowed_lengths=(8,), description="an 8 bit float with binary8p4 format"),
248
+ DtypeDefinition('e4m3mxfp', Bits._sete4m3mxfp, Bits._gete4m3mxfp, float, True, e4m3mxfp_bits2chars,
249
+ allowed_lengths=(8,), description="an 8 bit float with MXFP E4M3 format"),
250
+ DtypeDefinition('e5m2mxfp', Bits._sete5m2mxfp, Bits._gete5m2mxfp, float, True, e5m2mxfp_bits2chars,
251
+ allowed_lengths=(8,), description="an 8 bit float with MXFP E5M2 format"),
252
+ DtypeDefinition('e3m2mxfp', Bits._sete3m2mxfp, Bits._gete3m2mxfp, float, True, e3m2mxfp_bits2chars,
253
+ allowed_lengths=(6,), description="a 6 bit float with MXFP E3M2 format"),
254
+ DtypeDefinition('e2m3mxfp', Bits._sete2m3mxfp, Bits._gete2m3mxfp, float, True, e2m3mxfp_bits2chars,
255
+ allowed_lengths=(6,), description="a 6 bit float with MXFP E2M3 format"),
256
+ DtypeDefinition('e2m1mxfp', Bits._sete2m1mxfp, Bits._gete2m1mxfp, float, True, e2m1mxfp_bits2chars,
257
+ allowed_lengths=(4,), description="a 4 bit float with MXFP E2M1 format"),
258
+ DtypeDefinition('e8m0mxfp', Bits._sete8m0mxfp, Bits._gete8m0mxfp, float, False, e8m0mxfp_bits2chars,
259
+ allowed_lengths=(8,), description="an 8 bit float with MXFP E8M0 format"),
260
+ DtypeDefinition('mxint', Bits._setmxint, Bits._getmxint, float, True, mxint_bits2chars,
261
+ allowed_lengths=(8,), description="an 8 bit float with MXFP INT8 format"),
262
+ ]
263
+
264
+
265
+ aliases: List[Tuple[str, str]] = [
266
+ # Floats default to big endian
267
+ ('float', 'floatbe'),
268
+ ('bfloat', 'bfloatbe'),
269
+
270
+ # Some single letter aliases for popular types
271
+ ('int', 'i'),
272
+ ('uint', 'u'),
273
+ ('hex', 'h'),
274
+ ('oct', 'o'),
275
+ ('bin', 'b'),
276
+ ('float', 'f'),
277
+ ]
278
+
279
+ # Create native-endian aliases depending on the byteorder of the system
280
+ byteorder: str = sys.byteorder
281
+ if byteorder == 'little':
282
+ aliases.extend([
283
+ ('uintle', 'uintne'),
284
+ ('intle', 'intne'),
285
+ ('floatle', 'floatne'),
286
+ ('bfloatle', 'bfloatne'),
287
+ ])
288
+ else:
289
+ aliases.extend([
290
+ ('uintbe', 'uintne'),
291
+ ('intbe', 'intne'),
292
+ ('floatbe', 'floatne'),
293
+ ('bfloatbe', 'bfloatne'),
294
+ ])
295
+
296
+
297
+ for dt in dtype_definitions:
298
+ dtype_register.add_dtype(dt)
299
+ for alias in aliases:
300
+ dtype_register.add_dtype_alias(alias[0], alias[1])
301
+
302
+ property_docstrings = [f'{name} -- Interpret as {dtype_register[name].description}.' for name in dtype_register.names]
303
+ property_docstring = '\n '.join(property_docstrings)
304
+
305
+ Bits.__doc__ = Bits.__doc__.replace('[GENERATED_PROPERTY_DESCRIPTIONS]', property_docstring)
306
+ BitArray.__doc__ = BitArray.__doc__.replace('[GENERATED_PROPERTY_DESCRIPTIONS]', property_docstring)
307
+ ConstBitStream.__doc__ = ConstBitStream.__doc__.replace('[GENERATED_PROPERTY_DESCRIPTIONS]', property_docstring)
308
+ BitStream.__doc__ = BitStream.__doc__.replace('[GENERATED_PROPERTY_DESCRIPTIONS]', property_docstring)
309
+
310
+
311
+ __all__ = ['ConstBitStream', 'BitStream', 'BitArray', 'Array',
312
+ 'Bits', 'pack', 'Error', 'ReadError', 'InterpretError',
313
+ 'ByteAlignError', 'CreationError', 'bytealigned', 'lsb0', 'Dtype', 'options']
@@ -29,14 +29,14 @@ $ python -m bitstring hex=01, uint:12=352.hex
29
29
  01160
30
30
  """)
31
31
  return
32
- if fp in dtype_register.name_to_meta_dtype:
32
+ if fp in dtype_register.names:
33
33
  # concatenate all other parameters and interpret using the final one
34
34
  b1 = Bits(','.join(sys.argv[1: -1]))
35
35
  print(b1._readtoken(fp, 0, b1.__len__())[0])
36
36
  else:
37
37
  # does final parameter end with a dot then an interpretation string?
38
38
  interp = fp[fp.rfind('.') + 1:]
39
- if interp in dtype_register.name_to_meta_dtype:
39
+ if interp in dtype_register.names:
40
40
  sys.argv[-1] = fp[:fp.rfind('.')]
41
41
  b1 = Bits(','.join(sys.argv[1:]))
42
42
  print(b1._readtoken(interp, 0, b1.__len__())[0])