Skip to main content
`Ctrl`+`K`

# MsgPack

class numcodecs.msgpacks.MsgPack(use_single_float=False, use_bin_type=True, raw=False)

  Codec to encode data as msgpacked bytes. Useful for encoding an array of Python objects.

  Changed in version 0.6: The encoding format has been changed to include the array shape in the encoded data, which ensures that all object arrays can be correctly encoded and decoded.

  Parameters:
    use_single_float bool, optional

      Use single precision float type for float.

    use_bin_type bool, optional

      Use bin type introduced in msgpack spec 2.0 for bytes. It also enables str8 type for unicode.

    raw bool, optional

      If true, unpack msgpack raw to Python bytes. Otherwise, unpack to Python str by decoding with UTF-8 encoding.

  See also

  `numcodecs.pickles.Pickle`, `numcodecs.json.JSON`, `numcodecs.vlen.VLenUTF8`

  Notes

  Requires msgpack to be installed.

  Examples

      >>> import numcodecs
      >>> import numpy as np
      >>> x = np.array(['foo', 'bar', 'baz'], dtype='object')
      >>> codec = numcodecs.MsgPack()
      >>> codec.decode(codec.encode(x))
      array(['foo', 'bar', 'baz'], dtype=object)

  codec_id: str | None = 'msgpack2'

    Codec identifier.

  encode(buf)

    Encode data in buf.

    Parameters:
      buf buffer-like

        Data to be encoded. May be any object supporting the new-style buffer protocol.

    Returns:
      enc buffer-like

        Encoded data. May be any object supporting the new-style buffer protocol.

  decode(buf, out=None)

    Decode data in buf.

    Parameters:
      buf buffer-like

        Encoded data. May be any object supporting the new-style buffer protocol.

      out buffer-like, optional

        Writeable buffer to store decoded data. N.B. if provided, this buffer must be exactly the right size to store the decoded data.

    Returns:
      dec buffer-like

        Decoded data. May be any object supporting the new-style buffer protocol.

  get_config()

    Return a dictionary holding configuration parameters for this codec. Must include an ‘id’ field with the codec identifier. All values must be compatible with JSON encoding.

  classmethod from_config(config)

    Instantiate codec from a configuration object.