Spaces:
Running
Running
File size: 30,138 Bytes
ba2f5d6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 |
Metadata-Version: 2.1
Name: bitarray
Version: 2.5.1
Summary: efficient arrays of booleans -- C extension
Home-page: https://github.com/ilanschnell/bitarray
Author: Ilan Schnell
Author-email: [email protected]
License: PSF
Platform: UNKNOWN
Classifier: License :: OSI Approved :: Python Software Foundation License
Classifier: Development Status :: 6 - Mature
Classifier: Intended Audience :: Developers
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: C
Classifier: Programming Language :: Python :: 2
Classifier: Programming Language :: Python :: 2.7
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.5
Classifier: Programming Language :: Python :: 3.6
Classifier: Programming Language :: Python :: 3.7
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Topic :: Utilities
License-File: LICENSE
bitarray: efficient arrays of booleans
======================================
This library provides an object type which efficiently represents an array
of booleans. Bitarrays are sequence types and behave very much like usual
lists. Eight bits are represented by one byte in a contiguous block of
memory. The user can select between two representations: little-endian
and big-endian. All functionality is implemented in C.
Methods for accessing the machine representation are provided, including the
ability to import and export buffers. This allows creating bitarrays that
are mapped to other objects, including memory-mapped files.
Key features
------------
* The bit endianness can be specified for each bitarray object, see below.
* Sequence methods: slicing (including slice assignment and deletion),
operations ``+``, ``*``, ``+=``, ``*=``, the ``in`` operator, ``len()``
* Bitwise operations: ``~``, ``&``, ``|``, ``^``, ``<<``, ``>>`` (as well as
their in-place versions ``&=``, ``|=``, ``^=``, ``<<=``, ``>>=``).
* Fast methods for encoding and decoding variable bit length prefix codes.
* Bitarray objects support the buffer protocol (both importing and
exporting buffers).
* Packing and unpacking to other binary data formats, e.g. ``numpy.ndarray``.
* Pickling and unpickling of bitarray objects.
* Immutable ``frozenbitarray`` objects which are hashable
* Sequential search
* Type hinting
* Extensive test suite with over 400 unittests.
* Utility module ``bitarray.util``:
* conversion to and from hexadecimal strings
* (de-) serialization
* pretty printing
* conversion to and from integers
* creating Huffman codes
* various count functions
* other helpful functions
Installation
------------
Python wheels are are available on PyPI for all mayor platforms and Python
versions. Which means you can simply:
.. code-block:: shell-session
$ pip install bitarray
In addition, conda packages are available (both the default Anaconda
repository as well as conda-forge support bitarray):
.. code-block:: shell-session
$ conda install bitarray
Once you have installed the package, you may want to test it:
.. code-block:: shell-session
$ python -c 'import bitarray; bitarray.test()'
bitarray is installed in: /Users/ilan/bitarray/bitarray
bitarray version: 2.5.1
sys.version: 3.9.4 (default, May 10 2021, 22:13:15) [Clang 11.1.0]
sys.prefix: /Users/ilan/Mini3/envs/py39
pointer size: 64 bit
sizeof(size_t): 8
sizeof(bitarrayobject): 80
PY_UINT64_T defined: 1
USE_WORD_SHIFT: 1
DEBUG: 0
.........................................................................
.........................................................................
................................................................
----------------------------------------------------------------------
Ran 433 tests in 0.531s
OK
You can always import the function test,
and ``test().wasSuccessful()`` will return ``True`` when the test went well.
Using the module
----------------
As mentioned above, bitarray objects behave very much like lists, so
there is not too much to learn. The biggest difference from list
objects (except that bitarray are obviously homogeneous) is the ability
to access the machine representation of the object.
When doing so, the bit endianness is of importance; this issue is
explained in detail in the section below. Here, we demonstrate the
basic usage of bitarray objects:
.. code-block:: python
>>> from bitarray import bitarray
>>> a = bitarray() # create empty bitarray
>>> a.append(1)
>>> a.extend([1, 0])
>>> a
bitarray('110')
>>> x = bitarray(2 ** 20) # bitarray of length 1048576 (uninitialized)
>>> len(x)
1048576
>>> bitarray('1001 011') # initialize from string (whitespace is ignored)
bitarray('1001011')
>>> lst = [1, 0, False, True, True]
>>> a = bitarray(lst) # initialize from iterable
>>> a
bitarray('10011')
>>> a.count(1)
3
>>> a.remove(0) # removes first occurrence of 0
>>> a
bitarray('1011')
Like lists, bitarray objects support slice assignment and deletion:
.. code-block:: python
>>> a = bitarray(50)
>>> a.setall(0) # set all elements in a to 0
>>> a[11:37:3] = 9 * bitarray('1')
>>> a
bitarray('00000000000100100100100100100100100100000000000000')
>>> del a[12::3]
>>> a
bitarray('0000000000010101010101010101000000000')
>>> a[-6:] = bitarray('10011')
>>> a
bitarray('000000000001010101010101010100010011')
>>> a += bitarray('000111')
>>> a[9:]
bitarray('001010101010101010100010011000111')
In addition, slices can be assigned to booleans, which is easier (and
faster) than assigning to a bitarray in which all values are the same:
.. code-block:: python
>>> a = 20 * bitarray('0')
>>> a[1:15:3] = True
>>> a
bitarray('01001001001001000000')
This is easier and faster than:
.. code-block:: python
>>> a = 20 * bitarray('0')
>>> a[1:15:3] = 5 * bitarray('1')
>>> a
bitarray('01001001001001000000')
Note that in the latter we have to create a temporary bitarray whose length
must be known or calculated. Another example of assigning slices to Booleans,
is setting ranges:
.. code-block:: python
>>> a = bitarray(30)
>>> a[:] = 0 # set all elements to 0 - equivalent to a.setall(0)
>>> a[10:25] = 1 # set elements in range(10, 25) to 1
>>> a
bitarray('000000000011111111111111100000')
Bitwise operators
-----------------
Bitarray objects support the bitwise operators ``~``, ``&``, ``|``, ``^``,
``<<``, ``>>`` (as well as their in-place versions ``&=``, ``|=``, ``^=``,
``<<=``, ``>>=``). The behavior is very much what one would expect:
.. code-block:: python
>>> a = bitarray('101110001')
>>> ~a # invert
bitarray('010001110')
>>> b = bitarray('111001011')
>>> a ^ b
bitarray('010111010')
>>> a &= b
>>> a
bitarray('101000001')
>>> a <<= 2 # in-place left shift by 2
>>> a
bitarray('100000100')
>>> b >> 1
bitarray('011100101')
The C language does not specify the behavior of negative shifts and
of left shifts larger or equal than the width of the promoted left operand.
The exact behavior is compiler/machine specific.
This Python bitarray library specifies the behavior as follows:
* the length of the bitarray is never changed by any shift operation
* blanks are filled by 0
* negative shifts raise ``ValueError``
* shifts larger or equal to the length of the bitarray result in
bitarrays with all values 0
Bit endianness
--------------
Unless explicitly converting to machine representation, using
the ``.tobytes()``, ``.frombytes()``, ``.tofile()`` and ``.fromfile()``
methods, as well as using ``memoryview``, the bit endianness will have no
effect on any computation, and one can skip this section.
Since bitarrays allows addressing individual bits, where the machine
represents 8 bits in one byte, there are two obvious choices for this
mapping: little-endian and big-endian.
When dealing with the machine representation of bitarray objects, it is
recommended to always explicitly specify the endianness.
By default, bitarrays use big-endian representation:
.. code-block:: python
>>> a = bitarray()
>>> a.endian()
'big'
>>> a.frombytes(b'A')
>>> a
bitarray('01000001')
>>> a[6] = 1
>>> a.tobytes()
b'C'
Big-endian means that the most-significant bit comes first.
Here, ``a[0]`` is the lowest address (index) and most significant bit,
and ``a[7]`` is the highest address and least significant bit.
When creating a new bitarray object, the endianness can always be
specified explicitly:
.. code-block:: python
>>> a = bitarray(endian='little')
>>> a.frombytes(b'A')
>>> a
bitarray('10000010')
>>> a.endian()
'little'
Here, the low-bit comes first because little-endian means that increasing
numeric significance corresponds to an increasing address.
So ``a[0]`` is the lowest address and least significant bit,
and ``a[7]`` is the highest address and most significant bit.
The bit endianness is a property of the bitarray object.
The endianness cannot be changed once a bitarray object is created.
When comparing bitarray objects, the endianness (and hence the machine
representation) is irrelevant; what matters is the mapping from indices
to bits:
.. code-block:: python
>>> bitarray('11001', endian='big') == bitarray('11001', endian='little')
True
Bitwise operations (``|``, ``^``, ``&=``, ``|=``, ``^=``, ``~``) are
implemented efficiently using the corresponding byte operations in C, i.e. the
operators act on the machine representation of the bitarray objects.
Therefore, it is not possible to perform bitwise operators on bitarrays
with different endianness.
When converting to and from machine representation, using
the ``.tobytes()``, ``.frombytes()``, ``.tofile()`` and ``.fromfile()``
methods, the endianness matters:
.. code-block:: python
>>> a = bitarray(endian='little')
>>> a.frombytes(b'\x01')
>>> a
bitarray('10000000')
>>> b = bitarray(endian='big')
>>> b.frombytes(b'\x80')
>>> b
bitarray('10000000')
>>> a == b
True
>>> a.tobytes() == b.tobytes()
False
As mentioned above, the endianness can not be changed once an object is
created. However, you can create a new bitarray with different endianness:
.. code-block:: python
>>> a = bitarray('111000', endian='little')
>>> b = bitarray(a, endian='big')
>>> b
bitarray('111000')
>>> a == b
True
Buffer protocol
---------------
Bitarray objects support the buffer protocol. They can both export their
own buffer, as well as import another object's buffer. To learn more about
this topic, please read `buffer protocol <https://github.com/ilanschnell/bitarray/blob/master/doc/buffer.rst>`__. There is also an example that shows how
to memory-map a file to a bitarray: `mmapped-file.py <https://github.com/ilanschnell/bitarray/blob/master/examples/mmapped-file.py>`__
Variable bit length prefix codes
--------------------------------
The ``.encode()`` method takes a dictionary mapping symbols to bitarrays
and an iterable, and extends the bitarray object with the encoded symbols
found while iterating. For example:
.. code-block:: python
>>> d = {'H':bitarray('111'), 'e':bitarray('0'),
... 'l':bitarray('110'), 'o':bitarray('10')}
...
>>> a = bitarray()
>>> a.encode(d, 'Hello')
>>> a
bitarray('111011011010')
Note that the string ``'Hello'`` is an iterable, but the symbols are not
limited to characters, in fact any immutable Python object can be a symbol.
Taking the same dictionary, we can apply the ``.decode()`` method which will
return a list of the symbols:
.. code-block:: python
>>> a.decode(d)
['H', 'e', 'l', 'l', 'o']
>>> ''.join(a.decode(d))
'Hello'
Since symbols are not limited to being characters, it is necessary to return
them as elements of a list, rather than simply returning the joined string.
The above dictionary ``d`` can be efficiently constructed using the function
``bitarray.util.huffman_code()``. I also wrote `Huffman coding in Python
using bitarray <http://ilan.schnell-web.net/prog/huffman/>`__ for more
background information.
When the codes are large, and you have many decode calls, most time will
be spent creating the (same) internal decode tree objects. In this case,
it will be much faster to create a ``decodetree`` object, which can be
passed to bitarray's ``.decode()`` and ``.iterdecode()`` methods, instead
of passing the prefix code dictionary to those methods itself:
.. code-block:: python
>>> from bitarray import bitarray, decodetree
>>> t = decodetree({'a': bitarray('0'), 'b': bitarray('1')})
>>> a = bitarray('0110')
>>> a.decode(t)
['a', 'b', 'b', 'a']
>>> ''.join(a.iterdecode(t))
'abba'
The sole purpose of the immutable ``decodetree`` object is to be passed
to bitarray's ``.decode()`` and ``.iterdecode()`` methods.
Frozenbitarrays
---------------
A ``frozenbitarray`` object is very similar to the bitarray object.
The difference is that this a ``frozenbitarray`` is immutable, and hashable,
and can therefore be used as a dictionary key:
.. code-block:: python
>>> from bitarray import frozenbitarray
>>> key = frozenbitarray('1100011')
>>> {key: 'some value'}
{frozenbitarray('1100011'): 'some value'}
>>> key[3] = 1
Traceback (most recent call last):
...
TypeError: frozenbitarray is immutable
Reference
=========
bitarray version: 2.5.1 -- `change log <https://github.com/ilanschnell/bitarray/blob/master/doc/changelog.rst>`__
In the following, ``item`` and ``value`` are usually a single bit -
an integer 0 or 1.
The bitarray object:
--------------------
``bitarray(initializer=0, /, endian='big', buffer=None)`` -> bitarray
Return a new bitarray object whose items are bits initialized from
the optional initial object, and endianness.
The initializer may be of the following types:
``int``: Create a bitarray of given integer length. The initial values are
uninitialized.
``str``: Create bitarray from a string of ``0`` and ``1``.
``iterable``: Create bitarray from iterable or sequence or integers 0 or 1.
Optional keyword arguments:
``endian``: Specifies the bit endianness of the created bitarray object.
Allowed values are ``big`` and ``little`` (the default is ``big``).
The bit endianness effects the buffer representation of the bitarray.
``buffer``: Any object which exposes a buffer. When provided, ``initializer``
cannot be present (or has to be ``None``). The imported buffer may be
readonly or writable, depending on the object type.
New in version 2.3: optional ``buffer`` argument.
bitarray methods:
-----------------
``all()`` -> bool
Return True when all bits in the array are True.
Note that ``a.all()`` is faster than ``all(a)``.
``any()`` -> bool
Return True when any bit in the array is True.
Note that ``a.any()`` is faster than ``any(a)``.
``append(item, /)``
Append ``item`` to the end of the bitarray.
``buffer_info()`` -> tuple
Return a tuple containing:
0. memory address of buffer
1. buffer size (in bytes)
2. bit endianness as a string
3. number of unused padding bits
4. allocated memory for the buffer (in bytes)
5. memory is read-only
6. buffer is imported
7. number of buffer exports
``bytereverse(start=0, stop=<end of buffer>, /)``
Reverse the order of bits in byte-range(start, stop) in-place.
The start and stop indices are given in terms of bytes (not bits).
Also note that this method only changes the buffer; it does not change the
endianness of the bitarray object.
New in version 2.2.5: optional start and stop arguments.
``clear()``
Remove all items from the bitarray.
New in version 1.4.
``copy()`` -> bitarray
Return a copy of the bitarray.
``count(value=1, start=0, stop=<end of array>, step=1, /)`` -> int
Count the number of occurrences of ``value`` in the bitarray.
New in version 1.1.0: optional start and stop arguments.
New in version 2.3.7: optional step argument.
``decode(code, /)`` -> list
Given a prefix code (a dict mapping symbols to bitarrays, or ``decodetree``
object), decode the content of the bitarray and return it as a list of
symbols.
``encode(code, iterable, /)``
Given a prefix code (a dict mapping symbols to bitarrays),
iterate over the iterable object with symbols, and extend the bitarray
with the corresponding bitarray for each symbol.
``endian()`` -> str
Return the bit endianness of the bitarray as a string (``little`` or ``big``).
``extend(iterable, /)``
Append all items from ``iterable`` to the end of the bitarray.
If the iterable is a string, each ``0`` and ``1`` are appended as
bits (ignoring whitespace and underscore).
``fill()`` -> int
Add zeros to the end of the bitarray, such that the length of the bitarray
will be a multiple of 8, and return the number of bits added (0..7).
``find(sub_bitarray, start=0, stop=<end of array>, /)`` -> int
Return the lowest index where sub_bitarray is found, such that sub_bitarray
is contained within ``[start:stop]``.
Return -1 when sub_bitarray is not found.
New in version 2.1.
``frombytes(bytes, /)``
Extend the bitarray with raw bytes from a bytes-like object.
Each added byte will add eight bits to the bitarray.
New in version 2.5.0: allow bytes-like argument.
``fromfile(f, n=-1, /)``
Extend bitarray with up to n bytes read from the file object f.
When n is omitted or negative, reads all data until EOF.
When n is provided and positive but exceeds the data available,
``EOFError`` is raised (but the available data is still read and appended.
``index(sub_bitarray, start=0, stop=<end of array>, /)`` -> int
Return the lowest index where sub_bitarray is found, such that sub_bitarray
is contained within ``[start:stop]``.
Raises ``ValueError`` when the sub_bitarray is not present.
``insert(index, value, /)``
Insert ``value`` into the bitarray before ``index``.
``invert(index=<all bits>, /)``
Invert all bits in the array (in-place).
When the optional ``index`` is given, only invert the single bit at index.
New in version 1.5.3: optional index argument.
``iterdecode(code, /)`` -> iterator
Given a prefix code (a dict mapping symbols to bitarrays, or ``decodetree``
object), decode the content of the bitarray and return an iterator over
the symbols.
``itersearch(sub_bitarray, /)`` -> iterator
Searches for the given sub_bitarray in self, and return an iterator over
the start positions where bitarray matches self.
``pack(bytes, /)``
Extend the bitarray from a bytes-like object, where each byte corresponds
to a single bit. The byte ``b'\x00'`` maps to bit 0 and all other bytes
map to bit 1.
This method, as well as the unpack method, are meant for efficient
transfer of data between bitarray objects to other python objects
(for example NumPy's ndarray object) which have a different memory view.
New in version 2.5.0: allow bytes-like argument.
``pop(index=-1, /)`` -> item
Return the i-th (default last) element and delete it from the bitarray.
Raises ``IndexError`` if bitarray is empty or index is out of range.
``remove(value, /)``
Remove the first occurrence of ``value`` in the bitarray.
Raises ``ValueError`` if item is not present.
``reverse()``
Reverse all bits in the array (in-place).
``search(sub_bitarray, limit=<none>, /)`` -> list
Searches for the given sub_bitarray in self, and return the list of start
positions.
The optional argument limits the number of search results to the integer
specified. By default, all search results are returned.
``setall(value, /)``
Set all elements in the bitarray to ``value``.
Note that ``a.setall(value)`` is equivalent to ``a[:] = value``.
``sort(reverse=False)``
Sort the bits in the array (in-place).
``to01()`` -> str
Return a string containing '0's and '1's, representing the bits in the
bitarray.
``tobytes()`` -> bytes
Return the bitarray buffer in bytes (unused bits are set to zero).
``tofile(f, /)``
Write the byte representation of the bitarray to the file object f.
``tolist()`` -> list
Return a list with the items (0 or 1) in the bitarray.
Note that the list object being created will require 32 or 64 times more
memory (depending on the machine architecture) than the bitarray object,
which may cause a memory error if the bitarray is very large.
``unpack(zero=b'\x00', one=b'\x01')`` -> bytes
Return bytes containing one character for each bit in the bitarray,
using the specified mapping.
Other objects:
--------------
``frozenbitarray(initializer=0, /, endian='big', buffer=None)`` -> frozenbitarray
Return a frozenbitarray object, which is initialized the same way a bitarray
object is initialized. A frozenbitarray is immutable and hashable.
Its contents cannot be altered after it is created; however, it can be used
as a dictionary key.
New in version 1.1.
``decodetree(code, /)`` -> decodetree
Given a prefix code (a dict mapping symbols to bitarrays),
create a binary tree object to be passed to ``.decode()`` or ``.iterdecode()``.
New in version 1.6.
Functions defined in the `bitarray` module:
-------------------------------------------
``bits2bytes(n, /)`` -> int
Return the number of bytes necessary to store n bits.
``get_default_endian()`` -> string
Return the default endianness for new bitarray objects being created.
Unless ``_set_default_endian()`` is called, the return value is ``big``.
New in version 1.3.
``test(verbosity=1, repeat=1)`` -> TextTestResult
Run self-test, and return unittest.runner.TextTestResult object.
Functions defined in `bitarray.util` module:
--------------------------------------------
This sub-module was added in version 1.2.
``zeros(length, /, endian=None)`` -> bitarray
Create a bitarray of length, with all values 0, and optional
endianness, which may be 'big', 'little'.
``urandom(length, /, endian=None)`` -> bitarray
Return a bitarray of ``length`` random bits (uses ``os.urandom``).
New in version 1.7.
``pprint(bitarray, /, stream=None, group=8, indent=4, width=80)``
Prints the formatted representation of object on ``stream`` (which defaults
to ``sys.stdout``). By default, elements are grouped in bytes (8 elements),
and 8 bytes (64 elements) per line.
Non-bitarray objects are printed by the standard library
function ``pprint.pprint()``.
New in version 1.8.
``make_endian(bitarray, /, endian)`` -> bitarray
When the endianness of the given bitarray is different from ``endian``,
return a new bitarray, with endianness ``endian`` and the same elements
as the original bitarray.
Otherwise (endianness is already ``endian``) the original bitarray is returned
unchanged.
New in version 1.3.
``rindex(bitarray, value=1, start=0, stop=<end of array>, /)`` -> int
Return the rightmost (highest) index of ``value`` in bitarray.
Raises ``ValueError`` if the value is not present.
New in version 2.3.0: optional start and stop arguments.
``strip(bitarray, /, mode='right')`` -> bitarray
Return a new bitarray with zeros stripped from left, right or both ends.
Allowed values for mode are the strings: ``left``, ``right``, ``both``
``count_n(a, n, value=1, /)`` -> int
Return lowest index ``i`` for which ``a[:i].count(value) == n``.
Raises ``ValueError``, when n exceeds total count (``a.count(value)``).
New in version 2.3.6: optional value argument.
``parity(a, /)`` -> int
Return the parity of bitarray ``a``.
This is equivalent to ``a.count() % 2`` (but more efficient).
New in version 1.9.
``count_and(a, b, /)`` -> int
Return ``(a & b).count()`` in a memory efficient manner,
as no intermediate bitarray object gets created.
``count_or(a, b, /)`` -> int
Return ``(a | b).count()`` in a memory efficient manner,
as no intermediate bitarray object gets created.
``count_xor(a, b, /)`` -> int
Return ``(a ^ b).count()`` in a memory efficient manner,
as no intermediate bitarray object gets created.
``subset(a, b, /)`` -> bool
Return ``True`` if bitarray ``a`` is a subset of bitarray ``b``.
``subset(a, b)`` is equivalent to ``(a & b).count() == a.count()`` but is more
efficient since we can stop as soon as one mismatch is found, and no
intermediate bitarray object gets created.
``ba2hex(bitarray, /)`` -> hexstr
Return a string containing the hexadecimal representation of
the bitarray (which has to be multiple of 4 in length).
``hex2ba(hexstr, /, endian=None)`` -> bitarray
Bitarray of hexadecimal representation. hexstr may contain any number
(including odd numbers) of hex digits (upper or lower case).
``ba2base(n, bitarray, /)`` -> str
Return a string containing the base ``n`` ASCII representation of
the bitarray. Allowed values for ``n`` are 2, 4, 8, 16, 32 and 64.
The bitarray has to be multiple of length 1, 2, 3, 4, 5 or 6 respectively.
For ``n=16`` (hexadecimal), ``ba2hex()`` will be much faster, as ``ba2base()``
does not take advantage of byte level operations.
For ``n=32`` the RFC 4648 Base32 alphabet is used, and for ``n=64`` the
standard base 64 alphabet is used.
See also: `Bitarray representations <https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst>`__
New in version 1.9.
``base2ba(n, asciistr, /, endian=None)`` -> bitarray
Bitarray of the base ``n`` ASCII representation.
Allowed values for ``n`` are 2, 4, 8, 16, 32 and 64.
For ``n=16`` (hexadecimal), ``hex2ba()`` will be much faster, as ``base2ba()``
does not take advantage of byte level operations.
For ``n=32`` the RFC 4648 Base32 alphabet is used, and for ``n=64`` the
standard base 64 alphabet is used.
See also: `Bitarray representations <https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst>`__
New in version 1.9.
``ba2int(bitarray, /, signed=False)`` -> int
Convert the given bitarray to an integer.
The bit-endianness of the bitarray is respected.
``signed`` indicates whether two's complement is used to represent the integer.
``int2ba(int, /, length=None, endian=None, signed=False)`` -> bitarray
Convert the given integer to a bitarray (with given endianness,
and no leading (big-endian) / trailing (little-endian) zeros), unless
the ``length`` of the bitarray is provided. An ``OverflowError`` is raised
if the integer is not representable with the given number of bits.
``signed`` determines whether two's complement is used to represent the integer,
and requires ``length`` to be provided.
``serialize(bitarray, /)`` -> bytes
Return a serialized representation of the bitarray, which may be passed to
``deserialize()``. It efficiently represents the bitarray object (including
its endianness) and is guaranteed not to change in future releases.
See also: `Bitarray representations <https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst>`__
New in version 1.8.
``deserialize(bytes, /)`` -> bitarray
Return a bitarray given a bytes-like representation such as returned
by ``serialize()``.
See also: `Bitarray representations <https://github.com/ilanschnell/bitarray/blob/master/doc/represent.rst>`__
New in version 1.8.
New in version 2.5.0: allow bytes-like argument.
``vl_encode(bitarray, /)`` -> bytes
Return variable length binary representation of bitarray.
This representation is useful for efficiently storing small bitarray
in a binary stream. Use ``vl_decode()`` for decoding.
See also: `Variable length bitarray format <https://github.com/ilanschnell/bitarray/blob/master/doc/variable_length.rst>`__
New in version 2.2.
``vl_decode(stream, /, endian=None)`` -> bitarray
Decode binary stream (an integer iterator, or bytes-like object), and return
the decoded bitarray. This function consumes only one bitarray and leaves
the remaining stream untouched. ``StopIteration`` is raised when no
terminating byte is found.
Use ``vl_encode()`` for encoding.
See also: `Variable length bitarray format <https://github.com/ilanschnell/bitarray/blob/master/doc/variable_length.rst>`__
New in version 2.2.
``huffman_code(dict, /, endian=None)`` -> dict
Given a frequency map, a dictionary mapping symbols to their frequency,
calculate the Huffman code, i.e. a dict mapping those symbols to
bitarrays (with given endianness). Note that the symbols are not limited
to being strings. Symbols may may be any hashable object (such as ``None``).
``canonical_huffman(dict, /)`` -> tuple
Given a frequency map, a dictionary mapping symbols to their frequency,
calculate the canonical Huffman code. Returns a tuple containing:
0. the canonical Huffman code as a dict mapping symbols to bitarrays
1. a list containing the number of symbols of each code length
2. a list of symbols in canonical order
Note: the two lists may be used as input for ``canonical_decode()``.
See also: `Canonical Huffman Coding <https://github.com/ilanschnell/bitarray/blob/master/doc/canonical.rst>`__
New in version 2.5.
``canonical_decode(bitarray, count, symbol, /)`` -> iterator
Decode bitarray using canonical Huffman decoding tables
where ``count`` is a sequence containing the number of symbols of each length
and ``symbol`` is a sequence of symbols in canonical order.
See also: `Canonical Huffman Coding <https://github.com/ilanschnell/bitarray/blob/master/doc/canonical.rst>`__
New in version 2.5.
|