# `CodecFactory`

digraph inheritance99df818614 { bgcolor=transparent; rankdir=UD; ratio=compress; size="8.0, 12.0"; "CodecFactory" [URL="#taurus.core.util.codecs.CodecFactory",color=dodgerblue1,fillcolor=white,fontcolor=black,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.5,shape=box,style=rounded,target="_top",tooltip="The singleton CodecFactory class."]; "Singleton" -> "CodecFactory" [arrowsize=0.5,style="setlinewidth(0.5)"]; "Logger" -> "CodecFactory" [arrowsize=0.5,style="setlinewidth(0.5)"]; "Logger" [URL="taurus-Logger.html#taurus.Logger",color=dodgerblue1,fillcolor=white,fontcolor=black,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.5,shape=box,style=rounded,target="_top",tooltip="The taurus logger class. All taurus pertinent classes should inherit"]; "Singleton" [URL="taurus.core.util.singleton-Singleton.html#taurus.core.util.singleton.Singleton",color=dodgerblue1,fillcolor=white,fontcolor=black,fontname="Vera Sans, DejaVu Sans, Liberation Sans, Arial, Helvetica, sans",fontsize=10,height=0.5,shape=box,style=rounded,target="_top",tooltip="This class allows Singleton objects"]; }

class CodecFactory(*p, **k)

  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)
  encode(format, data, *args, **kwargs)
  getCodec(format)

    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)

    Singleton instance initialization.

  registerCodec(format, klass)

    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)

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

    Parameters:

      format (str) – the codec id

    Raises:

      KeyError