DEV: Add zipline.utils.memoize.

- Moved zipline.utils.lazyval.
- Added `remember_last` which is just `lru_cache(1)` with simpler logic.
This commit is contained in:
Scott Sanderson
2015-09-16 01:28:15 -04:00
parent dad3bbd879
commit 58ceb7b7bb
5 changed files with 140 additions and 5 deletions
+1 -1
View File
@@ -45,7 +45,7 @@ from zipline.modelling.factor.technical import (
MaxDrawdown,
SimpleMovingAverage,
)
from zipline.utils.lazyval import lazyval
from zipline.utils.memoize import lazyval
from zipline.utils.test_utils import (
make_rotating_asset_info,
make_simple_asset_info,
+3 -3
View File
@@ -9,7 +9,7 @@ from zipline.modelling import (
expression,
)
from zipline.utils import (
lazyval,
memoize,
test_utils,
)
@@ -47,8 +47,8 @@ class DoctestTestCase(TestCase):
def test_engine_docs(self):
self._check_docs(engine)
def test_lazyval_docs(self):
self._check_docs(lazyval)
def test_memoize_docs(self):
self._check_docs(memoize)
def test_test_utils_docs(self):
self._check_docs(test_utils)
+34
View File
@@ -0,0 +1,34 @@
"""
Tests for zipline.utils.memoize.
"""
from unittest import TestCase
from zipline.utils.memoize import remember_last
class TestRememberLast(TestCase):
def test_remember_last(self):
# Store the count in a list so we can mutate it from inside `func`.
call_count = [0]
@remember_last
def func(x):
call_count[0] += 1
return x
self.assertEqual((func(1), call_count[0]), (1, 1))
# Calling again with the same argument should just re-use the old
# value, which means func shouldn't get called again.
self.assertEqual((func(1), call_count[0]), (1, 1))
self.assertEqual((func(1), call_count[0]), (1, 1))
# Calling with a new value should increment the counter.
self.assertEqual((func(2), call_count[0]), (2, 2))
self.assertEqual((func(2), call_count[0]), (2, 2))
# Calling the old value should still increment the counter.
self.assertEqual((func(1), call_count[0]), (1, 3))
self.assertEqual((func(1), call_count[0]), (1, 3))