pyiron_base.interfaces.has_groups module

Mixin for classes that represent a hierarchical structures of “groups” and “nodes”.

class pyiron_base.interfaces.has_groups.HasGroups

Bases: ABC

Abstract mixin to capture hierarchical structure of pyiron objects.

Necessary overrides are :method:`.__getitem__()`, :method:`._list_groups()` and :method:`._list_nodes()`. Sub classes may override :method:`._list_all()`, but the default implementation will call the other two methods.

A hierarchical object has a number of children and associates them with string names. :method:`.__getitem__()` looks up a child with the given name. Some of these children may have children of their own and also implement HasGroups. These are called “groups”; other children are “nodes”. :method:`._list_groups()` and :method:`._list_nodes()` must return the names of the respective type of children.

Sub classes should document in their class docstring what they consider has “groups” or “nodes”.

Here’s an example class that uses nested dicts to store children

>>> class NestedDicts(HasGroups):
...
...    def __init__(self, data):
...        self._data = data
...
...    def __getitem__(self, key):
...        v = self._data[key]
...        if isinstance(v, dict):
...            return NestedDicts(v)
...        else:
...            return v
...
...    def _list_groups(self):
...        return [k for k, v in self._data.items() if isinstance(v, dict)]
...
...    def _list_nodes(self):
...        return [k for k, v in self._data.items() if not isinstance(v, dict)]
>>> nd = NestedDicts({"foo": 0, "bar": 1, "baz": {"apple": "yummy", "pear": "yuck"}})
>>> nd.list_nodes()
['foo', 'bar']
>>> nd.list_groups()
['baz']
>>> isinstance(nd["baz"], HasGroups)
True
list_all()

Returns dictionary of :method:`.list_groups()` and :method:`.list_nodes()`.

Returns:

results of :method:`.list_groups() under the key "groups"; results of :method:`.list_nodes()` und the

key “nodes”

Return type:

dict

list_groups()

Return a list of names of all nested groups.

Returns:

group names

Return type:

list of str

list_nodes()

Return a list of names of all nested nodes.

Returns:

node names

Return type:

list of str