Skip to content
Snippets Groups Projects
Commit f8fc0ded authored by Mads M. Pedersen's avatar Mads M. Pedersen
Browse files

example of cache property

parent 5e2d9ded
No related branches found
No related tags found
No related merge requests found
......@@ -17,9 +17,6 @@ import unittest
from wetb.utils.timing import get_time
from wetb.utils.caching import cache_function, set_cache_property, cache_method
import pdb
class Example(object):
......@@ -32,6 +29,11 @@ class Example(object):
def slow_function(self):
time.sleep(1)
return 1
@property
@cache_function
def test_cache_property(self):
return self.slow_function()
@cache_function
def test_cache_function(self):
......@@ -65,14 +67,14 @@ class TestCacheProperty(unittest.TestCase):
e = Example()
self.assertAlmostEqual(e.prop("test")[1], 1, 1)
self.assertAlmostEqual(e.prop("test")[1], 0, 2)
def testcache_property_pool(self):
e = Example()
e.prop("pool") #load pool
self.assertAlmostEqual(e.prop("pool")[1], 0, places=4)
#print (get_time(e.pool.map)(f, range(10)))
def test_cache_function(self):
e = Example()
self.assertAlmostEqual(get_time(e.test_cache_function)()[1], 1, places=1)
......@@ -81,8 +83,8 @@ class TestCacheProperty(unittest.TestCase):
self.assertAlmostEqual(get_time(e.test_cache_function)()[1], 0, places=1)
e.clear_cache()
self.assertAlmostEqual(get_time(e.test_cache_function)()[1], 1, places=1)
def test_cache_function_with_arguments(self):
e = Example()
self.assertAlmostEqual(get_time(e.test_cache_method1)(3)[1], 1, places=1)
......@@ -99,6 +101,15 @@ class TestCacheProperty(unittest.TestCase):
self.assertEqual(e.test_cache_method1(3), 3)
self.assertEqual(e.test_cache_method1(5), 5)
self.assertAlmostEqual(get_time(e.test_cache_method1)(5)[1], 0, places=1)
def test_cache_property(self):
e = Example()
t = time.time()
e.test_cache_property
self.assertAlmostEqual(time.time()-t, 1, places=1)
t = time.time()
e.test_cache_property
self.assertAlmostEqual(time.time()-t, 0, places=1)
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment