Cells#
- class Cells(_impl)[source]#
Callable object with a formula that computes and caches values based on arguments.
Cells represent formulas in modelx that can compute values for different argument combinations. They function like spreadsheet cells but with parameters, allowing you to define recursive formulas with automatic dependency tracking and caching.
Key Characteristics:
Callable: Invoke like a function to compute values
Cached: Results are stored and reused (configurable via
is_cached)Parameterized: Accept arguments like regular Python functions
Recursive: Can reference themselves and other cells
Dependency-tracked: The modelx system tracks formula dependencies automatically
Dictionary-like (Not available within formulas): Access cached values via subscription (
cells[args])
- Creation:
Cells can be created in several ways:
Using decorator (recommended):
>>> @mx.defcells ... def present_value(t): ... if t == 0: ... return 0 ... return cashflow(t) / (1 + disc_rate()) ** t + present_value(t-1)
Using new_cells method:
>>> def pv_formula(t): # Can be any valid identifier, such as '_' ... return cashflow(t) / (1 + disc_rate()) ** t >>> space.new_cells(name='pv', formula=pv_formula)
- Usage:
Cells can be called like functions or accessed like dictionaries:
>>> present_value(10) # Call with argument 95420.5 >>> present_value[10] # Access cached value 95420.5 >>> 10 in present_value # Check if value exists True
- Caching Behavior:
By default, computed values are cached. For unhashable arguments, set
is_cachedtoFalse:>>> @mx.defcells ... def process_data(data: list): # list is unhashable ... return sum(data) >>> process_data.is_cached = False >>> process_data([1, 2, 3]) 6
- Input vs Calculated Values:
Cells distinguish between input values (assigned directly) and calculated values (computed by formula):
>>> cells[5] = 100 # Input value >>> cells.is_input(5) True >>> cells(10) # Calculated value 250 >>> cells.is_input(10) False
See also
defcells(): Decorator to create cellscached(): Decorator for cached cells (default)uncached(): Decorator for uncached cellsnew_cells(): Create cells programmaticallyUserSpace: Container for cellsChanged in version 0.1.0: Separated
clear()(calculated values only) fromclear_all()(all values including input)
Basic properties#
Name of the object. |
|
Dotted name of the object. |
|
Documentation string |
|
|
Set the |
Whether a cells can have None as its value. |
|
The model this object belongs to. |
|
The parent of this object. |
|
|
Set property |
Property to get or set whether the Cells is cached. |
|
An object whose |
Cells operations#
Value operations#
Formula operations#
Property to get, set, delete formula. |
|
|
Set formula from a function. |
Clear the formula. |
|
A tuple of parameter strings. |
Node operations#
|
Return a Node object for the given arguments. |
|
Return a list of predecessors of a cell. |
|
Return a list of successors of a cell. |
|
Return a list of the precedents. |