# pymatgen.util.serialization module¶

class PmgPickler(file, protocol=None)[source]

Bases: pickle.Pickler

Persistence of External Objects as described in section 12.1.5.1 of https://docs.python.org/3/library/pickle.html

This takes a file-like object for writing a pickle data stream.

The optional protocol argument tells the pickler to use the given protocol; supported protocols are 0, 1, 2. The default protocol is 0, to be backwards compatible. (Protocol 0 is the only protocol that can be written to a file opened in text mode and read back successfully. When using a protocol higher than 0, make sure the file is opened in binary mode, both when pickling and unpickling.)

Protocol 1 is more efficient than protocol 0; protocol 2 is more efficient than protocol 1.

Specifying a negative protocol version selects the highest protocol version supported. The higher the protocol used, the more recent the version of Python needed to read the pickle produced.

The file parameter must have a write() method that accepts a single string argument. It can thus be an open file object, a StringIO object, or any other custom object that meets this interface.

persistent_id(obj)[source]

Instead of pickling as a regular class instance, we emit a persistent ID.

class PmgUnpickler(file)[source]

Bases: pickle.Unpickler

Persistence of External Objects as described in section 12.1.5.1 of https://docs.python.org/3/library/pickle.html

This takes a file-like object for reading a pickle data stream.

The protocol version of the pickle is detected automatically, so no proto argument is needed.

The file-like object must have two methods, a read() method that takes an integer argument, and a readline() method that requires no arguments. Both methods should return a string. Thus file-like object can be a file object opened for reading, a StringIO object, or any other custom object that meets this interface.

persistent_load(pid)[source]

This method is invoked whenever a persistent ID is encountered. Here, pid is the tuple returned by PmgPickler.

class SlotPickleMixin[source]

Bases: object

This mixin makes it possible to pickle/unpickle objects with __slots__ defined.

json_pretty_dump(obj, filename)[source]

Serialize obj as a JSON formatted stream to the given filename ( pretty printing version)

pmg_pickle_dump(obj, filobj, **kwargs)[source]

Dump an object to a pickle file using PmgPickler.

Parameters: obj (object) – Object to dump. fileobj – File-like object **kwargs – Any of the keyword arguments supported by PmgPickler
pmg_pickle_load(filobj, **kwargs)[source]

Loads a pickle file and deserialize it with PmgUnpickler.

Parameters: filobj – File-like object **kwargs – Any of the keyword arguments supported by PmgUnpickler Deserialized object.
pmg_serialize(method)[source]

Decorator for methods that add MSON serializations keys to the dictionary. See documentation of MSON for more details