Package: bitarray
bitarray: efficient arrays of booleansThis 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.RoadmapIn 2024 (probably around July), we are planning the release of bitarray 3.0.
The 3.0 release will:Remove Python 2.7 support.Rename.itersearch()to.search()and.iterdecode()to.decode()(and remove their non-iterator counterpart).Removeutil.rindex(), use.index(...,right=1)insteadRemoveutil.make_endian(), usebitarray(...,endian=...)insteadRemove hackish support forbitarray()handling unpickling,
see detailed explaination in#207.  This will close#206.Key featuresThe bit-endianness can be specified for each bitarray object, see below.Sequence methods: slicing (including slice assignment and deletion),
operations+,*,+=,*=, theinoperator,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.Immutablefrozenbitarrayobjects which are hashableSequential searchType hintingExtensive test suite with about 500 unittests.Utility modulebitarray.util:conversion to and from hexadecimal strings(de-) serializationpretty printingconversion to and from integerscreating Huffman codescompression of sparse bitarraysvarious count functionsother helpful functionsInstallationPython wheels are are available on PyPI for all mayor platforms and Python
versions.  Which means you can simply:$pipinstallbitarrayIn addition, conda packages are available (both the default Anaconda
repository as well as conda-forge support bitarray):$condainstallbitarrayOnce you have installed the package, you may want to test it:$python-c'import bitarray; bitarray.test()'bitarray is installed in: /Users/ilan/bitarray/bitarray
bitarray version: 2.9.2
sys.version: 3.11.0 (main, Oct 25 2022) [Clang 14.0.4]
sys.prefix: /Users/ilan/Mini3/envs/py311
pointer size: 64 bit
sizeof(size_t): 8
sizeof(bitarrayobject): 80
HAVE_BUILTIN_BSWAP64: 1
default bit-endianness: big
machine byte-order: little
DEBUG: 0
.........................................................................
.........................................................................
................................................................
----------------------------------------------------------------------
Ran 502 tests in 0.416s

