pygsti.baseobjs.nicelyserializable
¶
Defines the NicelySerializable class
Module Contents¶
Classes¶
The base class for all "nicely serializable" objects in pyGSTi. |
- class pygsti.baseobjs.nicelyserializable.NicelySerializable¶
Bases:
object
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(cls, path, format=None)¶
Read an object of this type, or a subclass of this type, from a file.
- Parameters
path (str 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(cls, f, format='json')¶
Load an object of this type, or a subclass of this type, from an input stream.
- Parameters
f (file-like) – An open input stream to read from.
format ({'json'}) – The format of the input stream data.
- Returns
NicelySerializable
- classmethod loads(cls, s, format='json')¶
Load an object of this type, or a subclass of this type, from a string.
- Parameters
s (str) – The serialized object.
format ({'json'}) – The format of the string data.
- Returns
NicelySerializable
- classmethod from_nice_serialization(cls, 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
state (object) – An object, usually a dictionary, representing the object to de-serialize.
- Returns
object
- to_nice_serialization(self)¶
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(self, path, **format_kwargs)¶
Writes this object to a file.
- Parameters
path (str or Path) – The name of the file that is written.
format ({'json', 'repr'}) – The format to write.
format_kwargs (dict, 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(self, f, format='json', **format_kwargs)¶
Serializes and writes this object to a given output stream.
- Parameters
f (file-like) – A writable output stream.
format ({'json', 'repr'}) – The format to write.
format_kwargs (dict, 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(self, format='json', **format_kwargs)¶
Serializes this object and returns it as a string.
- Parameters
format ({'json', 'repr'}) – The format to write.
format_kwargs (dict, 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
- _dump_or_dumps(self, f, format='json', **format_kwargs)¶
Serializes and writes this object to a given output stream.
- Parameters
f (file-like) – A writable output stream. If None, then object is written as a string and returned.
format ({'json', 'repr'}) – The format to write.
format_kwargs (dict, 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 or None – If f is None, then the serialized object as a string is returned. Otherwise, None is returned.
- _to_nice_serialization(self)¶
- classmethod _from_nice_serialization(cls, state)¶
- classmethod _state_class(cls, state, check_is_subclass=True)¶
Returns the class specified by the given state dictionary
- classmethod _check_compatible_nice_state(cls, state)¶
- classmethod _encodemx(cls, mx)¶
- classmethod _decodemx(cls, mx)¶
- classmethod _encodevalue(cls, val)¶
- classmethod _decodevalue(cls, val)¶