Topology Utilities

class vivarium.library.topology.TestUpdateIn[source]

Bases: object

d = {'bar': {'c': 'd'}, 'foo': {1: {'a': 'b'}}}
vivarium.library.topology.assoc_path(d, path, value)[source]

Insert value into the dictionary d at path.

>>> d = {'a': {'b': 'c'}}
>>> assoc_path(d, ('a', 'd'), 'e')
{'a': {'b': 'c', 'd': 'e'}}
>>> d
{'a': {'b': 'c', 'd': 'e'}}

Create new dictionaries recursively as needed.

vivarium.library.topology.convert_path_style(path)[source]
vivarium.library.topology.delete_in(d, path)[source]

Delete an item from a dictionary by its path.

>>> d = {'a': {'b': 'c', 'd': 'e'}}
>>> delete_in(d, ('a', 'b'))
>>> d
{'a': {'d': 'e'}}
vivarium.library.topology.dict_to_paths(root, d)[source]

Get all the paths in a dictionary.

For example:

>>> root = ('root', 'subroot')
>>> d = {
...     'a': {
...         'b': 'c',
...     },
...     'd': 'e',
... }
>>> dict_to_paths(root, d)
[(('root', 'subroot', 'a', 'b'), 'c'), (('root', 'subroot', 'd'), 'e')]
vivarium.library.topology.get_in(d, path, default=None)[source]

Get the value from a dictionary by its path.

>>> d = {'a': {'b': 'c', 'd': 'e'}}
>>> get_in(d, ('a', 'b'))
'c'
>>> get_in(d, ('a', 'z'))
>>> get_in(d, ('a', 'z'), 'y')
'y'
vivarium.library.topology.inverse_topology(outer, update, topology, inverse=None, multi_updates=True)[source]

Transform an update from the form its process produced into one aligned to the given topology.

The inverse of this function (using a topology to construct a view for the perspective of a Process ports_schema()) lives in Store, called topology_state. This one stands alone as it does not require a store to calculate.

vivarium.library.topology.normalize_path(path)[source]

Make a path absolute by resolving .. elements.

vivarium.library.topology.paths_to_dict(path_list, f=<function <lambda>>)[source]

Create a new dictionary that has the paths in path_list.

Parameters
  • path_list – A list of tuples (path, value).

  • f – A function to apply to each value before inserting it into the dictionary.

Returns

A new dictionary with the specified values (after being passed through f) at each associated path.

vivarium.library.topology.update_in(d, path, f)[source]

Update every value in a dictionary based on f.

Parameters
  • d – The dictionary path applies to. This object is not modified.

  • path – Path to the sub-dictionary within d that should be updated.

  • f – Function to call on every value in the dictionary to update. The updated dictionary’s values will be return values from f.

Returns

A copy of d with all the values under path updated to the value returned when f is called on the original value.