GufeTokenizable#

class openfe.utils.GufeTokenizable(*args, **kwargs)#

Bases: ABC

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

copy_with_replacements

Make a modified copy of this object.

defaults

Dict of default key-value pairs for this GufeTokenizable object.

from_dict

Generate an instance from full dict representation.

from_keyed_dict

Generate an instance from keyed dict representation.

from_shallow_dict

Generate an instance from shallow dict representation.

to_dict

Generate full dict representation, with all referenced GufeTokenizable objects also given in full dict representations.

to_keyed_dict

Generate keyed dict representation, with all referenced GufeTokenizable objects given in keyed representations.

to_shallow_dict

Generate shallow dict representation, with all referenced GufeTokenizable objects left intact.

Attributes

key

logger

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

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.