UserSpace.set_formula#
- UserSpace.set_formula(formula)[source]#
Set the parameter formula for this space.
Defines a formula that determines how this space creates
ItemSpaceinstances when accessed with arguments. The formula’s parameters become the space’s parameters, and its return value can control ItemSpace creation.Formula Return Values:
None (default): Use this space as base with default behavior
dict: Specify
'base'space and/or'refs'for ItemSpace
Warning
Controlling base space and references via the return value of the formula is experimental and may lead to unexpected behavior. This feature is not reflected in exported models.
- Parameters:
formula (callable or
str) – A function or lambda expression that defines the parameters. Can return None or a dict with ‘base’ and ‘refs’ keys.
Example
Below is an example of setting a parameter formula by
set_formula:>>> space = model.new_space('Projection') >>> space.set_formula(lambda product, scenario: None) >>> space.parameters ('product', 'scenario') >>> item = space['ProductA', 'Base']
This is equivalent to setting the formula via the
parametersproperty:>>> spce.parameters = ('product', 'scenario') >>> space.parameters ('product', 'scenario') >>> space.formula lambda product, scenario: None
The formula can also return a dict to control ItemSpace creation. This is an exprimental feature and not supported in exported models:
>>> space.new_space('BaseA') >>> space.new_space('BaseB') >>> def select_base(product_type): ... if product_type == 'A': ... return {'base': BaseA} ... else: ... return {'base': BaseB} >>> space.set_formula(select_base) >>> item_a = space['A'] # Uses BaseA as base space >>> item_b = space['B'] # Uses BaseB as base space
See also
del_formula(): Remove the parameter formulaformula: Access the formula objectparameters: View parameter names