API Docs¶
Encoder¶
-
class
quickle.
Encoder
(*, memoize=True, collect_buffers=False, registry=None, write_buffer_size=4096)¶ A quickle encoder.
Creating an
Encoder
and calling theEncoder.dumps
method multiple times is more efficient than callingquickle.dumps
multiple times.- Parameters
memoize (bool, optional) – Whether to enable memoization. If True (default), any duplicate objects will only be serialized once. Disabling memoization can be more efficient for objects unlikely to contain duplicate values, but self-referential objects will then fail to serialize.
collect_buffers (bool, optional) – Whether to collect out-of-band buffers. If True, the return value of
Encoder.dumps
will be the serialized bytes, and a list of anyPickleBuffer
objects found (orNone
if none are found).registry (list or dict, optional) – A registry of user-defined types this decoder instance should support. Can be either a list of types (recommended), or a dict mapping the type to a unique positive integer. Note that for deserialization to be successful, the registry should match that of the corresponding
Decoder
.write_buffer_size (int, optional) – The size of the internal static write buffer.
-
dumps
(*, memoize=None, collect_buffers=None)¶ Serialize an object to bytes.
- Parameters
memoize (bool, optional) – Whether to enable memoization. Defaults to the value set on the encoder. If True, any duplicate objects will only be serialized once. Disabling memoization can be more efficient for objects unlikely to contain duplicate values, but self-referential objects will then fail to serialize.
collect_buffers (bool, optional) – Whether to collect out-of-band buffers. Defaults to the value set on the encoder. If True, the return value will be the serialized object, and a list of any
PickleBuffer
objects found (orNone
if none are found).
- Returns
data (bytes) – The serialized object
buffers (list of
PickleBuffer
orNone
, optional) – Ifcollect_buffers
isTrue
, a list of out-of-band buffers will also be returned (or None if no buffers are found). Not returned ifcollect_buffers
isFalse
Decoder¶
-
class
quickle.
Decoder
(registry=None)¶ A quickle decoder.
Creating a
Decoder
and calling theDecoder.loads
method multiple times is more efficient than callingquickle.loads
multiple times.- Parameters
registry (list or dict, optional) – A registry of user-defined types this encoder instance should support. Can be either a list of types (recommended), or a dict mapping positive integers to each type. Note that for deserialization to be successful, the registry should match that of the corresponding
Encoder
.
-
loads
(data, *, buffers=None)¶ Deserialize an object from bytes.
- Parameters
data (bytes) – The serialized data
buffers (iterable of bytes, optional) – An iterable of out-of-band buffers generated by passing
collect_buffers=True
to the correspondingEncoder.dumps
call.
- Returns
obj – The deserialized object
- Return type
Struct¶
-
class
quickle.
Struct
¶ A base class for defining efficient serializable objects.
Fields are defined using type annotations. Fields may optionally have default values, which result in keyword parameters to the constructor. Note that mutable default values are deepcopied in the constructor to prevent accidental sharing.
Structs automatically define
__init__
,__eq__
,__repr__
, and__copy__
methods. Additional methods can be defined on the class as needed. Note that__init__
/__new__
cannot be overridden, but other methods can. A tuple of the field names is available on the class via the__struct_fields__
attribute if needed.Examples
Here we define a new
Struct
type for describing a dog. It has three fields; two required and one optional.>>> class Dog(Struct): ... name: str ... breed: str ... is_good_boy: bool = True ... >>> Dog('snickers', breed='corgi') Dog(name='snickers', breed='corgi', is_good_boy=True)
To serialize or deserialize
Struct
types, they need to be registered with anEncoder
andDecoder
through theregistry
argument.>>> enc = Encoder(registry=[Dog]) >>> dec = Decoder(registry=[Dog]) >>> data = enc.dumps(Dog('snickers', 'corgi')) >>> dec.loads(data) Dog(name='snickers', breed='corgi', is_good_boy=True)
PickleBuffer¶
Functions¶
-
quickle.
dumps
(obj, *, memoize=True, collect_buffers=False, registry=None)¶ Serialize an object to bytes.
- Parameters
memoize (bool, optional) – Whether to enable memoization. If True (default), any duplicate objects will only be serialized once. Disabling memoization can be more efficient for objects unlikely to contain duplicate values, but self-referential objects will then fail to serialize.
collect_buffers (bool, optional) – Whether to collect out-of-band buffers. If True, the return value will be the serialized bytes, and a list of any
PickleBuffer
objects found (orNone
if none are found).registry (list or dict, optional) – A registry of user-defined types to support. Can be either a list of types (recommended), or a dict mapping the type to a unique positive integer. Note that for deserialization to be successful, the registry should match that of the corresponding
loads
.
- Returns
data (bytes) – The serialized object
buffers (list of
PickleBuffer
orNone
, optional) – Ifcollect_buffers
isTrue
, a list of out-of-band buffers will also be returned (or None if no buffers are found). Not returned ifcollect_buffers
isFalse
See also
-
quickle.
loads
(data, *, buffers=None, registry=None)¶ Deserialize an object from bytes.
- Parameters
buffers (iterable of bytes, optional) – An iterable of out-of-band buffers generated by passing
collect_buffers=True
to the correspondingdumps
call.registry (list or dict, optional) – A registry of user-defined types to support. Can be either a list of types (recommended), or a dict mapping positive integers to each type. Note that for deserialization to be successful, the registry should match that of the corresponding
dumps
.
- Returns
obj – The deserialized object.
- Return type
See also
Exceptions¶
-
exception
quickle.
EncodingError
¶ Bases:
quickle.QuickleError
A protocol error occurred while encoding an object
-
exception
quickle.
DecodingError
¶ Bases:
quickle.QuickleError
A protocol error occurred while decoding an object