ring.coder — Auto encode/decode layer

Coder is a configurable layer that provides ways to encode raw data and decode stored cache data.

class ring.coder.Coder

Abstract coder interface.

See coderize() to create a Coder-compatible object in an easy way. See CoderTuple to create a Coder-compatible object with functions.


Abstract decode function. Children must implement this function.


Abstract encode function. Children must implement this function.


Coder-compatible tuple with encode and decode functions

alias of ring.coder.Coder

class ring.coder.DataclassCoder
static decode(binary)

Deserialize json encoded dictionary to dataclass object

static encode(data)

Serialize dataclass object to json encoded dictionary

class ring.coder.JsonCoder

JSON Coder.

When ujson package is installed, ujson is automatically selected; Otherwise, json will be used.

static decode(binary)

Decode UTF-8 bytes to JSON string and load it to object

static encode(data)

Dump data to JSON string and encode it to UTF-8 bytes

class ring.coder.Registry

Coder registry.

See:ring.coder.registry() for default registry instance.

Get the registered coder for corresponding coder_name.

This method is internally called when coder parameter is passed to ring object factory.

register(coder_name, raw_coder)

Register raw_coder as a new coder with alias coder_name.

Coder can be one of next types:

  • A Coder subclass.
  • A CoderTuple object.
  • A tuple of encode and decode functions.
  • An object which has encode and decode methods.
  • coder_name (str) – A new coder name to register.
  • raw_coder (object) – A new coder object.
ring.coder.bypass_coder = (<function bypass>, <function bypass>)

Default coder.

encode and decode functions bypass the given parameter.

ring.coder.pickle_coder = (<built-in function dumps>, <built-in function loads>)

Pickle coder.

encode is pickle.dumps() and decode is pickle.loads(). cpickle will be automatically loaded for CPython2.

ring.coder.registry = <ring.coder.Registry object>

The default coder registry with pre-registered coders. Built-in coders are registered by default.

See:ring.coder.Registry for the class definition.