API Docs¶
Encoder¶
-
class
quickle.Encoder(*, memoize=True, collect_buffers=False, registry=None, write_buffer_size=4096)¶ A quickle encoder.
Creating an
Encoderand calling theEncoder.dumpsmethod multiple times is more efficient than callingquickle.dumpsmultiple 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.dumpswill be the serialized bytes, and a list of anyPickleBufferobjects found (orNoneif 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
PickleBufferobjects found (orNoneif none are found).
- Returns
data (bytes) – The serialized object
buffers (list of
PickleBufferorNone, optional) – Ifcollect_buffersisTrue, a list of out-of-band buffers will also be returned (or None if no buffers are found). Not returned ifcollect_buffersisFalse
Decoder¶
-
class
quickle.Decoder(registry=None)¶ A quickle decoder.
Creating a
Decoderand calling theDecoder.loadsmethod multiple times is more efficient than callingquickle.loadsmultiple 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=Trueto the correspondingEncoder.dumpscall.
- 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
Structtype 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
Structtypes, they need to be registered with anEncoderandDecoderthrough theregistryargument.>>> 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
PickleBufferobjects found (orNoneif 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
PickleBufferorNone, optional) – Ifcollect_buffersisTrue, a list of out-of-band buffers will also be returned (or None if no buffers are found). Not returned ifcollect_buffersisFalse
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=Trueto the correspondingdumpscall.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.QuickleErrorA protocol error occurred while encoding an object
-
exception
quickle.DecodingError¶ Bases:
quickle.QuickleErrorA protocol error occurred while decoding an object