OKThetest()function is part of the API.  It will return
aunittest.runner.TextTestResultobject, such that one can verify that
all tests ran successfully by:importbitarrayassertbitarray.test().wasSuccessful()UsageAs 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:>>>frombitarrayimportbitarray>>>a=bitarray()# create empty bitarray>>>a.append(1)>>>a.extend([1,0])>>>abitarray('110')>>>x=bitarray(2**20)# bitarray of length 1048576 (initialized to 0)>>>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>>>abitarray('10011')>>>a[2]# indexing a single item will always return an integer0>>>a[2:4]# whereas indexing a slice will always return a bitarraybitarray('01')>>>a[2:3]# even when the slice length is just onebitarray('0')>>>a.count(1)3>>>a.remove(0)# removes first occurrence of 0>>>abitarray('1011')Like lists, bitarray objects support slice assignment and deletion:>>>a=bitarray(50)>>>a.setall(0)# set all elements in a to 0>>>a[11:37:3]=9*bitarray('1')>>>abitarray('00000000000100100100100100100100100100000000000000')>>>dela[12::3]>>>abitarray('0000000000010101010101010101000000000')>>>a[-6:]=bitarray('10011')>>>abitarray('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:>>>a=20*bitarray('0')>>>a[1:15:3]=True>>>abitarray('01001001001001000000')This is easier and faster than:>>>a=20*bitarray('0')>>>a[1:15:3]=5*bitarray('1')>>>abitarray('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:>>>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>>>abitarray('000000000011111111111111100000')As of bitarray version 2.8, indices may also be lists of arbitrary
indices (like in NumPy), or bitarrays that are treated as masks,
seeBitarray indexing.Bitwise operatorsBitarray objects support the bitwise operators~,&,|,^,<<,>>(as well as their in-place versions&=,|=,^=,<<=,>>=).  The behavior is very much what one would expect:>>>a=bitarray('101110001')>>>~a# invertbitarray('010001110')>>>b=bitarray('111001011')>>>a^bbitarray('010111010')>>>a&=b>>>abitarray('101000001')>>>a<<=2# in-place left shift by 2>>>abitarray('100000100')>>>b>>1bitarray('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 operationblanks are filled by 0negative shifts raiseValueErrorshifts larger or equal to the length of the bitarray result in
bitarrays with all values 0It is worth noting that (regardless of bit-endianness) the bitarray left
shift (<<) always shifts towards lower indices, and the right
shift (>>) always shifts towards higher indices.Bit-endiannessUnless explicitly converting to machine representation, using
the.tobytes(),.frombytes(),.tofile()and.fromfile()methods, as well as usingmemoryview, 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:>>>a=bitarray()>>>a.endian()'big'>>>a.frombytes(b'A')>>>abitarray('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,
anda[7]is the highest address and least significant bit.When creating a new bitarray object, the endianness can always be
specified explicitly:>>>a=bitarray(endian='little')>>>a.frombytes(b'A')>>>abitarray('10000010')>>>a.endian()'little'Here, the low-bit comes first because little-endian means that increasing
numeric significance corresponds to an increasing address.
Soa[0]is the lowest address and least significant bit,
anda[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:>>>bitarray('11001',endian='big')==bitarray('11001',endian='little')TrueBitwise 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:>>>a=bitarray(endian='little')>>>a.frombytes(b'\x01')>>>abitarray('10000000')>>>b=bitarray(endian='big')>>>b.frombytes(b'\x80')>>>bbitarray('10000000')>>>a==bTrue>>>a.tobytes()==b.tobytes()FalseAs mentioned above, the endianness can not be changed once an object is
created.  However, you can create a new bitarray with different endianness:>>>a=bitarray('111000',endian='little')>>>b=bitarray(a,endian='big')>>>bbitarray('111000')>>>a==bTrueBuffer protocolBitarray 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 readbuffer protocol.  There is also an example that shows how
to memory-map a file to a bitarray:mmapped-file.pyVariable bit length prefix codesThe.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:>>>d={'H':bitarray('111'),'e':bitarray('0'),...'l':bitarray('110'),'o':bitarray('10')}...>>>a=bitarray()>>>a.encode(d,'Hello')>>>abitarray('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:>>>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 dictionarydcan be efficiently constructed using the functionbitarray.util.huffman_code().  I also wroteHuffman coding in Python
using bitarrayfor 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 adecodetreeobject, which can be
passed to bitarray’s.decode()and.iterdecode()methods, instead
of passing the prefix code dictionary to those methods itself:>>>frombitarrayimportbitarray,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 immutabledecodetreeobject is to be passed
to bitarray’s.decode()and.iterdecode()methods.FrozenbitarraysAfrozenbitarrayobject is very similar to the bitarray object.
The difference is that this afrozenbitarrayis immutable, and hashable,
and can therefore be used as a dictionary key:>>>frombitarrayimportfrozenbitarray>>>key=frozenbitarray('1100011')>>>{key:'some value'}{frozenbitarray('1100011'):'some value'}>>>key[3]=1Traceback(mostrecentcalllast):...TypeError:frozenbitarrayisimmutableReferencebitarray version: 2.9.2 –change logIn the following,itemandvalueare usually a single bit -
an integer 0 or 1.Also,sub_bitarrayrefers to either a bitarray, or anitem.The bitarray object:bitarray(initializer=0, /,endian='big',buffer=None)-> bitarrayReturn 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
all0.str: Create bitarray from a string of0and1.iterable: Create bitarray from iterable or sequence of integers 0 or 1.Optional keyword arguments:endian: Specifies the bit-endianness of the created bitarray object.
Allowed values arebigandlittle(the default isbig).
The bit-endianness effects the buffer representation of the bitarray.buffer: Any object which exposes a buffer.  When provided,initializercannot be present (or has to beNone).  The imported buffer may be
read-only or writable, depending on the object type.New in version 2.3: optionalbufferargument.bitarray methods:all()-> boolReturn True when all bits in bitarray are True.
Note thata.all()is faster thanall(a).any()-> boolReturn True when any bit in bitarray is True.
Note thata.any()is faster thanany(a).append(item, /)Appenditemto the end of the bitarray.buffer_info()-> tupleReturn a tuple containing:memory address of bufferbuffer size (in bytes)bit-endianness as a stringnumber of pad bitsallocated memory for the buffer (in bytes)memory is read-onlybuffer is importednumber of buffer exportsbytereverse(start=0,stop=<endof buffer>, /)For each byte in byte-range(start, stop) reverse bits 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.  Padbits are left unchanged such that
two consecutive calls will always leave the bitarray unchanged.New in version 2.2.5: optional start and stop arguments.clear()Remove all items from the bitarray.New in version 1.4.copy()-> bitarrayReturn a copy of the bitarray.count(value=1, start=0,stop=<end>,step=1, /)-> intNumber of occurrences ofvaluebitarray within[start:stop:step].
Optional argumentsstart,stopandstepare interpreted in
slice notation, meaninga.count(value, start, stop, step)equalsa[start:stop:step].count(value).
Thevaluemay also be a sub-bitarray.  In this case non-overlapping
occurrences are counted within[start:stop](stepmust be 1).New in version 1.1.0: optional start and stop arguments.New in version 2.3.7: optional step argument.New in version 2.9: add non-overlapping sub-bitarray count.decode(code, /)-> listGiven a prefix code (a dict mapping symbols to bitarrays, ordecodetreeobject), decode content of 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 bitarray
with corresponding bitarray for each symbol.endian()-> strReturn the bit-endianness of the bitarray as a string (littleorbig).extend(iterable, /)Append all items fromiterableto the end of the bitarray.
If the iterable is a string, each0and1are appended as
bits (ignoring whitespace and underscore).fill()-> intAdd zeros to the end of the bitarray, such that the length will be
a multiple of 8, and return the number of bits added [0..7].find(sub_bitarray, start=0,stop=<end>,/, right=False)-> intReturn lowest (or rightmost whenright=True) 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.New in version 2.9: add optional keyword argumentright.frombytes(bytes, /)Extend 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 tonbytes read from file objectf(or any
other binary stream what supports a.read()method, e.g.io.BytesIO).
Each read byte will add eight bits to the bitarray.  Whennis omitted or
negative, all bytes until EOF are read.  Whennis non-negative but
exceeds the data available,EOFErroris raised (but the available data
is still read and appended).index(sub_bitarray, start=0,stop=<end>,/, right=False)-> intReturn lowest (or rightmost whenright=True) index where sub_bitarray
is found, such that sub_bitarray is contained within[start:stop].
RaisesValueErrorwhen the sub_bitarray is not present.New in version 2.9: add optional keyword argumentright.insert(index, value, /)Insertvalueinto bitarray beforeindex.invert(index=<allbits>, /)Invert all bits in bitarray (in-place).
When the optionalindexis given, only invert the single bit at index.New in version 1.5.3: optional index argument.iterdecode(code, /)-> iteratorGiven a prefix code (a dict mapping symbols to bitarrays, ordecodetreeobject), decode content of bitarray and return an iterator over
the symbols.itersearch(sub_bitarray, start=0,stop=<end>,/, right=False)-> iteratorReturn iterator over indices where sub_bitarray is found, such that
sub_bitarray is contained within[start:stop].
The indices are iterated in ascending order (from lowest to highest),
unlessright=True, which will iterate in descending oder (starting with
rightmost match).New in version 2.9: optional start and stop arguments - add optional keyword argumentright.pack(bytes, /)Extend bitarray from a bytes-like object, where each byte corresponds
to a single bit.  The byteb'\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,/)-> itemRemove and return item atindex(default last).
RaisesIndexErrorif index is out of range.remove(value, /)Remove the first occurrence ofvalue.
RaisesValueErrorif value is not present.reverse()Reverse all bits in bitarray (in-place).search(sub_bitarray,limit=<none>,/)-> listSearches for given sub_bitarray in self, and return 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 bitarray tovalue.
Note thata.setall(value)is equivalent toa[:]= value.sort(reverse=False)Sort all bits in bitarray (in-place).to01()-> strReturn a string containing ‘0’s and ‘1’s, representing the bits in the
bitarray.tobytes()-> bytesReturn the bitarray buffer in bytes (pad bits are set to zero).tofile(f, /)Write byte representation of bitarray to file object f.tolist()-> listReturn bitarray as list of integer items.a.tolist()is equal tolist(a).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')-> bytesReturn bytes containing one character for each bit in the bitarray,
using specified mapping.bitarray data descriptors:Data descriptors were added in version 2.6.nbytes-> intbuffer size in bytespadbits-> intnumber of pad bitsreadonly-> boolbool indicating whether buffer is read-onlyOther objects:frozenbitarray(initializer=0, /,endian='big',buffer=None)-> frozenbitarrayReturn afrozenbitarrayobject.  Initialized the same way abitarrayobject is initialized.  Afrozenbitarrayis immutable and hashable,
and may therefore be used as a dictionary key.New in version 1.1.decodetree(code, /)-> decodetreeGiven 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 thebitarraymodule:bits2bytes(n, /)-> intReturn the number of bytes necessary to store n bits.get_default_endian()-> strReturn the default endianness for new bitarray objects being created.
Unless_set_default_endian('little')was called, the default endianness
isbig.New in version 1.3.test(verbosity=1)-> TextTestResultRun self-test, and return unittest.runner.TextTestResult object.Functions defined inbitarray.utilmodule:This sub-module was added in version 1.2.zeros(length, /, endian=None)-> bitarrayCreate a bitarray of length, with all values 0, and optional
endianness, which may be ‘big’, ‘little’.ones(length, /, endian=None)-> bitarrayCreate a bitarray of length, with all values 1, and optional
endianness, which may be ‘big’, ‘little’.New in version 2.9.urandom(length, /, endian=None)-> bitarrayReturn a bitarray oflengthrandom bits (usesos.urandom).New in version 1.7.pprint(bitarray, /, stream=None, group=8, indent=4, width=80)Prints the formatted representation of object onstream(which defaults
tosys.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
functionpprint.pprint().New in version 1.8.make_endian(bitarray, /, endian)-> bitarrayWhen the endianness of the given bitarray is different fromendian,
return a new bitarray, with endiannessendianand the same elements
as the original bitarray.
Otherwise (endianness is alreadyendian) the original bitarray is returned
unchanged.New in version 1.3.New in version 2.9: deprecated - usebitarray().rindex(bitarray, sub_bitarray=1, start=0,stop=<end>,/)-> intReturn rightmost (highest) index where sub_bitarray (or item - defaults
to 1) is found in bitarray (a), such that sub_bitarray is contained
withina[start:stop].
RaisesValueErrorwhen the sub_bitarray is not present.New in version 2.3.0: optional start and stop arguments.New in version 2.9: deprecated - use.index(...,right=1).strip(bitarray, /,mode='right')-> bitarrayReturn a new bitarray with zeros stripped from left, right or both ends.
Allowed values for mode are the strings:left,right,bothcount_n(a, n, value=1, /)-> intReturn lowest indexifor whicha[:i].count(value)== n.
RaisesValueErrorwhennexceeds total count (a.count(value)).New in version 2.3.6: optional value argument.parity(a, /)-> intReturn parity of bitarraya.parity(a)is equivalent toa.count() % 2but more efficient.New in version 1.9.count_and(a, b, /)-> intReturn(a &b).count()in a memory efficient manner,
as no intermediate bitarray object gets created.count_or(a, b, /)-> intReturn(a |b).count()in a memory efficient manner,
as no intermediate bitarray object gets created.count_xor(a, b, /)-> intReturn(a ^b).count()in a memory efficient manner,
as no intermediate bitarray object gets created.This is also known as the Hamming distance.any_and(a, b, /)-> boolEfficient implementation ofany(a & b).New in version 2.7.subset(a, b, /)-> boolReturnTrueif bitarrayais a subset of bitarrayb.subset(a, b)is equivalent toa | b == b(and equallya & b == a) but
more efficient as no intermediate bitarray object is created and the buffer
iteration is stopped as soon as one mismatch is found.intervals(bitarray, /)-> iteratorCompute all uninterrupted intervals of 1s and 0s, and return an
iterator over tuples(value, start, stop).  The intervals are guaranteed
to be in order, and their size is always non-zero (stop - start > 0).New in version 2.7.ba2hex(bitarray, /)-> hexstrReturn a string containing the hexadecimal representation of
the bitarray (which has to be multiple of 4 in length).hex2ba(hexstr, /, endian=None)-> bitarrayBitarray of hexadecimal representation.  hexstr may contain any number
(including odd numbers) of hex digits (upper or lower case).ba2base(n, bitarray, /)-> strReturn a string containing the basenASCII representation of
the bitarray.  Allowed values fornare 2, 4, 8, 16, 32 and 64.
The bitarray has to be multiple of length 1, 2, 3, 4, 5 or 6 respectively.
Forn=32the RFC 4648 Base32 alphabet is used, and forn=64the
standard base 64 alphabet is used.See also:Bitarray representationsNew in version 1.9.base2ba(n, asciistr, /, endian=None)-> bitarrayBitarray of basenASCII representation.
Allowed values fornare 2, 4, 8, 16, 32 and 64.
Forn=32the RFC 4648 Base32 alphabet is used, and forn=64the
standard base 64 alphabet is used.See also:Bitarray representationsNew in version 1.9.ba2int(bitarray, /, signed=False)-> intConvert the given bitarray to an integer.
The bit-endianness of the bitarray is respected.signedindicates whether two’s complement is used to represent the integer.int2ba(int, /, length=None, endian=None, signed=False)-> bitarrayConvert the given integer to a bitarray (with given endianness,
and no leading (big-endian) / trailing (little-endian) zeros), unless
thelengthof the bitarray is provided.  AnOverflowErroris raised
if the integer is not representable with the given number of bits.signeddetermines whether two’s complement is used to represent the integer,
and requireslengthto be provided.serialize(bitarray, /)-> bytesReturn a serialized representation of the bitarray, which may be passed todeserialize().  It efficiently represents the bitarray object (including
its bit-endianness) and is guaranteed not to change in future releases.See also:Bitarray representationsNew in version 1.8.deserialize(bytes, /)-> bitarrayReturn a bitarray given a bytes-like representation such as returned
byserialize().See also:Bitarray representationsNew in version 1.8.New in version 2.5.0: allow bytes-like argument.sc_encode(bitarray, /)-> bytesCompress a sparse bitarray and return its binary representation.
This representation is useful for efficiently storing sparse bitarrays.
Usesc_decode()for decompressing (decoding).See also:Compression of sparse bitarraysNew in version 2.7.sc_decode(stream)-> bitarrayDecompress binary stream (an integer iterator, or bytes-like object) of a
sparse compressed (sc) bitarray, and return the decoded  bitarray.
This function consumes only one bitarray and leaves the remaining stream
untouched.  Usesc_encode()for compressing (encoding).See also:Compression of sparse bitarraysNew in version 2.7.vl_encode(bitarray, /)-> bytesReturn variable length binary representation of bitarray.
This representation is useful for efficiently storing small bitarray
in a binary stream.  Usevl_decode()for decoding.See also:Variable length bitarray formatNew in version 2.2.vl_decode(stream, /, endian=None)-> bitarrayDecode 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.  Usevl_encode()for encoding.See also:Variable length bitarray formatNew in version 2.2.huffman_code(dict, /, endian=None)-> dictGiven 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 asNone).canonical_huffman(dict, /)-> tupleGiven a frequency map, a dictionary mapping symbols to their frequency,
calculate the canonical Huffman code.  Returns a tuple containing:the canonical Huffman code as a dict mapping symbols to bitarraysa list containing the number of symbols of each code lengtha list of symbols in canonical orderNote: the two lists may be used as input forcanonical_decode().See also:Canonical Huffman CodingNew in version 2.5.canonical_decode(bitarray, count, symbol, /)-> iteratorDecode bitarray using canonical Huffman decoding tables
wherecountis a sequence containing the number of symbols of each length
andsymbolis a sequence of symbols in canonical order.See also:Canonical Huffman CodingNew in version 2.5.