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
  • name (str) – Name of parameter.

  • value (int or float, optional, default=None) – Value of parameter. Other types may work but not supported.

name

Name of parameter.

Type

str

value

Value of parameter, without operations applied.

Type

int or float

operations

List of operations to apply.

Type

list

Raises

See also

Parameters

Collection of synced Parameter objects.

Notes

Basic operations can be applied to this object, i.e., +, -, * and /. The Parameter 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 are Parameter, float and int. Any type should work as a value for Parameter (such as an array), however, only float and int 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

value_or_name

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
  • op (str) – Operation symbol to apply. +, -, * or /.

  • other (float or int) – Number on RHS.

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 not a.apply_operation('+', 1).

copy()[source]

Return a copy of the parameter.

eval()[source]

Evaluate the expression using the parameter’s current value.

verify()[source]

Enforce order of operations.