__init__.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (c) 2008 - 2025, Ilan Schnell; All Rights Reserved
  2. """
  3. This package defines an object type which can efficiently represent
  4. a bitarray. Bitarrays are sequence types and behave very much like lists.
  5. Please find a description of this package at:
  6. https://github.com/ilanschnell/bitarray
  7. Author: Ilan Schnell
  8. """
  9. from collections import namedtuple
  10. from bitarray._bitarray import (
  11. bitarray, decodetree, bits2bytes, _bitarray_reconstructor,
  12. get_default_endian, _set_default_endian, _sysinfo,
  13. BITARRAY_VERSION as __version__
  14. )
  15. __all__ = ['bitarray', 'frozenbitarray', 'decodetree', 'bits2bytes']
  16. BufferInfo = namedtuple('BufferInfo',
  17. ['address', 'nbytes', 'endian', 'padbits',
  18. 'alloc', 'readonly', 'imported', 'exports'])
  19. class frozenbitarray(bitarray):
  20. """frozenbitarray(initializer=0, /, endian='big', buffer=None) -> \
  21. frozenbitarray
  22. Return a `frozenbitarray` object. Initialized the same way a `bitarray`
  23. object is initialized. A `frozenbitarray` is immutable and hashable,
  24. and may therefore be used as a dictionary key.
  25. """
  26. def __init__(self, *args, **kwargs):
  27. self._freeze()
  28. def __repr__(self):
  29. return 'frozen' + bitarray.__repr__(self)
  30. def __hash__(self):
  31. "Return hash(self)."
  32. # ensure hash is independent of endianness
  33. a = bitarray(self, 'big')
  34. return hash((len(a), a.tobytes()))
  35. # Technically the code below is not necessary, as all these methods will
  36. # raise a TypeError on read-only memory. However, with a different error
  37. # message.
  38. def __delitem__(self, *args, **kwargs):
  39. "" # no docstring
  40. raise TypeError("frozenbitarray is immutable")
  41. append = bytereverse = clear = extend = encode = fill = __delitem__
  42. frombytes = fromfile = insert = invert = pack = pop = __delitem__
  43. remove = reverse = setall = sort = __setitem__ = __delitem__
  44. __iadd__ = __iand__ = __imul__ = __ior__ = __ixor__ = __delitem__
  45. __ilshift__ = __irshift__ = __delitem__
  46. def test(verbosity=1):
  47. """test(verbosity=1) -> TextTestResult
  48. Run self-test, and return `unittest.runner.TextTestResult` object.
  49. """
  50. from bitarray import test_bitarray
  51. return test_bitarray.run(verbosity=verbosity)