armi.utils.properties module

This module contains methods for adding properties with custom behaviors to classes.

armi.utils.properties.areEqual(val1, val2, relativeTolerance=0.0)[source]
armi.utils.properties.numpyHackForEqual(val1, val2)[source]

Checks lots of types for equality like strings and dicts.

armi.utils.properties.createImmutableProperty(name, dependencyAction, doc)[source]

Create a properrty that raises useful AttributeErrors when the attribute has not been assigned.

Parameters:
  • name (str) – Name of the property. This is unfortunately necessary, because the method does not know the name of the property being assigned by the developer.

  • dependencyAction (str) – Description of an action that needs to be performed in order to set the value of the property.

  • doc (str) – Docstring of the property.

Examples

The following example is esentially exactly how this should be used.

>>> class SomeClass:
...     myNum = createImmutableProperty('myNum', 'You must invoke the initialize() method', 'My random number')
...     def initialize(self, val):
...         unlockImmutableProperties(self)
...         try:
...             self.myNum = val
...         finally:
...             lockImmutableProperties(self)
>>> sc = SomeClass()
>>> sc.myNum.__doc__
My Random Number
>>> sc.myNum # raises error, because it hasn't been assigned
ImmutablePropertyError
>>> sc.myNum = 42.1
>>> sc.myNum
42.1
>>> sc.myNum = 21.05 * 2 # raises error, because the value cannot change after it has been assigned.
ImmutablePropertyError
>>> sc.initialize(42.1) # this works, because the values are the same.
>>> sc.initialize(100) # this fails, because the value cannot change
ImmutablePropertyError
exception armi.utils.properties.ImmutablePropertyError[source]

Bases: Exception

Exception raised when performing an illegal operation on an immutable property.

armi.utils.properties.unlockImmutableProperties(lib)[source]

Unlock an object that has immutable properties for modification.

This will prevent raising errors when reading or assigning values to an immutable property

armi.utils.properties.lockImmutableProperties(lib)[source]

Lock an object that has immutable properties such that accessing unassigned properties, or attempting to modify the properties raises an exception.