pygsti.baseobjs.nicelyserializable

Defines the NicelySerializable class

Module Contents

Classes

NicelySerializable

The base class for all "nicely serializable" objects in pyGSTi.

Attributes

class_location_changes

pygsti.baseobjs.nicelyserializable.class_location_changes
class pygsti.baseobjs.nicelyserializable.NicelySerializable(doc_id=None)

Bases: pygsti.baseobjs.mongoserializable.MongoSerializable

The base class for all “nicely serializable” objects in pyGSTi.

A “nicely serializable” object can be converted to and created from a native Python object (like a string or dict) that contains only other native Python objects. In addition, there are constraints on the makeup of these objects so that they can be easily serialized to standard text-based formats, e.g. JSON. For example, dictionary keys must be strings, and the list vs. tuple distinction cannot be assumed to be preserved during serialization.

classmethod read(path, format=None)

Read an object of this type, or a subclass of this type, from a file.

Parameters

pathstr or Path or file-like

The filename to open or an already open input stream.

format{‘json’, None}

The format of the file. If None this is determined automatically by the filename extension of a given path.

Returns

NicelySerializable

classmethod load(f, format='json')

Load an object of this type, or a subclass of this type, from an input stream.

Parameters

ffile-like

An open input stream to read from.

format{‘json’}

The format of the input stream data.

Returns

NicelySerializable

classmethod loads(s, format='json')

Load an object of this type, or a subclass of this type, from a string.

Parameters

sstr

The serialized object.

format{‘json’}

The format of the string data.

Returns

NicelySerializable

classmethod from_nice_serialization(state)

Create and initialize an object from a “nice” serialization.

A “nice” serialization here means one created by a prior call to to_nice_serialization using this class or a subclass of it. Nice serializations adhere to additional rules (e.g. that dictionary keys must be strings) that make them amenable to common file formats (e.g. JSON).

The state argument is typically a dictionary containing ‘module’ and ‘state’ keys specifying the type of object that should be created. This type must be this class or a subclass of it.

Parameters

stateobject

An object, usually a dictionary, representing the object to de-serialize.

Returns

object

to_nice_serialization()

Serialize this object in a way that adheres to “niceness” rules of common text file formats.

Returns

object

Usually a dictionary representing the serialized object, but may also be another native Python type, e.g. a string or list.

write(path, **format_kwargs)

Writes this object to a file.

Parameters

pathstr or Path

The name of the file that is written.

format_kwargsdict, optional

Additional arguments specific to the format being used. For example, the JSON format accepts indent as an argument because json.dump does.

Returns

None

dump(f, format='json', **format_kwargs)

Serializes and writes this object to a given output stream.

Parameters

ffile-like

A writable output stream.

format{‘json’, ‘repr’}

The format to write.

format_kwargsdict, optional

Additional arguments specific to the format being used. For example, the JSON format accepts indent as an argument because json.dump does.

Returns

None

dumps(format='json', **format_kwargs)

Serializes this object and returns it as a string.

Parameters

format{‘json’, ‘repr’}

The format to write.

format_kwargsdict, optional

Additional arguments specific to the format being used. For example, the JSON format accepts indent as an argument because json.dump does.

Returns

str