API Reference¶
Functions¶
-
rlp.encode(obj, sedes=None, infer_serializer=True, cache=True)[source]¶ Encode a Python object in RLP format.
By default, the object is serialized in a suitable way first (using
rlp.infer_sedes()) and then encoded. Serialization can be explicitly suppressed by setting infer_serializer toFalseand not passing an alternative as sedes.If obj has an attribute
_cached_rlp(as, notably,rlp.Serializable) and its value is not None, this value is returned bypassing serialization and encoding, unless sedes is given (as the cache is assumed to refer to the standard serialization which can be replaced by specifying sedes).If obj is a
rlp.Serializableand cache is true, the result of the encoding will be stored in_cached_rlpif it is empty.- Parameters
sedes – an object implementing a function
serialize(obj)which will be used to serializeobjbefore encoding, orNoneto use the infered one (if any)infer_serializer – if
Truean appropriate serializer will be selected usingrlp.infer_sedes()to serialize obj before encodingcache – cache the return value in obj._cached_rlp if possible (default True)
- Returns
the RLP encoded item
- Raises
rlp.EncodingErrorin the rather unlikely case that the item is too big to encode (will not happen)- Raises
rlp.SerializationErrorif the serialization fails
-
rlp.decode(rlp, sedes=None, strict=True, recursive_cache=False, **kwargs)[source]¶ Decode an RLP encoded object.
If the deserialized result obj has an attribute
_cached_rlp(e.g. if sedes is a subclass ofrlp.Serializable) it will be set to rlp, which will improve performance on subsequentrlp.encode()calls. Bear in mind however that obj needs to make sure that this value is updated whenever one of its fields changes or prevent such changes entirely (rlp.sedes.Serializabledoes the latter).- Parameters
sedes – an object implementing a function
deserialize(code)which will be applied after decoding, orNoneif no deserialization should be performed**kwargs – additional keyword arguments that will be passed to the deserializer
strict – if false inputs that are longer than necessary don’t cause an exception
- Returns
the decoded and maybe deserialized Python object
- Raises
rlp.DecodingErrorif the input string does not end after the root item and strict is true- Raises
rlp.DeserializationErrorif the deserialization fails
-
rlp.decode_lazy(rlp, sedes=None, **sedes_kwargs)[source]¶ Decode an RLP encoded object in a lazy fashion.
If the encoded object is a bytestring, this function acts similar to
rlp.decode(). If it is a list however, aLazyListis returned instead. This object will decode the string lazily, avoiding both horizontal and vertical traversing as much as possible.The way sedes is applied depends on the decoded object: If it is a string sedes deserializes it as a whole; if it is a list, each element is deserialized individually. In both cases, sedes_kwargs are passed on. Note that, if a deserializer is used, only “horizontal” but not “vertical lazyness” can be preserved.
- Parameters
rlp – the RLP string to decode
sedes – an object implementing a method
deserialize(code)which is used as described above, orNoneif no deserialization should be performed**sedes_kwargs – additional keyword arguments that will be passed to the deserializers
- Returns
either the already decoded and deserialized object (if encoded as a string) or an instance of
rlp.LazyList
-
class
rlp.LazyList(rlp, start, end, sedes=None, **sedes_kwargs)[source]¶ A RLP encoded list which decodes itself when necessary.
Both indexing with positive indices and iterating are supported. Getting the length with
len()is possible as well but requires full horizontal encoding.- Parameters
rlp – the rlp string in which the list is encoded
start – the position of the first payload byte of the encoded list
end – the position of the last payload byte of the encoded list
sedes – a sedes object which deserializes each element of the list, or
Nonefor no deserialization**sedes_kwargs – keyword arguments which will be passed on to the deserializer
-
rlp.infer_sedes(obj)[source]¶ Try to find a sedes objects suitable for a given Python object.
The sedes objects considered are obj’s class, big_endian_int and binary. If obj is a sequence, a
rlp.sedes.Listwill be constructed recursively.- Parameters
obj – the python object for which to find a sedes object
- Raises
TypeErrorif no appropriate sedes could be found
Sedes Objects¶
-
rlp.sedes.raw¶ A sedes object that does nothing. Thus, it can serialize everything that can be directly encoded in RLP (nested lists of strings). This sedes can be used as a placeholder when deserializing larger structures.
-
class
rlp.sedes.Binary(min_length=None, max_length=None, allow_empty=False)[source]¶ A sedes object for binary data of certain length.
- Parameters
min_length – the minimal length in bytes or None for no lower limit
max_length – the maximal length in bytes or None for no upper limit
allow_empty – if true, empty strings are considered valid even if a minimum length is required otherwise
-
rlp.sedes.binary¶ A sedes object for binary data of arbitrary length (an instance of
rlp.sedes.Binarywith default arguments).
-
rlp.sedes.boolean¶ A sedes object for boolean types.
-
class
rlp.sedes.Text(min_length=None, max_length=None, allow_empty=False, encoding='utf8')[source]¶ A sedes object for encoded text data of certain length.
- Parameters
min_length – the minimal length in encoded characters or None for no lower limit
max_length – the maximal length in encoded characters or None for no upper limit
allow_empty – if true, empty strings are considered valid even if a minimum length is required otherwise
-
rlp.sedes.text¶ A sedes object for utf encoded text data of arbitrary length (an instance of
rlp.sedes.Textwith default arguments).
-
class
rlp.sedes.BigEndianInt(l=None)[source]¶ A sedes for big endian integers.
- Parameters
l – the size of the serialized representation in bytes or None to use the shortest possible one
-
rlp.sedes.big_endian_int¶ A sedes object for integers encoded in big endian without any leading zeros (an instance of
rlp.sedes.BigEndianIntwith default arguments).
-
class
rlp.sedes.List(elements=None, strict=True)[source]¶ A sedes for lists, implemented as a list of other sedes objects.
- Parameters
strict – If true (de)serializing lists that have a length not matching the sedes length will result in an error. If false (de)serialization will stop as soon as either one of the lists runs out of elements.
Exceptions¶
-
exception
rlp.EncodingError(message, obj)[source]¶ Exception raised if encoding fails.
- Variables
obj – the object that could not be encoded
-
exception
rlp.DecodingError(message, rlp)[source]¶ Exception raised if decoding fails.
- Variables
rlp – the RLP string that could not be decoded