term.py_codec_impl module¶
Module implements encoder and decoder from ETF (Erlang External Term Format) used by the network distribution layer.
-
term.py_codec_impl.
binary_to_term
(data: bytes, options: dict = None) -> (<built-in function any>, <class 'bytes'>)¶ Strip 131 header and unpack if the data was compressed.
Parameters: - data -- The incoming encoded data with the 131 byte
- options --
- "atom": "str" | "bytes" | "Atom" (default "Atom"). Returns atoms as strings, as bytes or as atom.Atom objects.
- "byte_string": "str" | "bytes" (default "str"). Returns 8-bit strings as Python str or bytes.
Raises: PyCodecError -- when the tag is not 131, when compressed data is incomplete or corrupted
Returns: Remaining unconsumed bytes
-
term.py_codec_impl.
binary_to_term_2
(data: bytes, options: dict = None) -> (<built-in function any>, <class 'bytes'>)¶ Proceed decoding after leading tag has been checked and removed.
Erlang lists are decoded into
term.List
object, whoseelements_
field contains the data,tail_
field has the optional tail and a helper function exists to assist with extracting an unicode string.Atoms are decoded to
Atom
or optionally to bytes or to strings. Pids are decoded intoPid
. Refs decoded to andReference
. Maps are decoded into Pythondict
. Binaries are decoded intobytes
object and bitstrings into a pair of(bytes, last_byte_bits:int)
.Parameters: - options --
- dict(str, _);
- "atom": "str" | "bytes" | "Atom"; default "Atom". Returns atoms as strings, as bytes or as atom.Atom class objects
- "byte_string": "str" | "bytes" (default "str"). Returns 8-bit strings as Python str or bytes.
- data -- Bytes containing encoded term without 131 header
Returns: Tuple[Value, Tail: bytes] The function consumes as much data as possible and returns the tail. Tail can be used again to parse another term if there was any.
Raises: PyCodecError(str) -- on various errors
- options --
-
term.py_codec_impl.
term_to_binary
(val, opt: typing.Union[NoneType, dict] = None) → bytes¶ Prepend the 131 header byte to encoded data. :param opt: None or dict of options: "encode_hook" is a callable which
will return representation for unknown object types. Returning None will be encoded as such and becomes Atom('undefined').
-
term.py_codec_impl.
term_to_binary_2
(val, encode_hook: typing.Union[typing.Callable, NoneType]) → bytes¶ Erlang lists are decoded into term.List object, whose
elements_
field contains the data,tail_
field has the optional tail and a helper function exists to assist with extracting an unicode string.Parameters: - encode_hook -- None or a callable which will represent an unknown object as an Erlang term before encoding. Returning None will be encoded as such and becomes Atom('undefined').
- val -- Almost any Python value
Returns: bytes object with encoded data, but without a 131 header byte.
-
exception
term.py_codec_impl.
PyCodecError
¶ Bases:
Exception
-
term.py_codec_impl.
generic_serialize_object
(obj, cd: set = None)¶ Given an arbitraty Python object creates a tuple (ClassName, {Fields}). A fair effort is made to avoid infinite recursion on cyclic objects. :param obj: Arbitrary object to encode :param cd: A set with ids of object, for cycle detection :return: A pair of result: (ClassName :: bytes(), Fields :: {bytes(), _})
or None, and a CycleDetect value