Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
W
WindEnergyToolbox
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
toolbox
WindEnergyToolbox
Commits
f8fc0ded
Commit
f8fc0ded
authored
7 years ago
by
Mads M. Pedersen
Browse files
Options
Downloads
Patches
Plain Diff
example of cache property
parent
5e2d9ded
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
wetb/utils/tests/test_caching.py
+18
-7
18 additions, 7 deletions
wetb/utils/tests/test_caching.py
with
18 additions
and
7 deletions
wetb/utils/tests/test_caching.py
+
18
−
7
View file @
f8fc0ded
...
...
@@ -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
()
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment