Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/bitarray/__init__.py: 67%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

24 statements  

1# Copyright (c) 2008 - 2025, Ilan Schnell; All Rights Reserved 

2""" 

3This package defines an object type which can efficiently represent 

4a bitarray. Bitarrays are sequence types and behave very much like lists. 

5 

6Please find a description of this package at: 

7 

8 https://github.com/ilanschnell/bitarray 

9 

10Author: Ilan Schnell 

11""" 

12from collections import namedtuple 

13 

14from bitarray._bitarray import ( 

15 bitarray, decodetree, bits2bytes, _bitarray_reconstructor, 

16 get_default_endian, _sysinfo, BITARRAY_VERSION as __version__ 

17) 

18 

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

20 

21BufferInfo = namedtuple('BufferInfo', 

22 ['address', 'nbytes', 'endian', 'padbits', 

23 'alloc', 'readonly', 'imported', 'exports']) 

24 

25class frozenbitarray(bitarray): 

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

27frozenbitarray 

28 

29Return a `frozenbitarray` object. Initialized the same way a `bitarray` 

30object is initialized. A `frozenbitarray` is immutable and hashable, 

31and may therefore be used as a dictionary key. 

32""" 

33 def __init__(self, *args, **kwargs): 

34 self._freeze() 

35 

36 def __repr__(self): 

37 return 'frozen' + bitarray.__repr__(self) 

38 

39 def __hash__(self): 

40 "Return hash(self)." 

41 # ensure hash is independent of endianness 

42 a = bitarray(self, 'big') 

43 return hash((len(a), a.tobytes())) 

44 

45 # Technically the code below is not necessary, as all these methods will 

46 # raise a TypeError on read-only memory. However, with a different error 

47 # message. 

48 def __delitem__(self, *args, **kwargs): 

49 "" # no docstring 

50 raise TypeError("frozenbitarray is immutable") 

51 

52 append = bytereverse = clear = extend = encode = fill = __delitem__ 

53 frombytes = fromfile = insert = invert = pack = pop = __delitem__ 

54 remove = reverse = setall = sort = __setitem__ = __delitem__ 

55 __iadd__ = __iand__ = __imul__ = __ior__ = __ixor__ = __delitem__ 

56 __ilshift__ = __irshift__ = __delitem__ 

57 

58 

59def test(verbosity=1): 

60 """test(verbosity=1) -> TextTestResult 

61 

62Run self-test, and return `unittest.runner.TextTestResult` object. 

63""" 

64 from bitarray import test_bitarray 

65 return test_bitarray.run(verbosity=verbosity)