File: ndarray.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 (19 lines) | stat: -rw-r--r-- 398 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#
# This example illusatrates how binary data can be efficiently be passed
# between a bitarray object and an ndarray with dtype bool
#
import bitarray
import numpy  # type: ignore

a = bitarray.bitarray('100011001001')
print(a)

# bitarray  ->  ndarray
b = numpy.frombuffer(a.unpack(), dtype=bool)
print(repr(b))

# ndarray  ->  bitarray
c = bitarray.bitarray()
c.pack(b.tobytes())

assert a == c