GufeTokenizable
#
- class gufe.tokenization.GufeTokenizable(*args, **kwargs)#
Base class for all tokenizeable gufe objects.
Subclassing from this provides sorting, equality and hashing operators, provided that the class implements the _to_dict and _from_dict method.
This extra work in serializing is important for hashes that are stable across different Python sessions.
Methods
Make a modified copy of this object.
Dict of default key-value pairs for this GufeTokenizable object.
Generate an instance from full dict representation.
Generate an instance from keyed dict representation.
Generate an instance from shallow dict representation.
Migrate old serialization dicts to the current form.
Generate full dict representation, with all referenced GufeTokenizable objects also given in full dict representations.
Generate keyed dict representation, with all referenced GufeTokenizable objects given in keyed representations.
Generate shallow dict representation, with all referenced GufeTokenizable objects left intact.
Attributes
Return logger adapter for this instance
- copy_with_replacements(**replacements)#
Make a modified copy of this object.
Since GufeTokenizables are immutable, this is essentially a shortcut to mutate the object. Note that the keyword arguments it takes are based on keys of the dictionaries used in the the
_to_dict
/_from_dict
cycle for this object; in most cases that is the same as parameters to__init__
, but not always.This will always return a new object in memory. So using
obj.copy_with_replacements()
(with no keyword arguments) is a way to create a shallow copy: the object is different in memory, but its attributes will be the same objects in memory as the original.- Parameters:
replacements (Dict) – keyword arguments with keys taken from the keys given by the output of this object’s
to_dict
method.
- classmethod defaults()#
Dict of default key-value pairs for this GufeTokenizable object.
These defaults are stripped from the dict form of this object produced with
to_dict(include_defaults=False)
where default values are present.
- classmethod from_dict(dct: dict)#
Generate an instance from full dict representation.
- Parameters:
dct (Dict) – A dictionary produced by to_dict to instantiate from. If an identical instance already exists in memory, it will be returned. Otherwise, a new instance will be returned.
- classmethod from_keyed_dict(dct: dict)#
Generate an instance from keyed dict representation.
- Parameters:
dct (Dict) – A dictionary produced by to_keyed_dict to instantiate from. If an identical instance already exists in memory, it will be returned. Otherwise, a new instance will be returned.
- classmethod from_shallow_dict(dct: dict)#
Generate an instance from shallow dict representation.
- Parameters:
dct (Dict) – A dictionary produced by to_shallow_dict to instantiate from. If an identical instance already exists in memory, it will be returned. Otherwise, a new instance will be returned.
- property key#
- property logger#
Return logger adapter for this instance
- classmethod serialization_migration(old_dict: dict, version: int) dict #
Migrate old serialization dicts to the current form.
The input dict
old_dict
comes from some previous serialization version, given byversion
. The output dict should be in the format of the current serialization dict.The recommended pattern to use looks like this:
def serialization_migration(cls, old_dict, version): if version == 1: ... # do things for migrating version 1->2 if version <= 2: ... # do things for migrating version 2->3 if version <= 3: ... # do things for migrating version 3->4 # etc
This approach steps through each old serialization model on its way to the current version. It keeps code relatively minimal and readable.
As a convenience, the following functions are available to simplify the various kinds of changes that are likely to occur in as serializtion versions change:
- Parameters:
old_dict (dict) – dict as received from a serialized form
version (int) – the serialization version of
old_dict
- Returns:
serialization dict suitable for the current implementation of
from_dict
.- Return type:
dict
- to_dict(include_defaults=True) dict #
Generate full dict representation, with all referenced GufeTokenizable objects also given in full dict representations.
- Parameters:
include_defaults (bool) – If False, strip keys from dict representation with values equal to those in defaults.
- to_keyed_dict(include_defaults=True) dict #
Generate keyed dict representation, with all referenced GufeTokenizable objects given in keyed representations.
A keyed representation of an object is a dict of the form:
{‘:gufe-key:’: <GufeTokenizable.key>}
These function as stubs to allow for serialization and storage of GufeTokenizable objects with minimal duplication.
The original object can be re-assembled with from_keyed_dict.
- to_shallow_dict() dict #
Generate shallow dict representation, with all referenced GufeTokenizable objects left intact.