SyncedParameters

class mcalf.utils.collections.SyncedParameters[source]

Bases: object

Database for keeping Parameter objects in sync.

Parameter objects with the same name can be linked with this class and kept in sync with each other.

Examples

>>> a = Parameter('x') + 1
>>> b = Parameter('x', 2) * 3
>>> c = Parameter('y')

Now that Parameter objects have been created, we can keep them in sync.

>>> s = SyncedParameters()
>>> s.track_object(a)
>>> s.track_object(b)
>>> s.track_object(c)

Notice how the value of a has been synced to match the value provided in b.

>>> a == 3
True

The value of a named parameter can be updated and the change is propagated to all Parameter objects of that name.

>>> s.update_parameter('x', 3)
>>> a == 4
True
>>> b == 9
True

Methods Summary

exists(name)

Whether any parameters of a particular name are tracked.

get_parameter(name)

Get the value of the named parameter.

has_value(name)

Whether the named parameter has a value set.

track_object(obj)

Add a new Parameter object to keep in sync.

update_parameter(name, value)

Update the value of a named parameter.

Methods Documentation

exists(name: str) bool[source]

Whether any parameters of a particular name are tracked.

get_parameter(name)[source]

Get the value of the named parameter.

Parameters:

name (str) – Name of parameter to get value of.

Returns:

The current value of the parameter. None if not set.

Return type:

value

has_value(name: str) bool[source]

Whether the named parameter has a value set.

track_object(obj)[source]

Add a new Parameter object to keep in sync.

Parameters:

obj (Parameter) – The parameter object to keep in sync.

Notes

Any objects added must not have a value that contradicts the existing value of the parameter (if set). Objects added which have no value set will inherit the value of the other parameters of the same name.

update_parameter(name, value)[source]

Update the value of a named parameter.

All tracked Parameter objects of this name will be updated.

Parameters:
  • name (string) – Name of parameter to update.

  • value (float or int) – New value of parameter. Other types may work.