param module

A constainer class for arbitrary data, with type and state checking.

Param is the primary class introduces in this file. Empty is a singleton class denoting an “empty” parameter. The ParameterError class is also defined here.

class porchlight.param.Empty

Bases: object

An empty class representing missing parameters values.

At initialization, if an instance does not already exist it is created. No instance of Empty exists until it has been instantiated once.

It is recommended that Empty be used over None to denote parameters that have not been initialized with any value, so that None can be treated unambiguously when using porchlight.

class porchlight.param.Param(name: str, value: ~typing.Any = <porchlight.param.Empty object>, constant: bool = False, restrict: ~typing.Callable | None = None)

Bases: object

Container class for arbitrary Python data.

Although mutable, editing Param objects directly is strongly discouraged unless absolutely necessary.

Param uses __slots__, and no attributes other than those listed below may be assigned to Param objects.

_name

Parameter name.

Type:

str

_value

Value of the parameter. If the parameter does not contain an assigned value, this should be Empty.

Type:

Any

_type

The type corresponding to the type of Param._value.

Type:

type

constants

True if this object should be considered a constant. If the Param value is modified by Param.value’s setter, but constant is True, a ParameterError will be raised.

Type:

bool

restrict

If a callable, it will be invoked on the parameter whenever the parameter is changed. If it evaluates to False, a ParameterError is raised.

Example: “temperature” parameter should not be negative or zero in our model:

See Param.value for further details.

Type:

Callable or None

__init__(name: str, value: ~typing.Any = <porchlight.param.Empty object>, constant: bool = False, restrict: ~typing.Callable | None = None)

Initializes the Param object.

Parameters:
  • name (str) – Parameter name.

  • value (Any) – Value of the parameter. If the parameter does not contain an assigned value, this should be ~porchlight.param.Empty

  • constant (bool) – True if this object should be considered a constant. If the Param value is modified by Param.value’s setter, but constant is True, a ParameterError will be raised.

  • restrict (Callable or None) – If a callable is passed, whenever the value property of the parameter is set, restrict will be called on the candidate value. If the result evaluates to False, a ParameterError will be raised.

exception porchlight.param.ParameterError

Bases: Exception

Error for Param-specific exceptions.