construct.lib
– entire module¶
-
construct.lib.
bits2bytes
(data)¶ Converts between bit-string and byte-string representations, both as bytes type. Its length must be multiple of 8.
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 signed 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.
bytes2bits
(data)¶ Converts between bit-string and byte-string representations, both as bytes type.
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 integer list, 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
builtins.bytes
-
class
construct.lib.
Container
¶ 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.
Note that not all parameters can be accessed via attribute access (dot operator). If the name of an item matches a method name of the Container, it can only be accessed via key acces (square brackets). This includes the following names: clear, copy, fromkeys, get, items, keys, move_to_end, pop, popitem, search, search_all, setdefault, update, values.
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) # 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
-
update
(seqordict)¶ Appends items from another dict/Container or list-of-tuples.
-
copy
() → a shallow copy of od¶
-
search
(pattern)¶ Searches a container (non-recursively) using regex.
-
search_all
(pattern)¶ Searches a container (recursively) using regex.
-
-
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, signed=False)¶ Converts an integer into its binary representation in a bit-string. Width is the amount of bits to generate. Each bit is represented as either \x00 or \x01. The most significant bit 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, signed=False)¶ Converts an integer into a byte-string. This is reverse to bytes2integer.
Examples:
>>> integer2bytes(19, 4) '\x00\x00\x00\x13'
-
construct.lib.
integers2bytes
(ints)¶ Converts integer generator into bytes.
-
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.
swapbitsinbytes
(data)¶ Performs a bit-reversal on each byte within a byte-string.
Example:
>>> swapbitsinbytes(b"\xf0\x00") b"\x0f\x00"
-
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.
unicodestringtype
¶ alias of
builtins.str