File: __init__.py

package info (click to toggle)
python-bitarray 3.7.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,320 kB
  • sloc: python: 11,753; ansic: 7,680; makefile: 73; sh: 6
file content (66 lines) | stat: -rw-r--r-- 2,283 bytes parent folder | download
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
# Copyright (c) 2008 - 2025, Ilan Schnell; All Rights Reserved
"""
This package defines an object type which can efficiently represent
a bitarray.  Bitarrays are sequence types and behave very much like lists.

Please find a description of this package at:

    https://github.com/ilanschnell/bitarray

Author: Ilan Schnell
"""
from collections import namedtuple

from bitarray._bitarray import (
    bitarray, decodetree, bits2bytes, _bitarray_reconstructor,
    get_default_endian, _set_default_endian, _sysinfo,
    BITARRAY_VERSION as __version__
)

__all__ = ['bitarray', 'frozenbitarray', 'decodetree', 'bits2bytes']

BufferInfo = namedtuple('BufferInfo',
                        ['address', 'nbytes', 'endian', 'padbits',
                         'alloc', 'readonly', 'imported', 'exports'])

class frozenbitarray(bitarray):
    """frozenbitarray(initializer=0, /, endian='big', buffer=None) -> \
frozenbitarray

Return a `frozenbitarray` object.  Initialized the same way a `bitarray`
object is initialized.  A `frozenbitarray` is immutable and hashable,
and may therefore be used as a dictionary key.
"""
    def __init__(self, *args, **kwargs):
        self._freeze()

    def __repr__(self):
        return 'frozen' + bitarray.__repr__(self)

    def __hash__(self):
        "Return hash(self)."
        # ensure hash is independent of endianness
        a = bitarray(self, 'big')
        return hash((len(a), a.tobytes()))

    # Technically the code below is not necessary, as all these methods will
    # raise a TypeError on read-only memory.  However, with a different error
    # message.
    def __delitem__(self, *args, **kwargs):
        ""  # no docstring
        raise TypeError("frozenbitarray is immutable")

    append = bytereverse = clear = extend = encode = fill = __delitem__
    frombytes = fromfile = insert = invert = pack = pop = __delitem__
    remove = reverse = setall = sort = __setitem__ = __delitem__
    __iadd__ = __iand__ = __imul__ = __ior__ = __ixor__ = __delitem__
    __ilshift__ = __irshift__ = __delitem__


def test(verbosity=1):
    """test(verbosity=1) -> TextTestResult

Run self-test, and return `unittest.runner.TextTestResult` object.
"""
    from bitarray import test_bitarray
    return test_bitarray.run(verbosity=verbosity)