-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from ashilgard/master
fixed documentation for simulatedtimeseries
- Loading branch information
Showing
2 changed files
with
113 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,29 @@ | ||
class LazyOperation(): | ||
def __init__(self, function, *args, **kwargs): | ||
'''Inits a LazyOperation that stores the provided function and arguments''' | ||
self._function = function | ||
self._args = args | ||
self._kwargs = kwargs | ||
|
||
def eval(self): | ||
# Recursively eval() lazy args | ||
'''Recursively evaluates all of the lazy arguments''' | ||
new_args = [a.eval() if isinstance(a,LazyOperation) else a for a in self._args] | ||
new_kwargs = {k:v.eval() if isinstance(v,LazyOperation) else v for k,v in self._kwargs} | ||
return self._function(*new_args, **new_kwargs) | ||
|
||
# Debug: | ||
def thunk_tree(self, indent='| '): | ||
s = indent[:-2]+'| ['+self._function.__name__+']\n' | ||
for a in self._args: | ||
if isinstance(a, LazyOperation): | ||
s += a.thunk_tree(indent=indent+'| ') | ||
else: | ||
s += indent+'| '+str(a)+'\n' | ||
for k,v in self._kwargs: | ||
if isinstance(a, LazyOperation): | ||
s += str(k)+'='+v.thunk_tree(indent=indent+'| ') | ||
else: | ||
s += indent+'| '+str(k)+'='+str(v)+'\n' | ||
return s | ||
|
||
def lazy(function): | ||
'''A decorator to create a lazy version of a function. Stores the function | ||
and arguments in a thunk for later evaluation''' | ||
def create_thunk(*args, **kwargs): | ||
return LazyOperation(function, *args, **kwargs) | ||
return create_thunk | ||
|
||
@lazy | ||
def lazy_add(a,b): | ||
return a+b | ||
'''Lazy addition. Stores arguments and function for later evaluation''' | ||
return a+b | ||
|
||
@lazy | ||
def lazy_mul(a,b): | ||
return a*b | ||
'''Lazy multiplication. Stores arguments and function for later evaluation''' | ||
return a*b |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters