construct.lib – entire module

class construct.lib.Container(*args, **kw)

Generic ordered dictionary that allows both key and attribute access, and preserve key order by insertion. Also it uses __call__ method to chain add keys, because **kw does not preserve order.

Struct and Sequence, and few others parsers returns a container, since their members have order so do keys.

Example:

Container([ ("name","anonymous"), ("age",21) ])

Container(name="anonymous")(age=21)

# Note that this syntax does NOT work before python 3.6 due to unordered keyword arguments:
Container(name="anonymous", age=21)

Container(container2)
pop(key, *default)

Removes and returns the value for a given key, raises KeyError if not found.

popitem()

Removes and returns the last key and value from order.

class construct.lib.FlagsContainer(*args, **kw)

Container made to represent a FlagsEnum, only equality skips order. Provides pretty-printing for flags. Only set flags are displayed.

class construct.lib.ListContainer

A generic container for lists. Provides pretty-printing.

class construct.lib.LazyContainer(keysbackend, offsetmap, cached, stream, addoffset, context)

Lazy equivalent to Container. Works the same but parses subcons on first access whenever possible.

class construct.lib.LazyRangeContainer(subcon, subsize, count, stream, addoffset, context)

Lazy equivalent to ListContainer. Works the same but parses subcons on first access whenever possible.

class construct.lib.LazySequenceContainer(count, offsetmap, cached, stream, addoffset, context)

Lazy equivalent to ListContainer. Works the same but parses subcons on first access whenever possible.

construct.lib.integer2bits(number, width)

Converts an integer into its binary representation in a b-string. Width is the amount of bits to generate. If width is larger than the actual amount of bits required to represent number in binary, sign-extension is used. If it’s smaller, the representation is trimmed to width bits. Each bit is represented as either b’x00’ or b’x01’. The most significant is first, big-endian. This is reverse to bits2integer.

Examples:

>>> integer2bits(19, 8)
b'\x00\x00\x00\x01\x00\x00\x01\x01'
construct.lib.integer2bytes(number, width)

Converts a b-string into an integer. This is reverse to bytes2integer.

Examples:

>>> integer2bytes(19,4)
'\x00\x00\x00\x13'
construct.lib.bits2integer(data, signed=False)

Converts a b-string into an integer. Both b‘0’ and b’x00’ are considered zero, and both b‘1’ and b’x01’ are considered one. Set sign to interpret the number as a 2-s complement signed integer. This is reverse to integer2bits.

Examples:

>>> bits2integer(b"\x01\x00\x00\x01\x01")
19
>>> bits2integer(b"10011")
19
construct.lib.bytes2integer(data, signed=False)

Converts a b-string into an integer. This is reverse to integer2bytes.

Examples:

>>> bytes2integer(b'\x00\x00\x00\x13')
19
construct.lib.bytes2bits(data)

Converts between bit and byte representations in b-strings.

Example:

>>> bytes2bits(b'ab')
b"\x00\x01\x01\x00\x00\x00\x00\x01\x00\x01\x01\x00\x00\x00\x01\x00"
construct.lib.bits2bytes(data)

Converts between bit and byte representations in b-strings.

Example:

>>> bits2bytes(b"\x00\x01\x01\x00\x00\x00\x00\x01\x00\x01\x01\x00\x00\x00\x01\x00")
b'ab'
construct.lib.swapbytes(data, linesize=8)

Performs an endianness swap on a b-string.

Example:

>>> swapbytes(b'00011011', 2)
b'11100100'
>>> swapbytes(b'0000000011111111', 8)
b'1111111100000000'
class construct.lib.HexString(data, linesize=16)

Represents bytes that will be hex-dumped when parsing, and un-dumped when building.

See hexdump().

construct.lib.hexdump(data, linesize)

Turns bytes into a unicode string of the format:

>>>print(hexdump(b‘0’ * 100, 16)) 0000 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 0010 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 0020 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 0030 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 0040 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 0050 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 30 0000000000000000 0060 30 30 30 30 0000

construct.lib.hexundump(data, linesize)

Reverse of hexdump().

construct.lib.int2byte(i)

Converts int (0 through 255) into b’...’ character.

construct.lib.byte2int(s)

Converts b’...’ character into int (0 through 255).

construct.lib.str2bytes(s)

Converts ‘...’ str into b’...’ bytes. On PY2 they are equivalent.

construct.lib.bytes2str(b)

Converts b’...’ bytes into str. On PY2 they are equivalent.

construct.lib.str2unicode(b)

Converts ‘...’ str into u’...’ unicode string. On PY3 they are equivalent.

construct.lib.unicode2str(s)

Converts u’...’ string into ‘...’ str. On PY3 they are equivalent.

construct.lib.iteratebytes(s)

Iterates though b’...’ string yielding characters as b’...’ characters. On PY2 iter is the same.

construct.lib.iterateints(s)

Iterates though b’...’ string yielding characters as ints. On PY3 iter is the same.

construct.lib.setglobalfullprinting(enabled)

Sets full printing for all Container instances. When enabled, Container str produces full content of bytes and strings, otherwise and by default, it produces truncated output.

Parameters:enabled – bool to enable or disable full printing, or None to default
construct.lib.getglobalfullprinting()

Used internally.