bitarray 3.12.2__cp313-cp313-pyemscripten_2025_0_wasm32.whl
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.
- bitarray/__init__.py +66 -0
- bitarray/__init__.pyi +176 -0
- bitarray/_bitarray.cpython-313-wasm32-emscripten.so +0 -0
- bitarray/_util.cpython-313-wasm32-emscripten.so +0 -0
- bitarray/bitarray.h +357 -0
- bitarray/bitfields.py +447 -0
- bitarray/py.typed +0 -0
- bitarray/pythoncapi_compat.h +3138 -0
- bitarray/test_281.pickle +0 -0
- bitarray/test_bitarray.py +5788 -0
- bitarray/test_bitfields.py +721 -0
- bitarray/test_free_threading.py +840 -0
- bitarray/test_util.py +3166 -0
- bitarray/util.py +694 -0
- bitarray/util.pyi +86 -0
- bitarray-3.12.2.dist-info/METADATA +1134 -0
- bitarray-3.12.2.dist-info/RECORD +20 -0
- bitarray-3.12.2.dist-info/WHEEL +5 -0
- bitarray-3.12.2.dist-info/licenses/LICENSE +46 -0
- bitarray-3.12.2.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,1134 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: bitarray
|
|
3
|
+
Version: 3.12.2
|
|
4
|
+
Summary: efficient arrays of booleans -- C extension
|
|
5
|
+
Home-page: https://github.com/ilanschnell/bitarray
|
|
6
|
+
Author: Ilan Schnell
|
|
7
|
+
Author-email: ilanschnell@gmail.com
|
|
8
|
+
License: PSF-2.0
|
|
9
|
+
Classifier: Development Status :: 6 - Mature
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: C
|
|
13
|
+
Classifier: Programming Language :: Python :: 3
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.7
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
23
|
+
Classifier: Programming Language :: Python :: Free Threading :: 2 - Beta
|
|
24
|
+
Classifier: Topic :: Utilities
|
|
25
|
+
Requires-Python: >=3.7
|
|
26
|
+
License-File: LICENSE
|
|
27
|
+
Dynamic: author
|
|
28
|
+
Dynamic: author-email
|
|
29
|
+
Dynamic: classifier
|
|
30
|
+
Dynamic: description
|
|
31
|
+
Dynamic: home-page
|
|
32
|
+
Dynamic: license
|
|
33
|
+
Dynamic: license-file
|
|
34
|
+
Dynamic: requires-python
|
|
35
|
+
Dynamic: summary
|
|
36
|
+
|
|
37
|
+
bitarray: efficient arrays of booleans
|
|
38
|
+
======================================
|
|
39
|
+
|
|
40
|
+
This library provides an object type which efficiently represents an array
|
|
41
|
+
of booleans. Bitarrays are sequence types and behave very much like usual
|
|
42
|
+
lists. Eight bits are represented by one byte in a contiguous block of
|
|
43
|
+
memory. The user can select between two representations: little-endian
|
|
44
|
+
and big-endian. All functionality is implemented in C.
|
|
45
|
+
Methods for accessing the machine representation are provided, including the
|
|
46
|
+
ability to import and export buffers. This allows creating bitarrays that
|
|
47
|
+
are mapped to other objects, including memory-mapped files.
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
Key features
|
|
51
|
+
------------
|
|
52
|
+
|
|
53
|
+
* The bit-endianness can be specified for each bitarray object, see below.
|
|
54
|
+
* Sequence methods: slicing (including slice assignment and deletion),
|
|
55
|
+
operations ``+``, ``*``, ``+=``, ``*=``, the ``in`` operator, ``len()``
|
|
56
|
+
* Bitwise operations: ``~``, ``&``, ``|``, ``^``, ``<<``, ``>>`` (as well as
|
|
57
|
+
their in-place versions ``&=``, ``|=``, ``^=``, ``<<=``, ``>>=``).
|
|
58
|
+
* `Free-threading support <https://github.com/ilanschnell/bitarray/blob/master/doc/free_threading.rst>`__
|
|
59
|
+
(for GIL-disabled CPython 3.14 and later)
|
|
60
|
+
* Fast methods for encoding and decoding variable-length
|
|
61
|
+
`prefix codes <https://github.com/ilanschnell/bitarray/blob/master/doc/prefix_codes.rst>`__.
|
|
62
|
+
* Bitarray objects support the `buffer protocol <https://github.com/ilanschnell/bitarray/blob/master/doc/buffer.rst>`__
|
|
63
|
+
(both importing and exporting buffers).
|
|
64
|
+
* Efficient packing and unpacking for interoperability with byte-oriented
|
|
65
|
+
objects such as `NumPy arrays <https://github.com/ilanschnell/bitarray/blob/master/doc/ndarray.rst>`__.
|
|
66
|
+
* Pickling and unpickling of bitarray objects.
|
|
67
|
+
* Immutable ``frozenbitarray`` objects which are hashable
|
|
68
|
+
* Fast searching for bits and bit patterns
|
|
69
|
+
* ``bitfields`` sub-module for packing and unpacking fixed-width
|
|
70
|
+
`bit-field structures <https://github.com/ilanschnell/bitarray/blob/master/doc/bitfields.rst>`__
|
|
71
|
+
* Type hinting
|
|
72
|
+
* Extensive test suite with over 700 unit tests
|
|
73
|
+
* Utility module ``bitarray.util``:
|
|
74
|
+
|
|
75
|
+
* conversion to and from hexadecimal strings
|
|
76
|
+
* generating random bitarrays
|
|
77
|
+
* pretty printing
|
|
78
|
+
* conversion to and from integers
|
|
79
|
+
* creating Huffman codes
|
|
80
|
+
* `compression of sparse bitarrays <https://github.com/ilanschnell/bitarray/blob/master/doc/sparse_compression.rst>`__
|
|
81
|
+
* run-length encoding
|
|
82
|
+
* serialization and deserialization
|
|
83
|
+
* various count functions
|
|
84
|
+
* other helpful functions
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
Installation
|
|
88
|
+
------------
|
|
89
|
+
|
|
90
|
+
Python wheels are are available on PyPI for all major platforms and Python
|
|
91
|
+
versions. Which means you can simply:
|
|
92
|
+
|
|
93
|
+
.. code-block:: shell-session
|
|
94
|
+
|
|
95
|
+
$ pip install bitarray
|
|
96
|
+
|
|
97
|
+
Once you have installed the package, you may want to test it:
|
|
98
|
+
|
|
99
|
+
.. code-block:: shell-session
|
|
100
|
+
|
|
101
|
+
$ python -c 'import bitarray; bitarray.test()'
|
|
102
|
+
bitarray is installed in: /Users/ilan/bitarray/bitarray
|
|
103
|
+
sys.prefix: /Users/ilan/miniforge
|
|
104
|
+
bitarray version: 3.12.2
|
|
105
|
+
sys.version: 3.15.0 (main, Oct 9 2026) [Clang 21.1.8]
|
|
106
|
+
sys.abiflags: ''
|
|
107
|
+
sys._is_gil_enabled(): True
|
|
108
|
+
pointer size: 64 bit
|
|
109
|
+
sizeof(size_t): 8
|
|
110
|
+
sizeof(bitarrayobject): 80
|
|
111
|
+
HAVE_BUILTIN_BSWAP64: 1
|
|
112
|
+
default bit-endianness: big
|
|
113
|
+
machine byte-order: little
|
|
114
|
+
Py_GIL_DISABLED: 0
|
|
115
|
+
Py_DEBUG: 0
|
|
116
|
+
DEBUG: 0
|
|
117
|
+
.....................................s...................................
|
|
118
|
+
..........s.....................................s........................
|
|
119
|
+
......s.........................................................
|
|
120
|
+
----------------------------------------------------------------------
|
|
121
|
+
Ran 711 tests in 0.161s
|
|
122
|
+
|
|
123
|
+
OK (skipped=4)
|
|
124
|
+
|
|
125
|
+
The ``test()`` function is part of the API. It will return
|
|
126
|
+
a ``unittest.runner.TextTestResult`` object, such that one can verify that
|
|
127
|
+
all tests ran successfully by:
|
|
128
|
+
|
|
129
|
+
.. code-block:: python
|
|
130
|
+
|
|
131
|
+
import bitarray
|
|
132
|
+
assert bitarray.test().wasSuccessful()
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
Usage
|
|
136
|
+
-----
|
|
137
|
+
|
|
138
|
+
As mentioned above, bitarray objects behave very much like lists, so
|
|
139
|
+
there is not too much to learn. The biggest difference from list
|
|
140
|
+
objects (except that bitarray are obviously homogeneous) is the ability
|
|
141
|
+
to access the machine representation of the object.
|
|
142
|
+
When doing so, the bit-endianness is of importance; this issue is
|
|
143
|
+
explained in detail in the section below. Here, we demonstrate the
|
|
144
|
+
basic usage of bitarray objects:
|
|
145
|
+
|
|
146
|
+
.. code-block:: python
|
|
147
|
+
|
|
148
|
+
>>> from bitarray import bitarray
|
|
149
|
+
>>> a = bitarray() # create empty bitarray
|
|
150
|
+
>>> a.append(1)
|
|
151
|
+
>>> a.extend([1, 0])
|
|
152
|
+
>>> a
|
|
153
|
+
bitarray('110')
|
|
154
|
+
>>> x = bitarray(2 ** 20) # bitarray of length 1048576 (initialized to 0)
|
|
155
|
+
>>> len(x)
|
|
156
|
+
1048576
|
|
157
|
+
>>> bitarray('1001 011') # initialize from string (whitespace is ignored)
|
|
158
|
+
bitarray('1001011')
|
|
159
|
+
>>> lst = [1, 0, False, True, True]
|
|
160
|
+
>>> a = bitarray(lst) # initialize from iterable
|
|
161
|
+
>>> a
|
|
162
|
+
bitarray('10011')
|
|
163
|
+
>>> a[2] # indexing a single item will always return an integer
|
|
164
|
+
0
|
|
165
|
+
>>> a[2:4] # whereas indexing a slice will always return a bitarray
|
|
166
|
+
bitarray('01')
|
|
167
|
+
>>> a[2:3] # even when the slice length is just one
|
|
168
|
+
bitarray('0')
|
|
169
|
+
>>> a.count(1)
|
|
170
|
+
3
|
|
171
|
+
>>> a.remove(0) # removes first occurrence of 0
|
|
172
|
+
>>> a
|
|
173
|
+
bitarray('1011')
|
|
174
|
+
|
|
175
|
+
Like lists, bitarray objects support slice assignment and deletion:
|
|
176
|
+
|
|
177
|
+
.. code-block:: python
|
|
178
|
+
|
|
179
|
+
>>> a = bitarray(50)
|
|
180
|
+
>>> a.setall(0) # set all elements in a to 0
|
|
181
|
+
>>> a[11:37:3] = 9 * bitarray('1')
|
|
182
|
+
>>> a
|
|
183
|
+
bitarray('00000000000100100100100100100100100100000000000000')
|
|
184
|
+
>>> del a[12::3]
|
|
185
|
+
>>> a
|
|
186
|
+
bitarray('0000000000010101010101010101000000000')
|
|
187
|
+
>>> a[-6:] = bitarray('10011')
|
|
188
|
+
>>> a
|
|
189
|
+
bitarray('000000000001010101010101010100010011')
|
|
190
|
+
>>> a += bitarray('000111')
|
|
191
|
+
>>> a[9:]
|
|
192
|
+
bitarray('001010101010101010100010011000111')
|
|
193
|
+
|
|
194
|
+
In addition, slices can be assigned to booleans, which is easier (and
|
|
195
|
+
faster) than assigning to a bitarray in which all values are the same:
|
|
196
|
+
|
|
197
|
+
.. code-block:: python
|
|
198
|
+
|
|
199
|
+
>>> a = 20 * bitarray('0')
|
|
200
|
+
>>> a[1:15:3] = True
|
|
201
|
+
>>> a
|
|
202
|
+
bitarray('01001001001001000000')
|
|
203
|
+
|
|
204
|
+
This is easier and faster than:
|
|
205
|
+
|
|
206
|
+
.. code-block:: python
|
|
207
|
+
|
|
208
|
+
>>> a = 20 * bitarray('0')
|
|
209
|
+
>>> a[1:15:3] = 5 * bitarray('1')
|
|
210
|
+
>>> a
|
|
211
|
+
bitarray('01001001001001000000')
|
|
212
|
+
|
|
213
|
+
Note that in the latter we have to create a temporary bitarray whose length
|
|
214
|
+
must be known or calculated. Another example of assigning slices to Booleans,
|
|
215
|
+
is setting ranges:
|
|
216
|
+
|
|
217
|
+
.. code-block:: python
|
|
218
|
+
|
|
219
|
+
>>> a = bitarray(30)
|
|
220
|
+
>>> a[:] = 0 # set all elements to 0 - equivalent to a.setall(0)
|
|
221
|
+
>>> a[10:25] = 1 # set elements in range(10, 25) to 1
|
|
222
|
+
>>> a
|
|
223
|
+
bitarray('000000000011111111111111100000')
|
|
224
|
+
|
|
225
|
+
As of bitarray version 2.8, indices may also be lists of arbitrary
|
|
226
|
+
indices (like in NumPy), or bitarrays that are treated as masks,
|
|
227
|
+
see `Bitarray indexing <https://github.com/ilanschnell/bitarray/blob/master/doc/indexing.rst>`__.
|
|
228
|
+
|
|
229
|
+
|
|
230
|
+
Bitwise operators
|
|
231
|
+
-----------------
|
|
232
|
+
|
|
233
|
+
Bitarray objects support the bitwise operators ``~``, ``&``, ``|``, ``^``,
|
|
234
|
+
``<<``, ``>>`` (as well as their in-place versions ``&=``, ``|=``, ``^=``,
|
|
235
|
+
``<<=``, ``>>=``). The behavior is very much what one would expect:
|
|
236
|
+
|
|
237
|
+
.. code-block:: python
|
|
238
|
+
|
|
239
|
+
>>> a = bitarray('101110001')
|
|
240
|
+
>>> ~a # invert
|
|
241
|
+
bitarray('010001110')
|
|
242
|
+
>>> b = bitarray('111001011')
|
|
243
|
+
>>> a ^ b # bitwise XOR
|
|
244
|
+
bitarray('010111010')
|
|
245
|
+
>>> a &= b # inplace AND
|
|
246
|
+
>>> a
|
|
247
|
+
bitarray('101000001')
|
|
248
|
+
>>> a <<= 2 # in-place left-shift by 2
|
|
249
|
+
>>> a
|
|
250
|
+
bitarray('100000100')
|
|
251
|
+
>>> b >> 1 # return b right-shifted by 1
|
|
252
|
+
bitarray('011100101')
|
|
253
|
+
|
|
254
|
+
The C language does not specify the behavior of negative shifts and
|
|
255
|
+
of left shifts larger or equal than the width of the promoted left operand.
|
|
256
|
+
The exact behavior is compiler/machine specific.
|
|
257
|
+
This Python bitarray library specifies the behavior as follows:
|
|
258
|
+
|
|
259
|
+
* the length of the bitarray is never changed by any shift operation
|
|
260
|
+
* blanks are filled by 0
|
|
261
|
+
* negative shifts raise ``ValueError``
|
|
262
|
+
* shifts larger or equal to the length of the bitarray result in
|
|
263
|
+
bitarrays with all values 0
|
|
264
|
+
|
|
265
|
+
It is worth noting that (regardless of bit-endianness) the bitarray left
|
|
266
|
+
shift (``<<``) always shifts towards lower indices, and the right
|
|
267
|
+
shift (``>>``) always shifts towards higher indices.
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
Bit-endianness
|
|
271
|
+
--------------
|
|
272
|
+
|
|
273
|
+
For many purposes the bit-endianness is not of any relevance to the end user
|
|
274
|
+
and can be regarded as an implementation detail of bitarray objects.
|
|
275
|
+
However, there are use cases when the bit-endianness becomes important.
|
|
276
|
+
These use cases involve explicitly reading and writing the bitarray buffer
|
|
277
|
+
using ``.tobytes()``, ``.frombytes()``, ``.tofile()`` or ``.fromfile()``,
|
|
278
|
+
importing and exporting buffers. Also, a number of utility functions
|
|
279
|
+
in ``bitarray.util`` will return different results depending on
|
|
280
|
+
bit-endianness, such as ``ba2hex()`` or ``ba2int``.
|
|
281
|
+
To better understand this topic, please read `bit-endianness <https://github.com/ilanschnell/bitarray/blob/master/doc/endianness.rst>`__.
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
Buffer protocol
|
|
285
|
+
---------------
|
|
286
|
+
|
|
287
|
+
Bitarray objects support the buffer protocol. They can both export their
|
|
288
|
+
own buffer, as well as import another object's buffer. To learn more about
|
|
289
|
+
this topic, please read `buffer protocol <https://github.com/ilanschnell/bitarray/blob/master/doc/buffer.rst>`__. There is also an example that shows how
|
|
290
|
+
to memory-map a file to a bitarray: `mmapped-file.py <https://github.com/ilanschnell/bitarray/blob/master/examples/mmapped-file.py>`__
|
|
291
|
+
|
|
292
|
+
|
|
293
|
+
Frozenbitarrays
|
|
294
|
+
---------------
|
|
295
|
+
|
|
296
|
+
A ``frozenbitarray`` object is very similar to the bitarray object.
|
|
297
|
+
The difference is that this a ``frozenbitarray`` is immutable, and hashable,
|
|
298
|
+
and can therefore be used as a dictionary key:
|
|
299
|
+
|
|
300
|
+
.. code-block:: python
|
|
301
|
+
|
|
302
|
+
>>> from bitarray import frozenbitarray
|
|
303
|
+
>>> key = frozenbitarray('1100011')
|
|
304
|
+
>>> {key: 'some value'}
|
|
305
|
+
{frozenbitarray('1100011'): 'some value'}
|
|
306
|
+
>>> key[3] = 1
|
|
307
|
+
Traceback (most recent call last):
|
|
308
|
+
...
|
|
309
|
+
TypeError: frozenbitarray is immutable
|
|
310
|
+
|
|
311
|
+
|
|
312
|
+
Reference
|
|
313
|
+
=========
|
|
314
|
+
|
|
315
|
+
bitarray version: 3.12.2 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
|
|
316
|
+
|
|
317
|
+
In the following, ``item`` and ``value`` are usually a single bit -
|
|
318
|
+
an integer 0 or 1.
|
|
319
|
+
|
|
320
|
+
Also, ``sub_bitarray`` refers to either a bitarray, or an ``item``.
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
The bitarray object:
|
|
324
|
+
--------------------
|
|
325
|
+
|
|
326
|
+
``bitarray(initializer=0, /, endian='big', buffer=None)`` -> bitarray
|
|
327
|
+
Return a new bitarray object whose items are bits initialized from
|
|
328
|
+
the optional initializer, and bit-endianness.
|
|
329
|
+
The initializer may be one of the following types:
|
|
330
|
+
a.) ``int`` bitarray, initialized to zeros, of given length
|
|
331
|
+
b.) ``bytes`` or ``bytearray`` to initialize buffer directly
|
|
332
|
+
c.) ``str`` of 0s and 1s, ignoring whitespace and "_"
|
|
333
|
+
d.) iterable of integers 0 or 1.
|
|
334
|
+
|
|
335
|
+
Optional keyword arguments:
|
|
336
|
+
|
|
337
|
+
``endian``: Specifies the bit-endianness of the created bitarray object.
|
|
338
|
+
Allowed values are ``big`` and ``little`` (the default is ``big``).
|
|
339
|
+
The bit-endianness affects the buffer representation of the bitarray.
|
|
340
|
+
|
|
341
|
+
``buffer``: Any object which exposes a buffer. When provided, ``initializer``
|
|
342
|
+
cannot be present (or has to be ``None``). The imported buffer may be
|
|
343
|
+
read-only or writable, depending on the object type.
|
|
344
|
+
|
|
345
|
+
New in version 2.3: optional ``buffer`` argument
|
|
346
|
+
|
|
347
|
+
New in version 3.4: allow initializer ``bytes`` or ``bytearray`` to set buffer directly
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
bitarray methods:
|
|
351
|
+
^^^^^^^^^^^^^^^^^
|
|
352
|
+
|
|
353
|
+
``all()`` -> bool
|
|
354
|
+
Return ``True`` when all bits in bitarray are 1.
|
|
355
|
+
``a.all()`` is a faster version of ``all(a)``.
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
``any()`` -> bool
|
|
359
|
+
Return ``True`` when any bit in bitarray is 1.
|
|
360
|
+
``a.any()`` is a faster version of ``any(a)``.
|
|
361
|
+
|
|
362
|
+
|
|
363
|
+
``append(item, /)``
|
|
364
|
+
Append ``item`` to the end of the bitarray.
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
``buffer_info()`` -> BufferInfo
|
|
368
|
+
Return named tuple with following fields:
|
|
369
|
+
|
|
370
|
+
0. ``address``: memory address of buffer
|
|
371
|
+
1. ``nbytes``: buffer size (in bytes)
|
|
372
|
+
2. ``endian``: bit-endianness as a string
|
|
373
|
+
3. ``padbits``: number of pad bits
|
|
374
|
+
4. ``alloc``: allocated memory for buffer (in bytes)
|
|
375
|
+
5. ``readonly``: memory is read-only (bool)
|
|
376
|
+
6. ``imported``: buffer is imported (bool)
|
|
377
|
+
7. ``exports``: number of buffer exports
|
|
378
|
+
|
|
379
|
+
New in version 3.7: return named tuple
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
``bytereverse(start=0, stop=<end of buffer>, /)``
|
|
383
|
+
For each byte in byte-range(``start``, ``stop``) reverse bits in-place.
|
|
384
|
+
The start and stop indices are given in terms of bytes (not bits) and
|
|
385
|
+
are interpreted like slice bounds and clipped to the buffer size.
|
|
386
|
+
Also note that this method only changes the buffer; it does not change the
|
|
387
|
+
bit-endianness of the bitarray object. Pad bits are left unchanged such
|
|
388
|
+
that two consecutive calls will always leave the bitarray unchanged.
|
|
389
|
+
|
|
390
|
+
New in version 2.2.5: optional start and stop arguments
|
|
391
|
+
|
|
392
|
+
New in version 3.9.1: clip arguments instead of raising ``IndexError``
|
|
393
|
+
|
|
394
|
+
|
|
395
|
+
``clear()``
|
|
396
|
+
Remove all items from bitarray.
|
|
397
|
+
|
|
398
|
+
New in version 1.4
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
``copy()`` -> bitarray
|
|
402
|
+
Return copy of bitarray (with same bit-endianness).
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
``count(value=1, start=0, stop=<end>, step=1, /)`` -> int
|
|
406
|
+
Number of occurrences of ``value`` bitarray within ``[start:stop:step]``.
|
|
407
|
+
Optional arguments ``start``, ``stop`` and ``step`` are interpreted in
|
|
408
|
+
slice notation, meaning ``a.count(value, start, stop, step)`` equals
|
|
409
|
+
``a[start:stop:step].count(value)``.
|
|
410
|
+
The ``value`` may also be a sub-bitarray. In this case non-overlapping
|
|
411
|
+
occurrences are counted within ``[start:stop]`` (``step`` must be 1).
|
|
412
|
+
|
|
413
|
+
New in version 1.1.0: optional start and stop arguments
|
|
414
|
+
|
|
415
|
+
New in version 2.3.7: optional step argument
|
|
416
|
+
|
|
417
|
+
New in version 2.9: add non-overlapping sub-bitarray count
|
|
418
|
+
|
|
419
|
+
|
|
420
|
+
``decode(code, /)`` -> decodeiterator
|
|
421
|
+
Given a prefix code (a dict mapping symbols to bitarrays, or ``decodetree``
|
|
422
|
+
object), decode content of bitarray and return an iterator over
|
|
423
|
+
corresponding symbols.
|
|
424
|
+
The prefix code length cannot exceed 256 bits.
|
|
425
|
+
|
|
426
|
+
See also: `Bitarray 3 transition <https://github.com/ilanschnell/bitarray/blob/master/doc/bitarray3.rst>`__, `Variable-length prefix codes <https://github.com/ilanschnell/bitarray/blob/master/doc/prefix_codes.rst>`__
|
|
427
|
+
|
|
428
|
+
New in version 3.0: returns iterator (equivalent to past ``.iterdecode()``)
|
|
429
|
+
|
|
430
|
+
New in version 3.9: returns public ``decodeiterator`` object
|
|
431
|
+
|
|
432
|
+
|
|
433
|
+
``encode(code, iterable, /)``
|
|
434
|
+
Given a prefix code (a dict mapping symbols to bitarrays),
|
|
435
|
+
iterate over the iterable object with symbols, and extend bitarray
|
|
436
|
+
with corresponding bitarray for each symbol.
|
|
437
|
+
|
|
438
|
+
See also: `Variable-length prefix codes <https://github.com/ilanschnell/bitarray/blob/master/doc/prefix_codes.rst>`__
|
|
439
|
+
|
|
440
|
+
|
|
441
|
+
``extend(iterable, /)``
|
|
442
|
+
Append items from iterable to the end of the bitarray.
|
|
443
|
+
If ``iterable`` is a (Unicode) string, each ``0`` and ``1`` are appended as
|
|
444
|
+
bits (ignoring whitespace and underscore).
|
|
445
|
+
|
|
446
|
+
New in version 3.4: allow ``bytes`` object
|
|
447
|
+
|
|
448
|
+
|
|
449
|
+
``fill(m=8, /)`` -> int
|
|
450
|
+
Add zeros to the end of the bitarray, such that the length will be
|
|
451
|
+
a multiple of the positive integer ``m``, and return the number of bits
|
|
452
|
+
added (in ``range(m)``).
|
|
453
|
+
|
|
454
|
+
New in version 3.12: add optional alignment argument
|
|
455
|
+
|
|
456
|
+
|
|
457
|
+
``find(sub_bitarray, start=0, stop=<end>, /, right=False)`` -> int
|
|
458
|
+
Return lowest (or rightmost when ``right=True``) index where sub_bitarray
|
|
459
|
+
is found, such that sub_bitarray is contained within ``[start:stop]``.
|
|
460
|
+
Return -1 when sub_bitarray is not found.
|
|
461
|
+
|
|
462
|
+
New in version 2.1
|
|
463
|
+
|
|
464
|
+
New in version 2.9: add optional keyword argument ``right``
|
|
465
|
+
|
|
466
|
+
|
|
467
|
+
``frombytes(bytes, /)``
|
|
468
|
+
Extend bitarray with raw bytes from a bytes-like object.
|
|
469
|
+
Each added byte will add eight bits to the bitarray.
|
|
470
|
+
|
|
471
|
+
New in version 2.5.0: allow bytes-like argument
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
``fromfile(f, n=-1, /)``
|
|
475
|
+
Extend bitarray with up to ``n`` bytes read from file object ``f`` (or any
|
|
476
|
+
other binary stream that supports a ``.read()`` method, e.g. ``io.BytesIO``).
|
|
477
|
+
Each read byte will add eight bits to the bitarray. When ``n`` is omitted
|
|
478
|
+
or negative, reads and extends all data until EOF.
|
|
479
|
+
When ``n`` is non-negative but exceeds the available data, ``EOFError`` is
|
|
480
|
+
raised. However, the available data is still read and extended.
|
|
481
|
+
|
|
482
|
+
|
|
483
|
+
``index(sub_bitarray, start=0, stop=<end>, /, right=False)`` -> int
|
|
484
|
+
Return lowest (or rightmost when ``right=True``) index where sub_bitarray
|
|
485
|
+
is found, such that sub_bitarray is contained within ``[start:stop]``.
|
|
486
|
+
Raises ``ValueError`` when sub_bitarray is not present.
|
|
487
|
+
|
|
488
|
+
New in version 2.9: add optional keyword argument ``right``
|
|
489
|
+
|
|
490
|
+
|
|
491
|
+
``insert(index, value, /)``
|
|
492
|
+
Insert ``value`` into bitarray before ``index``.
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
``invert(index=<all bits>, /)``
|
|
496
|
+
Invert bits in-place. When ``index`` is omitted, invert all bits.
|
|
497
|
+
When ``index`` is an integer, invert the single bit at index.
|
|
498
|
+
When ``index`` is a slice, invert the selected bits.
|
|
499
|
+
|
|
500
|
+
New in version 1.5.3: optional index argument
|
|
501
|
+
|
|
502
|
+
|
|
503
|
+
``pack(bytes, /)``
|
|
504
|
+
Extend bitarray from a bytes-like object, where each byte corresponds
|
|
505
|
+
to a single bit. The byte ``b'\x00'`` maps to bit 0 and all other bytes
|
|
506
|
+
map to bit 1.
|
|
507
|
+
|
|
508
|
+
This method, as well as the ``.unpack()`` method, are meant for efficient
|
|
509
|
+
transfer of data between bitarray objects to other Python objects (for
|
|
510
|
+
example NumPy's ndarray object) which have a different memory view.
|
|
511
|
+
|
|
512
|
+
New in version 2.5.0: allow bytes-like argument
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
``pop(index=-1, /)`` -> item
|
|
516
|
+
Remove and return item at ``index`` (default last).
|
|
517
|
+
Raises ``IndexError`` if index is out of range.
|
|
518
|
+
|
|
519
|
+
|
|
520
|
+
``remove(value, /)``
|
|
521
|
+
Remove the first occurrence of ``value``.
|
|
522
|
+
Raises ``ValueError`` if value is not present.
|
|
523
|
+
|
|
524
|
+
|
|
525
|
+
``reverse()``
|
|
526
|
+
Reverse all bits in bitarray (in-place).
|
|
527
|
+
|
|
528
|
+
|
|
529
|
+
``rotate(k=1, /)``
|
|
530
|
+
Rotate bitarray in-place by ``k`` positions.
|
|
531
|
+
Positive ``k`` rotates right, negative ``k`` rotates left.
|
|
532
|
+
|
|
533
|
+
When bitarray ``a`` is not empty, rotating one step to the right is
|
|
534
|
+
equivalent to ``a.insert(0, a.pop())``, and rotating one step to the left
|
|
535
|
+
is equivalent to ``a.append(a.pop(0))``.
|
|
536
|
+
The same convention is used by the ``.rotate()`` method of
|
|
537
|
+
the ``collections.deque`` object.
|
|
538
|
+
|
|
539
|
+
New in version 3.9
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
``search(sub_bitarray, start=0, stop=<end>, /, right=False)`` -> iterator
|
|
543
|
+
Return iterator over indices where sub_bitarray is found, such that
|
|
544
|
+
sub_bitarray is contained within ``[start:stop]``.
|
|
545
|
+
The indices are iterated in ascending order (from lowest to highest),
|
|
546
|
+
unless ``right=True``, which will iterate in descending order (starting with
|
|
547
|
+
rightmost match).
|
|
548
|
+
|
|
549
|
+
For example, ``a.search(1)`` is the easiest (and most efficient) way
|
|
550
|
+
to create an iterator over all active indices in ``a``.
|
|
551
|
+
|
|
552
|
+
See also: `Bitarray 3 transition <https://github.com/ilanschnell/bitarray/blob/master/doc/bitarray3.rst>`__
|
|
553
|
+
|
|
554
|
+
New in version 2.9: optional start and stop arguments - add optional keyword argument ``right``
|
|
555
|
+
|
|
556
|
+
New in version 3.0: returns iterator (equivalent to past ``.itersearch()``)
|
|
557
|
+
|
|
558
|
+
|
|
559
|
+
``setall(value, /)``
|
|
560
|
+
Set all elements in bitarray to ``value``.
|
|
561
|
+
Note that ``a.setall(value)`` is equivalent to ``a[:] = value``.
|
|
562
|
+
|
|
563
|
+
|
|
564
|
+
``sort(reverse=False)``
|
|
565
|
+
Sort all bits in bitarray (in-place).
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
``to01(group=0, sep=' ')`` -> str
|
|
569
|
+
Return bitarray as (Unicode) string of ``0``s and ``1``s.
|
|
570
|
+
The bits are grouped into ``group`` bits (default is no grouping).
|
|
571
|
+
When grouped, the string ``sep`` is inserted between groups
|
|
572
|
+
of ``group`` characters, default is a space.
|
|
573
|
+
|
|
574
|
+
New in version 3.3: optional ``group`` and ``sep`` arguments
|
|
575
|
+
|
|
576
|
+
|
|
577
|
+
``tobytes()`` -> bytes
|
|
578
|
+
Return the bitarray buffer (pad bits are set to zero).
|
|
579
|
+
``a.tobytes()`` is equivalent to ``bytes(a)``
|
|
580
|
+
|
|
581
|
+
|
|
582
|
+
``tofile(f, /)``
|
|
583
|
+
Write bitarray buffer to file object ``f``.
|
|
584
|
+
|
|
585
|
+
|
|
586
|
+
``tolist()`` -> list
|
|
587
|
+
Return bitarray as list of integers.
|
|
588
|
+
``a.tolist()`` equals ``list(a)``.
|
|
589
|
+
|
|
590
|
+
Note that the list object being created will require 32 or 64 times more
|
|
591
|
+
memory (depending on the machine architecture) than the bitarray object,
|
|
592
|
+
which may cause a memory error if the bitarray is very large.
|
|
593
|
+
|
|
594
|
+
|
|
595
|
+
``unpack(zero=b'\x00', one=b'\x01')`` -> bytes
|
|
596
|
+
Return bytes that contain one byte for each bit in the bitarray,
|
|
597
|
+
using the specified mapping.
|
|
598
|
+
|
|
599
|
+
|
|
600
|
+
bitarray attributes:
|
|
601
|
+
^^^^^^^^^^^^^^^^^^^^
|
|
602
|
+
|
|
603
|
+
Attributes were added in version 2.6.
|
|
604
|
+
|
|
605
|
+
``endian`` -> str
|
|
606
|
+
bit-endianness as Unicode string
|
|
607
|
+
|
|
608
|
+
New in version 3.4: replaces former ``.endian()`` method
|
|
609
|
+
|
|
610
|
+
|
|
611
|
+
``nbytes`` -> int
|
|
612
|
+
buffer size in bytes
|
|
613
|
+
|
|
614
|
+
|
|
615
|
+
``padbits`` -> int
|
|
616
|
+
number of pad bits
|
|
617
|
+
|
|
618
|
+
|
|
619
|
+
``readonly`` -> bool
|
|
620
|
+
bool indicating whether buffer is read-only
|
|
621
|
+
|
|
622
|
+
|
|
623
|
+
decodeiterator methods:
|
|
624
|
+
^^^^^^^^^^^^^^^^^^^^^^^
|
|
625
|
+
|
|
626
|
+
``skipbits(n, /)`` -> bitarray
|
|
627
|
+
Skip over the next ``n`` bits and return them.
|
|
628
|
+
Raises ``ValueError`` if count is out of range.
|
|
629
|
+
|
|
630
|
+
New in version 3.9
|
|
631
|
+
|
|
632
|
+
|
|
633
|
+
decodeiterator attributes:
|
|
634
|
+
^^^^^^^^^^^^^^^^^^^^^^^^^^
|
|
635
|
+
|
|
636
|
+
``index`` -> int
|
|
637
|
+
current bit position to be decoded by subsequent ``next``
|
|
638
|
+
|
|
639
|
+
New in version 3.9
|
|
640
|
+
|
|
641
|
+
|
|
642
|
+
decodetree methods:
|
|
643
|
+
^^^^^^^^^^^^^^^^^^^
|
|
644
|
+
|
|
645
|
+
``nodes()`` -> tuple
|
|
646
|
+
Return tuple with number of:
|
|
647
|
+
|
|
648
|
+
0. incomplete nodes (pointing to a single child node)
|
|
649
|
+
1. complete nodes (pointing to two child nodes)
|
|
650
|
+
2. leaf nodes (pointing to a symbol)
|
|
651
|
+
|
|
652
|
+
New in version 3.10
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
``todict()`` -> dict
|
|
656
|
+
Return a dict mapping the symbols to frozenbitarrays. This dict is a
|
|
657
|
+
reconstruction of the code dict which the object was created with.
|
|
658
|
+
|
|
659
|
+
New in version 3.10
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
Other objects:
|
|
663
|
+
--------------
|
|
664
|
+
|
|
665
|
+
``frozenbitarray(initializer=0, /, endian='big', buffer=None)`` -> frozenbitarray
|
|
666
|
+
Return a ``frozenbitarray`` object. Initialized the same way a ``bitarray``
|
|
667
|
+
object is initialized. A ``frozenbitarray`` is immutable and hashable,
|
|
668
|
+
and may therefore be used as a dictionary key.
|
|
669
|
+
|
|
670
|
+
New in version 1.1
|
|
671
|
+
|
|
672
|
+
|
|
673
|
+
``decodetree(code, /)`` -> decodetree
|
|
674
|
+
Given a prefix code (a dict mapping symbols to bitarrays),
|
|
675
|
+
create a binary tree object to be passed to ``.decode()``.
|
|
676
|
+
The prefix code length cannot exceed 256 bits.
|
|
677
|
+
|
|
678
|
+
See also: `Variable-length prefix codes <https://github.com/ilanschnell/bitarray/blob/master/doc/prefix_codes.rst>`__
|
|
679
|
+
|
|
680
|
+
New in version 1.6
|
|
681
|
+
|
|
682
|
+
|
|
683
|
+
Functions defined in the `bitarray` module:
|
|
684
|
+
-------------------------------------------
|
|
685
|
+
|
|
686
|
+
``bits2bytes(n, /)`` -> int
|
|
687
|
+
Return the number of bytes necessary to store n bits.
|
|
688
|
+
|
|
689
|
+
|
|
690
|
+
``get_default_endian()`` -> str
|
|
691
|
+
Return the default bit-endianness for new bitarray objects being created.
|
|
692
|
+
|
|
693
|
+
New in version 1.3
|
|
694
|
+
|
|
695
|
+
|
|
696
|
+
``test(verbosity=1)`` -> TextTestResult
|
|
697
|
+
Run self-test, and return ``unittest.runner.TextTestResult`` object.
|
|
698
|
+
|
|
699
|
+
|
|
700
|
+
Functions defined in `bitarray.util` module:
|
|
701
|
+
--------------------------------------------
|
|
702
|
+
|
|
703
|
+
This sub-module was added in version 1.2.
|
|
704
|
+
|
|
705
|
+
``any_and(a, b, /)`` -> bool
|
|
706
|
+
Efficient implementation of ``any(a & b)``.
|
|
707
|
+
|
|
708
|
+
New in version 2.7
|
|
709
|
+
|
|
710
|
+
|
|
711
|
+
``ba2base(n, bitarray, /, group=0, sep=' ')`` -> str
|
|
712
|
+
Return a string containing the base ``n`` ASCII representation of
|
|
713
|
+
the bitarray. Allowed values for ``n`` are 2, 4, 8, 16, 32 and 64.
|
|
714
|
+
The bitarray has to have a length divisible by 1, 2, 3, 4, 5 or 6
|
|
715
|
+
respectively.
|
|
716
|
+
For ``n=32`` the RFC 4648 Base32 alphabet is used, and for ``n=64`` the
|
|
717
|
+
standard base 64 alphabet is used.
|
|
718
|
+
When grouped, the string ``sep`` is inserted between groups
|
|
719
|
+
of ``group`` characters, default is a space.
|
|
720
|
+
|
|
721
|
+
See also: `Bitarray representations <https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst>`__
|
|
722
|
+
|
|
723
|
+
New in version 1.9
|
|
724
|
+
|
|
725
|
+
New in version 3.3: optional ``group`` and ``sep`` arguments
|
|
726
|
+
|
|
727
|
+
|
|
728
|
+
``ba2hex(bitarray, /, group=0, sep=' ')`` -> hexstr
|
|
729
|
+
Return a string containing the hexadecimal representation of
|
|
730
|
+
the bitarray (which has to be multiple of 4 in length).
|
|
731
|
+
When grouped, the string ``sep`` is inserted between groups
|
|
732
|
+
of ``group`` characters, default is a space.
|
|
733
|
+
|
|
734
|
+
New in version 3.3: optional ``group`` and ``sep`` arguments
|
|
735
|
+
|
|
736
|
+
|
|
737
|
+
``ba2int(bitarray, /, signed=False)`` -> int
|
|
738
|
+
Convert the given bitarray to an integer.
|
|
739
|
+
The bit-endianness of the bitarray is respected.
|
|
740
|
+
``signed`` indicates whether two's complement is used to represent the integer.
|
|
741
|
+
|
|
742
|
+
|
|
743
|
+
``base2ba(n, asciistr, /, endian=None)`` -> bitarray
|
|
744
|
+
Bitarray of base ``n`` ASCII representation.
|
|
745
|
+
Allowed values for ``n`` are 2, 4, 8, 16, 32 and 64.
|
|
746
|
+
For ``n=32`` the RFC 4648 Base32 alphabet is used, and for ``n=64`` the
|
|
747
|
+
standard base 64 alphabet is used. ASCII whitespace is ignored.
|
|
748
|
+
|
|
749
|
+
See also: `Bitarray representations <https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst>`__
|
|
750
|
+
|
|
751
|
+
New in version 1.9
|
|
752
|
+
|
|
753
|
+
New in version 3.3: ignore whitespace
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
``byteswap(a, n=<buffer size>, /)``
|
|
757
|
+
Reverse every ``n`` consecutive bytes of ``a`` in-place.
|
|
758
|
+
By default, all bytes are reversed. Note that ``n`` is **not** limited
|
|
759
|
+
to 2, 4 or 8, but can be any positive integer.
|
|
760
|
+
Also, ``a`` may be any object that exposes a writable buffer.
|
|
761
|
+
Nothing about this function is specific to bitarray objects.
|
|
762
|
+
|
|
763
|
+
We should mention that Python's ``array.array`` object has a
|
|
764
|
+
method ``.byteswap()`` with similar functionality. However, unlike
|
|
765
|
+
bitarray's ``util.byteswap()`` function, this method is limited to
|
|
766
|
+
swapping 2, 4, or 8 consecutive bytes.
|
|
767
|
+
|
|
768
|
+
New in version 3.4
|
|
769
|
+
|
|
770
|
+
|
|
771
|
+
``canonical_decode(bitarray, count, symbol, /)`` -> iterator
|
|
772
|
+
Decode bitarray using canonical Huffman decoding tables
|
|
773
|
+
where ``count`` is a sequence containing the number of symbols of each length
|
|
774
|
+
and ``symbol`` is a sequence of symbols in canonical order.
|
|
775
|
+
|
|
776
|
+
See also: `Canonical Huffman Coding <https://github.com/ilanschnell/bitarray/blob/master/doc/canonical.rst>`__
|
|
777
|
+
|
|
778
|
+
New in version 2.5
|
|
779
|
+
|
|
780
|
+
|
|
781
|
+
``canonical_huffman(dict, /)`` -> tuple
|
|
782
|
+
Given a frequency map, a dictionary mapping symbols to their frequency,
|
|
783
|
+
calculate the canonical Huffman code. Returns a tuple containing:
|
|
784
|
+
|
|
785
|
+
0. the canonical Huffman code as a dict mapping symbols to frozenbitarrays
|
|
786
|
+
1. a list containing the number of symbols of each code length
|
|
787
|
+
2. a list of symbols in canonical order
|
|
788
|
+
|
|
789
|
+
Note: the two lists may be used as input for ``canonical_decode()``.
|
|
790
|
+
|
|
791
|
+
See also: `Canonical Huffman Coding <https://github.com/ilanschnell/bitarray/blob/master/doc/canonical.rst>`__
|
|
792
|
+
|
|
793
|
+
New in version 2.5
|
|
794
|
+
|
|
795
|
+
New in version 3.10: return a codedict mapping to frozenbitarrays
|
|
796
|
+
|
|
797
|
+
|
|
798
|
+
``correspond_all(a, b, /)`` -> tuple
|
|
799
|
+
Return tuple with counts of: ~a & ~b, ~a & b, a & ~b, a & b
|
|
800
|
+
|
|
801
|
+
New in version 3.4
|
|
802
|
+
|
|
803
|
+
|
|
804
|
+
``count_and(a, b, /)`` -> int
|
|
805
|
+
Return ``(a & b).count()`` in a memory efficient manner,
|
|
806
|
+
as no intermediate bitarray object gets created.
|
|
807
|
+
|
|
808
|
+
|
|
809
|
+
``count_n(a, n, value=1, /)`` -> int
|
|
810
|
+
Return lowest index ``i`` for which ``a[:i].count(value) == n``.
|
|
811
|
+
Raises ``ValueError`` when ``n`` exceeds total count (``a.count(value)``).
|
|
812
|
+
|
|
813
|
+
New in version 2.3.6: optional value argument
|
|
814
|
+
|
|
815
|
+
|
|
816
|
+
``count_or(a, b, /)`` -> int
|
|
817
|
+
Return ``(a | b).count()`` in a memory efficient manner,
|
|
818
|
+
as no intermediate bitarray object gets created.
|
|
819
|
+
|
|
820
|
+
|
|
821
|
+
``count_xor(a, b, /)`` -> int
|
|
822
|
+
Return ``(a ^ b).count()`` in a memory efficient manner,
|
|
823
|
+
as no intermediate bitarray object gets created.
|
|
824
|
+
|
|
825
|
+
This is also known as the Hamming distance.
|
|
826
|
+
|
|
827
|
+
|
|
828
|
+
``deserialize(bytes, /)`` -> bitarray
|
|
829
|
+
Return a bitarray given a bytes-like representation such as returned
|
|
830
|
+
by ``serialize()``.
|
|
831
|
+
|
|
832
|
+
See also: `Bitarray representations <https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst>`__
|
|
833
|
+
|
|
834
|
+
New in version 1.8
|
|
835
|
+
|
|
836
|
+
New in version 2.5.0: allow bytes-like argument
|
|
837
|
+
|
|
838
|
+
|
|
839
|
+
``gen_primes(n, /, endian=None, odd=False)`` -> bitarray
|
|
840
|
+
Generate a bitarray of length ``n`` in which active indices are prime numbers.
|
|
841
|
+
By default (``odd=False``), active indices correspond to prime numbers directly.
|
|
842
|
+
When ``odd=True``, only odd prime numbers are represented in the resulting
|
|
843
|
+
bitarray ``a``, and ``a[i]`` corresponds to ``2*i+1`` being prime or not.
|
|
844
|
+
|
|
845
|
+
Apart from working with prime numbers, this function is useful for
|
|
846
|
+
testing, as it provides a simple way to create a well-defined bitarray
|
|
847
|
+
of any length.
|
|
848
|
+
|
|
849
|
+
New in version 3.7
|
|
850
|
+
|
|
851
|
+
|
|
852
|
+
``hex2ba(hexstr, /, endian=None)`` -> bitarray
|
|
853
|
+
Bitarray of hexadecimal representation. hexstr may contain any number
|
|
854
|
+
(including odd numbers) of hex digits (upper or lower case).
|
|
855
|
+
ASCII whitespace is ignored.
|
|
856
|
+
|
|
857
|
+
New in version 3.3: ignore whitespace
|
|
858
|
+
|
|
859
|
+
|
|
860
|
+
``huffman_code(dict, /, endian=None)`` -> dict
|
|
861
|
+
Given a frequency map, a dictionary mapping symbols to their frequency,
|
|
862
|
+
calculate the Huffman code, i.e. a dict mapping those symbols to
|
|
863
|
+
frozenbitarrays (with given bit-endianness). Note that the symbols are not
|
|
864
|
+
limited to being strings. Symbols may be any hashable object.
|
|
865
|
+
|
|
866
|
+
See also: `Variable-length prefix codes <https://github.com/ilanschnell/bitarray/blob/master/doc/prefix_codes.rst>`__
|
|
867
|
+
|
|
868
|
+
New in version 3.10: return a codedict mapping to frozenbitarrays
|
|
869
|
+
|
|
870
|
+
|
|
871
|
+
``int2ba(int, /, length=None, endian=None, signed=False)`` -> bitarray
|
|
872
|
+
Convert the given integer to a bitarray (with given bit-endianness,
|
|
873
|
+
and no leading (big-endian) / trailing (little-endian) zeros), unless
|
|
874
|
+
the ``length`` of the bitarray is provided. An ``OverflowError`` is raised
|
|
875
|
+
if the integer is not representable with the given number of bits.
|
|
876
|
+
``signed`` determines whether two's complement is used to represent the integer,
|
|
877
|
+
and requires ``length`` to be provided.
|
|
878
|
+
|
|
879
|
+
|
|
880
|
+
``intervals(bitarray, /)`` -> iterator
|
|
881
|
+
Compute all uninterrupted intervals of 1s and 0s, and return an
|
|
882
|
+
iterator over tuples ``(value, start, stop)``. The intervals are guaranteed
|
|
883
|
+
to be in order, and their size is always non-zero (``stop - start > 0``).
|
|
884
|
+
|
|
885
|
+
New in version 2.7
|
|
886
|
+
|
|
887
|
+
|
|
888
|
+
``ones(n, /, endian=None)`` -> bitarray
|
|
889
|
+
Create a bitarray of length ``n``, with all values ``1``, and optional
|
|
890
|
+
bit-endianness (``little`` or ``big``).
|
|
891
|
+
|
|
892
|
+
New in version 2.9
|
|
893
|
+
|
|
894
|
+
|
|
895
|
+
``parity(a, /)`` -> int
|
|
896
|
+
Return parity of bitarray ``a``.
|
|
897
|
+
``parity(a)`` is equivalent to ``a.count() % 2`` but more efficient.
|
|
898
|
+
|
|
899
|
+
New in version 1.9
|
|
900
|
+
|
|
901
|
+
|
|
902
|
+
``pprint(bitarray, /, stream=None, group=8, indent=4, width=80)``
|
|
903
|
+
Pretty-print bitarray object to ``stream``, defaults to ``sys.stdout``.
|
|
904
|
+
By default, bits are grouped in bytes (8 bits), and 64 bits per line.
|
|
905
|
+
Non-bitarray objects are printed using ``pprint.pprint()``.
|
|
906
|
+
|
|
907
|
+
New in version 1.8
|
|
908
|
+
|
|
909
|
+
|
|
910
|
+
``random_k(n, /, k, endian=None)`` -> bitarray
|
|
911
|
+
Return (pseudo-) random bitarray of length ``n`` with ``k`` elements
|
|
912
|
+
set to one. Mathematically equivalent to setting (in a bitarray of
|
|
913
|
+
length ``n``) all bits at indices ``random.sample(range(n), k)`` to one.
|
|
914
|
+
The random bitarrays are reproducible when calling Python's ``random.seed()``
|
|
915
|
+
with a specific seed value.
|
|
916
|
+
|
|
917
|
+
New in version 3.6
|
|
918
|
+
|
|
919
|
+
|
|
920
|
+
``random_p(n, /, p=0.5, endian=None)`` -> bitarray
|
|
921
|
+
Return (pseudo-) random bitarray of length ``n``, where each bit has
|
|
922
|
+
probability ``p`` of being one (independent of any other bits). Mathematically
|
|
923
|
+
equivalent to ``bitarray((random() < p for _ in range(n)), endian)``, but much
|
|
924
|
+
faster for large ``n``. The random bitarrays are reproducible when calling
|
|
925
|
+
Python's ``random.seed()`` with a specific seed value.
|
|
926
|
+
|
|
927
|
+
This function requires Python 3.12 or higher, as it depends on the standard
|
|
928
|
+
library function ``random.binomialvariate()``. Raises ``NotImplementedError``
|
|
929
|
+
when Python version is too low.
|
|
930
|
+
|
|
931
|
+
See also: `Random Bitarrays <https://github.com/ilanschnell/bitarray/blob/master/doc/random_p.rst>`__
|
|
932
|
+
|
|
933
|
+
New in version 3.5
|
|
934
|
+
|
|
935
|
+
|
|
936
|
+
``rl_decode(stream, /, endian=None)`` -> bitarray
|
|
937
|
+
Decode a run-length encoded bitarray from a binary stream (an integer
|
|
938
|
+
iterator, or bytes-like object), and return the decoded bitarray.
|
|
939
|
+
This function consumes only one bitarray and leaves the remaining stream
|
|
940
|
+
untouched. Use ``rl_encode()`` for encoding.
|
|
941
|
+
|
|
942
|
+
See also: `Comparison of bitarray codecs <https://github.com/ilanschnell/bitarray/blob/master/doc/codecs.rst>`__
|
|
943
|
+
|
|
944
|
+
New in version 3.11
|
|
945
|
+
|
|
946
|
+
|
|
947
|
+
``rl_encode(bitarray, /)`` -> bytes
|
|
948
|
+
Return the run-length encoded binary representation of a bitarray.
|
|
949
|
+
This representation may be useful for bitarrays with long runs of
|
|
950
|
+
identical bits. Use ``rl_decode()`` for decoding.
|
|
951
|
+
|
|
952
|
+
See also: `Comparison of bitarray codecs <https://github.com/ilanschnell/bitarray/blob/master/doc/codecs.rst>`__
|
|
953
|
+
|
|
954
|
+
New in version 3.11
|
|
955
|
+
|
|
956
|
+
|
|
957
|
+
``sc_decode(stream, /)`` -> bitarray
|
|
958
|
+
Decompress binary stream (an integer iterator, or bytes-like object) of a
|
|
959
|
+
sparse compressed (``sc``) bitarray, and return the decoded bitarray.
|
|
960
|
+
This function consumes only one bitarray and leaves the remaining stream
|
|
961
|
+
untouched. Use ``sc_encode()`` for compressing (encoding).
|
|
962
|
+
|
|
963
|
+
See also: `Compression of sparse bitarrays <https://github.com/ilanschnell/bitarray/blob/master/doc/sparse_compression.rst>`__, `Comparison of bitarray codecs <https://github.com/ilanschnell/bitarray/blob/master/doc/codecs.rst>`__
|
|
964
|
+
|
|
965
|
+
New in version 2.7
|
|
966
|
+
|
|
967
|
+
|
|
968
|
+
``sc_encode(bitarray, /)`` -> bytes
|
|
969
|
+
Compress a bitarray using sparse encoding and return its binary
|
|
970
|
+
representation. This representation is useful for efficiently storing
|
|
971
|
+
sparse bitarrays. Use ``sc_decode()`` for decompressing (decoding).
|
|
972
|
+
|
|
973
|
+
See also: `Compression of sparse bitarrays <https://github.com/ilanschnell/bitarray/blob/master/doc/sparse_compression.rst>`__, `Comparison of bitarray codecs <https://github.com/ilanschnell/bitarray/blob/master/doc/codecs.rst>`__
|
|
974
|
+
|
|
975
|
+
New in version 2.7
|
|
976
|
+
|
|
977
|
+
|
|
978
|
+
``sc_stat(stream)`` -> dict
|
|
979
|
+
Scan a stream produced by ``sc_encode()`` and return a dictionary containing
|
|
980
|
+
its bit-endianness (``endian``), length (``nbits``), and the number of blocks
|
|
981
|
+
of each type (``blocks``). ``blocks`` is a list such that ``blocks[i]`` is the
|
|
982
|
+
number of blocks of type ``i``.
|
|
983
|
+
|
|
984
|
+
Like ``sc_decode()``, it consumes one encoded bitarray and leaves remaining
|
|
985
|
+
input untouched.
|
|
986
|
+
|
|
987
|
+
See also: `Compression of sparse bitarrays <https://github.com/ilanschnell/bitarray/blob/master/doc/sparse_compression.rst>`__
|
|
988
|
+
|
|
989
|
+
New in version 3.11
|
|
990
|
+
|
|
991
|
+
|
|
992
|
+
``serialize(bitarray, /)`` -> bytes
|
|
993
|
+
Return a serialized representation of the bitarray, which may be passed to
|
|
994
|
+
``deserialize()``. It efficiently represents the bitarray object (including
|
|
995
|
+
its bit-endianness) and is guaranteed not to change in future releases.
|
|
996
|
+
|
|
997
|
+
See also: `Bitarray representations <https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst>`__
|
|
998
|
+
|
|
999
|
+
New in version 1.8
|
|
1000
|
+
|
|
1001
|
+
|
|
1002
|
+
``strip(bitarray, /, mode='right')`` -> bitarray
|
|
1003
|
+
Return a new bitarray with zeros stripped from left, right or both ends.
|
|
1004
|
+
Allowed values for mode are the strings: ``left``, ``right``, ``both``
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
``subset(a, b, /)`` -> bool
|
|
1008
|
+
Return ``True`` if bitarray ``a`` is a subset of bitarray ``b``.
|
|
1009
|
+
``subset(a, b)`` is equivalent to ``a | b == b`` (and equally ``a & b == a``) but
|
|
1010
|
+
more efficient as no intermediate bitarray object is created and the buffer
|
|
1011
|
+
iteration is stopped as soon as one mismatch is found.
|
|
1012
|
+
|
|
1013
|
+
|
|
1014
|
+
``sum_indices(a, /, mode=1)`` -> int
|
|
1015
|
+
Return sum of indices of all active bits in bitarray ``a``.
|
|
1016
|
+
Equivalent to ``sum(i for i, v in enumerate(a) if v)``.
|
|
1017
|
+
``mode=2`` sums square of indices.
|
|
1018
|
+
|
|
1019
|
+
New in version 3.6
|
|
1020
|
+
|
|
1021
|
+
New in version 3.7: add optional mode argument
|
|
1022
|
+
|
|
1023
|
+
|
|
1024
|
+
``urandom(n, /, endian=None)`` -> bitarray
|
|
1025
|
+
Return random bitarray of length ``n`` (uses ``os.urandom()``).
|
|
1026
|
+
|
|
1027
|
+
New in version 1.7
|
|
1028
|
+
|
|
1029
|
+
|
|
1030
|
+
``vl_decode(stream, /, endian=None)`` -> bitarray
|
|
1031
|
+
Decode binary stream (an integer iterator, or bytes-like object), and
|
|
1032
|
+
return the decoded bitarray. This function consumes only one bitarray and
|
|
1033
|
+
leaves the remaining stream untouched. Use ``vl_encode()`` for encoding.
|
|
1034
|
+
|
|
1035
|
+
See also: `Variable length bitarray format <https://github.com/ilanschnell/bitarray/blob/master/doc/variable_length.rst>`__, `Comparison of bitarray codecs <https://github.com/ilanschnell/bitarray/blob/master/doc/codecs.rst>`__
|
|
1036
|
+
|
|
1037
|
+
New in version 2.2
|
|
1038
|
+
|
|
1039
|
+
|
|
1040
|
+
``vl_encode(bitarray, /)`` -> bytes
|
|
1041
|
+
Return variable length binary representation of bitarray.
|
|
1042
|
+
This representation is useful for efficiently storing small bitarray
|
|
1043
|
+
in a binary stream. Use ``vl_decode()`` for decoding.
|
|
1044
|
+
|
|
1045
|
+
See also: `Variable length bitarray format <https://github.com/ilanschnell/bitarray/blob/master/doc/variable_length.rst>`__, `Comparison of bitarray codecs <https://github.com/ilanschnell/bitarray/blob/master/doc/codecs.rst>`__
|
|
1046
|
+
|
|
1047
|
+
New in version 2.2
|
|
1048
|
+
|
|
1049
|
+
|
|
1050
|
+
``xor_indices(a, /)`` -> int
|
|
1051
|
+
Return xor reduced indices of all active bits in bitarray ``a``.
|
|
1052
|
+
This is essentially equivalent to
|
|
1053
|
+
``reduce(operator.xor, (i for i, v in enumerate(a) if v))``.
|
|
1054
|
+
|
|
1055
|
+
New in version 3.2
|
|
1056
|
+
|
|
1057
|
+
|
|
1058
|
+
``zeros(n, /, endian=None)`` -> bitarray
|
|
1059
|
+
Create a bitarray of length ``n``, with all values ``0``, and optional
|
|
1060
|
+
bit-endianness (``little`` or ``big``).
|
|
1061
|
+
|
|
1062
|
+
|
|
1063
|
+
The `bitarray.bitfields` module:
|
|
1064
|
+
--------------------------------
|
|
1065
|
+
|
|
1066
|
+
This sub-module was added in version 3.12.
|
|
1067
|
+
For a detailed description, see: `Bit-field structures <https://github.com/ilanschnell/bitarray/blob/master/doc/bitfields.rst>`__
|
|
1068
|
+
|
|
1069
|
+
``Struct(format)`` -> compiled struct object
|
|
1070
|
+
Central class for packing and unpacking bit-level structures.
|
|
1071
|
+
|
|
1072
|
+
|
|
1073
|
+
``calcsize(format)`` -> int
|
|
1074
|
+
Return the size (in bits) of the struct corresponding to the format string.
|
|
1075
|
+
|
|
1076
|
+
|
|
1077
|
+
``compile(format)`` -> Struct
|
|
1078
|
+
Compile given format string and return a compiled format object that
|
|
1079
|
+
can be used to pack and/or unpack data multiple times.
|
|
1080
|
+
|
|
1081
|
+
|
|
1082
|
+
``pack(format, v1, v2, ...)`` -> bitarray
|
|
1083
|
+
Return a bitarray containing the values v1, v2, ... packed according
|
|
1084
|
+
to the format string.
|
|
1085
|
+
|
|
1086
|
+
|
|
1087
|
+
``pack_into(format, bitarray, offset, v1, v2, ...)`` -> None
|
|
1088
|
+
Pack the values v1, v2, ... into the writable bitarray starting at bit
|
|
1089
|
+
offset ``offset``. A negative offset counts from the end of bitarray.
|
|
1090
|
+
|
|
1091
|
+
|
|
1092
|
+
``unpack(format, bitarray)`` -> tuple
|
|
1093
|
+
Return a tuple containing values unpacked according to the format string.
|
|
1094
|
+
|
|
1095
|
+
|
|
1096
|
+
``unpack_from(format, bitarray, offset=0)`` -> tuple
|
|
1097
|
+
Unpack values from bitarray starting at bit offset ``offset``, and return a
|
|
1098
|
+
tuple. A negative offset counts from the end of bitarray.
|
|
1099
|
+
|
|
1100
|
+
|
|
1101
|
+
Struct methods:
|
|
1102
|
+
^^^^^^^^^^^^^^^
|
|
1103
|
+
|
|
1104
|
+
``format()`` -> str
|
|
1105
|
+
Return the canonical format string reconstructed from this compiled format.
|
|
1106
|
+
|
|
1107
|
+
|
|
1108
|
+
``pack(v1, v2, ...)`` -> bitarray
|
|
1109
|
+
Return a bitarray containing the values v1, v2, ... packed according to this
|
|
1110
|
+
compiled format.
|
|
1111
|
+
|
|
1112
|
+
|
|
1113
|
+
``pack_into(bitarray, offset, v1, v2, ...)`` -> None
|
|
1114
|
+
Pack the values v1, v2, ... into the writable bitarray starting at bit
|
|
1115
|
+
offset ``offset``. A negative offset counts from the end of bitarray.
|
|
1116
|
+
|
|
1117
|
+
|
|
1118
|
+
``unpack(bitarray)`` -> tuple
|
|
1119
|
+
Return a tuple containing values unpacked according to this compiled format.
|
|
1120
|
+
|
|
1121
|
+
|
|
1122
|
+
``unpack_from(bitarray, offset=0)`` -> tuple
|
|
1123
|
+
Unpack values from bitarray starting at bit offset ``offset``, and return a
|
|
1124
|
+
tuple. A negative offset counts from the end of bitarray.
|
|
1125
|
+
|
|
1126
|
+
|
|
1127
|
+
Struct attributes:
|
|
1128
|
+
^^^^^^^^^^^^^^^^^^
|
|
1129
|
+
|
|
1130
|
+
``width`` -> int
|
|
1131
|
+
Total number of bits in the compiled structure.
|
|
1132
|
+
|
|
1133
|
+
``values`` -> int
|
|
1134
|
+
Number of values consumed by ``pack()`` and returned by ``unpack()``.
|