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 callingWorkflow
data storage methods.See Serialization of stored/cached data and Persistent data for further information.
-
register
(name, serializer)¶ Register
serializer
object undername
.Raises
AttributeError
ifserializer
in invalid.Note
name
will be used as the file extension of the saved files.Parameters: - name (
unicode
orstr
) – Name to registerserializer
under - serializer – object with
load()
anddump()
methods
- name (
-
serializer
(name)¶ Return serializer object for
name
orNone
if no such serializer is registeredParameters: name ( unicode
orstr
) – Name of serializer to returnReturns: serializer object or None
-
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
orstr
) – Name of serializer to removeReturns: serializer object
-
-
class
workflow.workflow.
JSONSerializer
¶ Wrapper around
json
. Setsindent
andencoding
.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
-
class
workflow.workflow.
CPickleSerializer
¶ Wrapper around
cPickle
. Setsprotocol
.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
-
class
workflow.workflow.
PickleSerializer
¶ Wrapper around
pickle
. Setsprotocol
.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