Pickling and unpickling objects – pickling

Default methods used by persist for pickling and unpickling objects.

pypersist.pickling.pickle(obj)[source]

Return a string representation of the object obj

This function takes any object, and uses the pickle and base64 modules to create a string which represents it. This string consists only of alphanumeric characters, hyphens and underscores. The object obj can later be reconstructed from this string using the unpickle function.

Examples

>>> pickle("Hello world")
'gANYCwAAAEhlbGxvIHdvcmxkcQAu'
>>> unpickle("gANYCwAAAEhlbGxvIHdvcmxkcQAu")
'Hello world'
pypersist.pickling.pickle_to_bytes(obj)[source]

Pickle an object to a bytes object

For most objects, this function is equivalent to pickle.dumps. However, if pickle.dumps fails, then an alternative pickling method will be attempted using Sage, if Sage is loaded. Otherwise, an error will be raised.

Used inside the pickle function in this file.

pypersist.pickling.unpickle(string)[source]

Restore an object from a string created by the pickle function

If string was created by the pickle function in this file, then this function returns an object identical to the one that was used to create string.

Examples

>>> pickle("Hello world")
'gANYCwAAAEhlbGxvIHdvcmxkcQAu'
>>> unpickle("gANYCwAAAEhlbGxvIHdvcmxkcQAu")
'Hello world'
pypersist.pickling.unpickle_from_bytes(obj)[source]

Unpickle a bytes object to produce the original object that was pickled

For most objects, this function is equivalent to pickle.loads. However, if pickle.loads fails, then an alternative unpickling method will be attempted using Sage, if Sage is loaded. Otherwise, an error will be raised.

Used inside the unpickle function in this file.