Parameter
- class mcalf.utils.collections.Parameter(name, value=None)[source]
Bases:
object
A named parameter with a optional value.
The value can be changed at any time and very basic operations can be queued and evaluated on demand.
- Parameters:
- Raises:
ValueError – The parameter is evaluated without a value.
NotImplementedError – Operations which require parentheses are applied.
See also
Parameters
Collection of synced
Parameter
objects.
Notes
Basic operations can be applied to this object, i.e.,
+
,-
,*
and/
. TheParameter
object must be the left-most entity in an expression. Expressions which require parentheses are not supported. The only types that are supported in expressions areParameter
,float
andint
. Any type should work as a value forParameter
(such as an array), however, onlyfloat
andint
are supported. See the examples for more details in how it works.Examples
>>> Parameter('x') 'x' >>> Parameter('x', 12) '12' >>> Parameter('x') + 1 'x+1' >>> Parameter('x', 1) + 1 'x+1' >>> a = Parameter('y') * 2 >>> a 'y*2' >>> a.value = 5 >>> a == 10 True >>> a.eval() 10 >>> (a * 10).eval() 100 >>> a * 10 + 1.5 'y*2*10+1.5' >>> (a + 10) * 2 Traceback (most recent call last): ... NotImplementedError: Parameter equations with brackets are not supported.
Attributes Summary
Returns its value if set, otherwise returns its name.
Methods Summary
apply_operation
(op, other)Apply an operation to the
Parameter
.copy
()Return a copy of the parameter.
eval
()Evaluate the expression using the parameter's current value.
verify
()Enforce order of operations.
Attributes Documentation
- value_or_name
Returns its value if set, otherwise returns its name.
Methods Documentation
- apply_operation(op, other)[source]
Apply an operation to the
Parameter
.- Parameters:
- Returns:
A copy of
self
with the operation added to the queue.- Return type:
obj
Notes
You should only use this method directly if a builtin Python operation is not otherwise implemented in this class, i.e., do
a + 1
and nota.apply_operation('+', 1)
.