Skip to content

Commit

Permalink
destroy kwargs + Before/AfterRun
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthewCaseres committed May 12, 2024
1 parent 36fa7bc commit 291da2d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 25 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ Model:

- projection controller
- class to subclass with your proprietary models
- `BeforeRun` and `AfterRun` methods
- get all values as a list with `values` attribute
- get the sum of all values with `sum()` method

Expand Down
35 changes: 11 additions & 24 deletions src/heavylight/heavylight.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,22 @@
class _Cache:
"""Cache provides controllable memoization for model methods"""

def __init__(self, func, param_len, param_names):
def __init__(self, func):
self.func = func
self.param_len = param_len
self.param_names = param_names
self.param_len = len(signature(func).parameters)
self.param_names = tuple(signature(func).parameters)
self.has_one_param = self.param_len == 1
self._store = dict()
self.__name__ = func.__name__

def __call__(self, *arg):
if arg in self._store:
return self._store[arg]
def __call__(self, *args, **kwargs):
if len(kwargs) > 0:
raise ValueError("Keyword arguments are not supported in heavylight")
if args in self._store:
return self._store[args]
else:
result = self.func(*arg)
self._store[arg] = result
result = self.func(*args)
self._store[args] = result
return result

def __repr__(self):
Expand Down Expand Up @@ -62,13 +64,6 @@ def __init__(self, *, do_run = None, proj_len:int = 0, **kwargs,):
RunModel(proj_len):
Run the model to proj_len.
Special user methods:
BeforeRun(self):
If this is specified in the user model it called before the projection starts, e.g. to set up some specific variables
AfterRun(self):
user method, called after Run is completed, e.g. can use to calculate NPVs of variables
methods/variables to avoid:
methods/variables starting with an underscore `_` are treated as internal. You may break functionality if you create your own.
Expand Down Expand Up @@ -113,9 +108,6 @@ def RunModel(self, proj_len: int):
if self._is_run:
# TODO: replace this with ability to run further, but warn that earlier values not recalculated?
raise ValueError("Run has already been completed.")

if hasattr(self, "BeforeRun"):
self.BeforeRun()

if not self._cached:
raise ValueError("Functions have not been cached") # NB: this shouldn't occur as now caching in instance
Expand All @@ -124,9 +116,6 @@ def RunModel(self, proj_len: int):
if func.has_one_param and func.param_names[0] == 't': # skip functions with more than one parameter
func(t) #call each function in turn, starting from t==0
self._is_run = True

if hasattr(self, "AfterRun"):
return self.AfterRun()

def _cache_funcs(self):
if self._cached:
Expand All @@ -136,9 +125,7 @@ def _cache_funcs(self):

for method_name, method in getmembers(self):
if method_name[0] != "_" and method_name[0].islower() and isinstance(method, types.MethodType):
param_count = len(signature(method).parameters) # count the parameters in the function.
param_names = tuple(signature(method).parameters)
cached_method = _Cache(method, param_count, param_names)
cached_method = _Cache(method)
setattr(self, method_name, cached_method)
self._funcs[method_name] = cached_method

Expand Down
6 changes: 6 additions & 0 deletions tests/test_heavylight.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from heavylight import Model
import pytest

class SimpleModel(Model):
def t(self, t):
Expand Down Expand Up @@ -41,6 +42,11 @@ def func_t(self, t):
def func_a(self, a):
return 100 * a

def test_no_kwargs(): # we do not support keyword arguments because multiple possible hashes for same function call
sm = UnitModel()
with pytest.raises(ValueError):
sm.t(t=5)

def test_UnitModel():
proj_len = 10
model = UnitModel(proj_len = proj_len)
Expand Down

0 comments on commit 291da2d

Please sign in to comment.