CodecFactory

Inheritance diagram of CodecFactory
class CodecFactory(*p, **k)[source]

The singleton CodecFactory class.

To get the singleton object do:

from taurus.core.util.codecs import CodecFactory
f = CodecFactory()

The CodecFactory class allows you to get a codec object for a given format and also to register new codecs.

The CodecPipeline is a special codec that is able to code/decode a sequence of codecs. This way you can have codecs ‘inside’ codecs.

Example:

>>> from taurus.core.util.codecs import CodecFactory
>>> cf = CodecFactory()
>>> json_codec = cf.getCodec('json')
>>> bz2_json_codec = cf.getCodec('bz2_json')
>>> data = range(100000)
>>> f1, enc_d1 = json_codec.encode(('', data))
>>> f2, enc_d2 = bz2_json_codec.encode(('', data))
>>> print(len(enc_d1), len(enc_d2))
688890 123511
>>>
>>> f1, dec_d1 = json_codec.decode((f1, enc_d1))
>>> f2, dec_d2 = bz2_json_codec.decode((f2, enc_d2))

A Taurus related example:

>>> # automatically get the data from a DEV_ENCODED attribute
>>> import taurus
>>> from taurus.core.util.codecs import CodecFactory
>>> cf = CodecFactory()
>>> devenc_attr = taurus.Attribute('a/b/c/devenc_attr')
>>> v = devenc_attr.read()
>>> codec = CodecFactory().getCodec(v.format)
>>> f, d = codec.decode((v.format, v.value))

Import from taurus.core.util.codecs as:

from taurus.core.util.codecs import CodecFactory
CODEC_MAP = {'': <class 'taurus.core.util.codecs.NullCodec'>, 'bson': <class 'taurus.core.util.codecs.BSONCodec'>, 'bz2': <class 'taurus.core.util.codecs.BZ2Codec'>, 'json': <class 'taurus.core.util.codecs.JSONCodec'>, 'none': <class 'taurus.core.util.codecs.NullCodec'>, 'null': <class 'taurus.core.util.codecs.NullCodec'>, 'pickle': <class 'taurus.core.util.codecs.PickleCodec'>, 'plot': <class 'taurus.core.util.codecs.PlotCodec'>, 'utf8': <class 'taurus.core.util.codecs.Utf8Codec'>, 'video_image': <class 'taurus.core.util.codecs.VideoImageCodec'>, 'videoimage': <class 'taurus.core.util.codecs.VideoImageCodec'>, 'zip': <class 'taurus.core.util.codecs.ZIPCodec'>}

Default minimum map of registered codecs

decode(data, *args, **kwargs)[source]
encode(format, data, *args, **kwargs)[source]
getCodec(format)[source]

Returns the codec object for the given format or None if no suitable codec is found

Parameters:

format (str) – the codec id

Returns:

the codec object for the given format

Return type:

Codec or None

init(*args, **kwargs)[source]

Singleton instance initialization.

registerCodec(format, klass)[source]

Registers a new codec. If a codec already exists for the given format it is removed.

Parameters:
  • format (str) – the codec id

  • klass (Codec class) – the class that handles the format

unregisterCodec(format)[source]

Unregisters the given format. If the format does not exist an exception is thrown.

Parameters:

format (str) – the codec id

Raises:

KeyError