serializer module

Module Summary

Serializer A convenience class for various serializers.
get_serializer Obtain a serializer object
get_serializer_from_filename Obtain a serializer object from a filename

Module

Library for serializing python objects to and from various different serializer formats

class lib.serializer.Serializer

Bases: object

A convenience class for various serializers.

This class should not be called directly as it acts as the parent for various serializers. All serializers should be called from get_serializer() or get_serializer_from_filename()

Example

>>> from lib.serializer import get_serializer
>>> serializer = get_serializer('json')
>>> json_file = '/path/to/json/file.json'
>>> data = serializer.load(json_file)
>>> serializer.save(json_file, data)
file_extension

The file extension of the serializer

Type:str
load(filename)

Load data from an existing serialized file

Parameters:filename (str) – The path to the serialized file
Returns:data – The data in a python object format
Return type:varies

Example

>>> serializer = get_serializer('json')
>>> json_file = '/path/to/json/file.json'
>>> data = serializer.load(json_file)
marshal(data)

Serialize an object

Parameters:data (varies) – The data that is to be serialized
Returns:data – The data in a the serialized data format
Return type:varies

Example

>>> serializer = get_serializer('json')
>>> data ['foo', 'bar']
>>> json_data = serializer.marshal(data)
save(filename, data)

Serialize data and save to a file

Parameters:
  • filename (str) – The path to where the serialized file should be saved
  • data (varies) – The data that is to be serialized to file

Example

>>> serializer = get_serializer('json')
>>> data ['foo', 'bar']
>>> json_file = '/path/to/json/file.json'
>>> serializer.save(json_file, data)
unmarshal(serialized_data)

Unserialize data to its original object type

Parameters:serialized_data (varies) – Data in serializer format that is to be unmarshalled to its original object
Returns:data – The data in a python object format
Return type:varies

Example

>>> serializer = get_serializer('json')
>>> json_data = <json object>
>>> data = serializer.unmarshal(json_data)
lib.serializer.get_serializer(serializer)

Obtain a serializer object

Parameters:serializer ({'json', 'pickle', yaml', 'npy', 'compressed'}) – The required serializer format
Returns:serializer – A serializer object for handling the requested data format
Return type:Serializer

Example

>>> serializer = get_serializer('json')
lib.serializer.get_serializer_from_filename(filename)

Obtain a serializer object from a filename

Parameters:filename (str) – Filename to determine the serializer type from
Returns:serializer – A serializer object for handling the requested data format
Return type:Serializer

Example

>>> filename = '/path/to/json/file.json'
>>> serializer = get_serializer_from_filename(filename)