UserSpace.new_cells_from_module#
- UserSpace.new_cells_from_module(module)[source]#
Create multiple cells from functions defined in a Python module.
Scans the specified module for function definitions and creates a
Cellsobject for each function found. The cells are named after their corresponding functions.This is useful for organizing formulas in separate Python files and importing them into modelx spaces.
- Parameters:
module (module or
str) – Either a Python module object or the module name as a string. If a string, the module is imported.- Returns:
- Dictionary mapping cell names to
Cells objects for all functions found in the module.
- Dictionary mapping cell names to
- Return type:
Example
Create a file
formulas.py:def present_value(t): return cashflow(t) / (1 + rate) ** t def future_value(t): return cashflow(t) * (1 + rate) ** t
Import the functions:
>>> space = model.new_space('Projection') >>> space.new_cells_from_module('formulas') {'present_value': <Cells present_value(t)>, 'future_value': <Cells future_value(t)>}
See also
new_cells(): Create a single cellsimport_funcs(): Deprecated alias for this method
Note
Only top-level function definitions are imported. Nested functions, lambda expressions, and class methods are not included.