construct.lib – entire module

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.bits2integer(data, signed=False)

Converts a bit-string into an integer. 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
construct.lib.byte2int(character)

Converts b’…’ character into (0 through 255) integer.

construct.lib.bytes(countorseq=0)

Backports bytes() from PY3.

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.bytes2integer(data, signed=False)

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

Examples:

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

Converts bytes into bytes/bytearray, so indexing/iterating yields integers.

construct.lib.bytes2str(string)

Converts b’…’ string into ‘…’ string. On PY2 they are equivalent. On PY3 its utf8 decoded.

construct.lib.bytestringtype

alias of __builtin__.str

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

Generic ordered dictionary that allows both key and attribute access, and preserves key order by insertion. Adding keys is preferred using **entrieskw (requires Python 3.6). Equality does NOT check item order. Also provides regex searching.

Example:

# empty dict
>>> Container()
# list of pairs, not recommended
>>> Container([ ("name","anonymous"), ("age",21) ])
# This syntax requires Python 3.6
>>> Container(name="anonymous", age=21)
# This syntax is for internal use only
>>> Container(name="anonymous")(age=21)
# copies another dict
>>> Container(dict2)
>>> Container(container2)
>>> print(repr(obj))
Container(text='utf8 decoded string...')(value=123)
>>> print(obj)
Container
    text = u'utf8 decoded string...' (total 22)
    value = 123
clear()

Removes all items.

copy() → a shallow copy of D
items() → list of D's (key, value) pairs, as 2-tuples
keys() → list of D's keys
pop(key)

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.

search(pattern)

Searches a container (non-recursively) using regex.

search_all(pattern)

Searches a container (recursively) using regex.

update(seqordict)

Appends items from another dict/Container or list-of-tuples.

values() → list of D's values
class construct.lib.HexDisplayedBytes

Used internally.

class construct.lib.HexDisplayedDict

Used internally.

class construct.lib.HexDisplayedInteger

Used internally.

construct.lib.hexdump(data, linesize)

Turns bytes into a unicode string of the format:

>>>print(hexdump(b'0' * 100, 16))
hexundump(\"\"\"
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
\"\"\")
class construct.lib.HexDumpDisplayedBytes

Used internally.

class construct.lib.HexDumpDisplayedDict

Used internally.

construct.lib.hexlify(data)

Returns binascii.hexlify(data).

construct.lib.hexundump(data, linesize)

Reverse of hexdump.

construct.lib.int2byte(character)

Converts (0 through 255) integer into b’…’ character.

construct.lib.integer2bits(number, width)

Converts an integer into its binary representation in a bit-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 \x00 or \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 bytes-string into an integer. This is reverse to bytes2integer.

Examples:

>>> integer2bytes(19,4)
'\x00\x00\x00\x13'
construct.lib.integers2bytes(ints)

Converts integer generator into bytes.

construct.lib.iteratebytes(data)

Iterates though b’…’ string yielding b’…’ characters.

construct.lib.iterateints(data)

Iterates though b’…’ string yielding (0 through 255) integers.

class construct.lib.ListContainer

Generic container like list. Provides pretty-printing. Also provides regex searching.

Example:

>>> ListContainer()
>>> ListContainer([1, 2, 3])
>>> print(repr(obj))
[1, 2, 3]
>>> print(obj)
ListContainer
    1
    2
    3
search(pattern)

Searches a container (non-recursively) using regex.

search_all(pattern)

Searches a container (recursively) using regex.

construct.lib.reprstring(data)

Ensures there is b- u- prefix before the string.

construct.lib.setGlobalPrintFalseFlags(enabled=False)

When enabled, Container __str__ that was produced by FlagsEnum parsing prints all values, otherwise and by default, it prints only the values that are True.

Parameters:enabled – bool
construct.lib.setGlobalPrintFullStrings(enabled=False)

When enabled, Container __str__ produces full content of bytes and unicode strings, otherwise and by default, it produces truncated output (16 bytes and 32 characters).

Parameters:enabled – bool
construct.lib.setGlobalPrintPrivateEntries(enabled=False)

When enabled, Container __str__ shows keys like _ _index _etc, otherwise and by default, it hides those keys. __repr__ never shows private entries.

Parameters:enabled – bool
construct.lib.str2bytes(string)

Converts ‘…’ string into b’…’ string. On PY2 they are equivalent. On PY3 its utf8 encoded.

construct.lib.str2unicode(string)

Converts ‘…’ string into u’…’ string. On PY2 its utf8 encoded. On PY3 they are equivalent.

construct.lib.swapbitsinbytes(data)

Performs a bit-reversal within a byte-string.

Example:

>>> swapbits(b'\xf0')
b'\x0f'
construct.lib.swapbytes(data)

Performs an endianness swap on byte-string.

Example:

>>> swapbytes(b'abcd')
b'dcba'
construct.lib.swapbytesinbits(data)

Performs an byte-swap within a bit-string. Its length must be multiple of 8.

Example:

>>> swapbytesinbits(b'0000000011111111')
b'1111111100000000'
construct.lib.trimstring(data)

Trims b- u- prefix

construct.lib.unhexlify(data)

Returns binascii.unhexlify(data).

construct.lib.unicode2str(string)

Converts u’…’ string into ‘…’ string. On PY2 its utf8 decoded. On PY3 they are equivalent.

construct.lib.unicodestringtype

alias of __builtin__.unicode