modelx v0.27.0 (25 August 2024)#
This release introduces the following enhancements and bug fixes.
To update modelx to the latest version, use the following command:
>>> pip install modelx --upgrade
Anaconda users should use the conda command instead:
>>> conda update modelx
Enhancements#
Introduction of Uncached Cells#
The Cells class now includes a new property,
is_cached, which indicates whether a cells is cache-enabled.
By setting this property to False, a cells becomes cache-disabled,
meaning that a call to the cells does not cache its returned value,
and the cells is executed every time it’s called.
To create an uncached cells,
the defcells() function now includes an is_cached parameter,
which specifies whether to create or update a cached or uncached cells.
Additionally,
the uncached() decorator has been introduced
as an alias for defcells(is_cached=False).
This decorator allows for the straightforward creation of uncached cells.
cached() has also been introduced as an alies for defcells().
import modelx as mx
@mx.cached
def cash_inflows():
return [1, 2, 3]
@mx.cached
def cash_outflows():
return [2, 1, 0]
@mx.uncached
def get_pv(cashflows: list):
rate = 0.05
disc = 1 / (1 + rate)
return sum(v * disc**(i + 1) for i, v in enumerate(cashflows))
@mx.cached
def pv_inflows():
return get_pv(cash_inflows())
@mx.cached
def pv_outflows():
return get_pv(cash_outflows())
@mx.cached
def pv_netflows():
return get_pv([i - o for i, o in zip(cash_inflows(), cash_outflows())])
print(f"pv_netflows(): {pv_netflows()}")
print(f"pv_inflows() - pv_outflows(): {pv_inflows() - pv_outflows()}")
In the example above, get_pv is defined as an uncached cells,
meaning that it does not store the result of its execution.
Unlike cached cells, uncached cells can accept unhashable arguments,
such as lists, because they do not need to use the arguments as keys to store results.
Without the uncached cells get_pv, the functions pv_inflows, pv_outflows,
and pv_netflows would need to repeat the same code for calculating the present
value of their respective cash flows: inflows, outflows, and netflows.
Using uncached cells allows you to define logic shared by multiple cells in one place, reducing code duplication and improving maintainability.
API changes
defcells()now has an optional parameteris_cached.The
new_cells()method ofUserSpacehas an optional parameteris_cached.uncached()has been introduced.cached()has been introduced as an alies fordefcells().
Other API Changes#
Formula trace messages now print traces up to 20 lines (GH144)