Serialization

Workflow has several methods for storing persistent data to your workflow’s data and cache directories. By default these are stored as Python pickle objects using CPickleSerializer (with the file extension .cpickle).

You may, however, want to serialize your data in a different format, e.g. JSON, to make it user-readable/-editable or to interface with other software, and the SerializerManager and data storage/caching APIs enable you to do this.

For more information on how to change the default serializers, specify alternative ones and register new ones, see Persistent data and Serialization of stored/cached data in the User Manual.

API

class workflow.workflow.SerializerManager

Contains registered serializers.

New in version 1.8.

A configured instance of this class is available at workflow.manager.

Use register() to register new (or replace existing) serializers, which you can specify by name when calling Workflow data storage methods.

See Serialization of stored/cached data and Persistent data for further information.

register(name, serializer)

Register serializer object under name.

Raises AttributeError if serializer in invalid.

Note

name will be used as the file extension of the saved files.

Parameters:
  • name (unicode or str) – Name to register serializer under
  • serializer – object with load() and dump() methods
serializer(name)

Return serializer object for name.

Parameters:name (unicode or str) – Name of serializer to return
Returns:serializer object or None if no such serializer is registered.
serializers

Return names of registered serializers.

unregister(name)

Remove registered serializer with name.

Raises a ValueError if there is no such registered serializer.

Parameters:name (unicode or str) – Name of serializer to remove
Returns:serializer object
class workflow.workflow.JSONSerializer

Wrapper around json. Sets indent and encoding.

New in version 1.8.

Use this serializer if you need readable data files. JSON doesn’t support Python objects as well as cPickle/pickle, so be careful which data you try to serialize as JSON.

classmethod dump(obj, file_obj)

Serialize object obj to open JSON file.

New in version 1.8.

Parameters:
  • obj (JSON-serializable data structure) – Python object to serialize
  • file_obj (file object) – file handle
classmethod load(file_obj)

Load serialized object from open JSON file.

New in version 1.8.

Parameters:file_obj (file object) – file handle
Returns:object loaded from JSON file
Return type:object
class workflow.workflow.CPickleSerializer

Wrapper around cPickle. Sets protocol.

New in version 1.8.

This is the default serializer and the best combination of speed and flexibility.

classmethod dump(obj, file_obj)

Serialize object obj to open pickle file.

New in version 1.8.

Parameters:
  • obj (Python object) – Python object to serialize
  • file_obj (file object) – file handle
classmethod load(file_obj)

Load serialized object from open pickle file.

New in version 1.8.

Parameters:file_obj (file object) – file handle
Returns:object loaded from pickle file
Return type:object
class workflow.workflow.PickleSerializer

Wrapper around pickle. Sets protocol.

New in version 1.8.

Use this serializer if you need to add custom pickling.

classmethod dump(obj, file_obj)

Serialize object obj to open pickle file.

New in version 1.8.

Parameters:
  • obj (Python object) – Python object to serialize
  • file_obj (file object) – file handle
classmethod load(file_obj)

Load serialized object from open pickle file.

New in version 1.8.

Parameters:file_obj (file object) – file handle
Returns:object loaded from pickle file
Return type:object