Functions#
pack#
- pack(fmt[, *values, **kwargs])#
Packs the values and keyword arguments according to the fmt string and returns a new
Bitsobject.
The format string consists of comma separated tokens, see Format tokens and Compact format strings for details.
A Dtype is accepted wherever its string form is, as it is by Bits.unpack, so pack(Dtype('u8'), 5) and pack('u8', 5) are the same.
The tokens can be ‘literals’, like 0xef, 0b110, u8=55, etc. which just represent a set sequence of bits.
They can also have the value missing, in which case the values contained in *values will be used.
>>> a = pack('bin3, hex4', '001', 'f')
>>> b = pack('u10', 33)
A dictionary or keyword arguments can also be provided. These will replace items in the format string.
>>> c = pack('i:a=b', a=10, b=20)
>>> d = pack('i8=a, bin=b, i4=a', a=7, b='0b110')
Plain names can also be used as follows:
>>> e = pack('a, b, b, a', a='0b11', b='0o2')
Tokens starting with an endianness identifier (< or >) imply a struct-like compact format string (see Compact format strings). For example this packs three little-endian 16-bit integers:
>>> f = pack('<3h', 12, 3, 108)
And of course you can combine the different methods in a single pack.
A ValueError will be raised if the *values are not all used up by the format string, and if a value provided doesn’t match the length specified by a token.
As an example of using just the *values arguments we can say:
s = bitstring.pack('hex32, u12, u12', '0x000001b3', 352, 288)
which is equivalent to initialising as:
s = Bits('0x000001b3, u12=352, u12=288')
The advantage of the pack function is if you want to write more general code for creation.
def foo(a, b, c, d):
return bitstring.pack('u8, 0b110, i6, bin, bits', a, b, c, d)
s1 = foo(12, 5, '0b00000', '')
s2 = foo(101, 3, '0b11011', s1)
Note how you can use some tokens without sizes (such as bin and bits in the above example), and use values of any length to fill them.
If the size had been specified then a ValueError would be raised if the parameter given was the wrong length.
Note also how bitstring literals can be used (the 0b110 in the bitstring returned by foo) and these don’t consume any of the items in *values.
You can also include keyword, value pairs (or an equivalent dictionary) as the final parameter(s). The values are then packed according to the positions of the keywords in the format string. This is most easily explained with some examples. Firstly the format string needs to contain parameter names:
format = 'hex32=start_code, u12=width, u12=height'
Then we can make a dictionary with these parameters as keys and pass it to pack:
d = {'start_code': '0x000001b3', 'width': 352, 'height': 288}
s = bitstring.pack(format, **d)
Another method is to pass the same information as keywords at the end of pack’s parameter list:
s = bitstring.pack(format, width=352, height=288, start_code='0x000001b3')
You can include constant bitstring tokens such as ‘0x101’, ‘0xff’, ‘u7=81’ etc. and also use a keyword for the length specifier in the token, for example:
s = bitstring.pack('0xabc, i:n=-1', n=100)
Note that the ':' is required here - it is optional before a numeric length, but a keyword length needs it.
Finally it is also possible just to use a keyword as a token:
s = bitstring.pack('hello, world', world='0x123', hello='0b110')
Exceptions#
There is no module-wide exception base class. Errors are raised as the standard Python type
that fits, so ValueError, TypeError and IndexError are what most code needs to
catch.
- exception ReadError(ValueError)#
Reading or peeking past the end of a bitstring.