support kwargs in the json memoize decorator
This commit is contained in:
parent
f6fe86a220
commit
ba7555ccc0
3 changed files with 65 additions and 15 deletions
1
.idea/dictionaries/orange.xml
generated
1
.idea/dictionaries/orange.xml
generated
|
@ -4,6 +4,7 @@
|
||||||
<w>anki</w>
|
<w>anki</w>
|
||||||
<w>matplotlib</w>
|
<w>matplotlib</w>
|
||||||
<w>memoize</w>
|
<w>memoize</w>
|
||||||
|
<w>memoizes</w>
|
||||||
</words>
|
</words>
|
||||||
</dictionary>
|
</dictionary>
|
||||||
</component>
|
</component>
|
47
.idea/workspace.xml
generated
47
.idea/workspace.xml
generated
|
@ -2,7 +2,7 @@
|
||||||
<project version="4">
|
<project version="4">
|
||||||
<component name="ChangeListManager">
|
<component name="ChangeListManager">
|
||||||
<list default="true" id="7ab214b9-b539-4600-9149-e54a1f884b51" name="Default" comment="">
|
<list default="true" id="7ab214b9-b539-4600-9149-e54a1f884b51" name="Default" comment="">
|
||||||
<change type="NEW" beforePath="" afterPath="$PROJECT_DIR$/json_memoize.py" />
|
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/json_memoize.py" afterPath="$PROJECT_DIR$/json_memoize.py" />
|
||||||
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/dictionaries/orange.xml" afterPath="$PROJECT_DIR$/.idea/dictionaries/orange.xml" />
|
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/dictionaries/orange.xml" afterPath="$PROJECT_DIR$/.idea/dictionaries/orange.xml" />
|
||||||
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
|
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
|
||||||
</list>
|
</list>
|
||||||
|
@ -31,8 +31,20 @@
|
||||||
<file leaf-file-name="json_memoize.py" pinned="false" current="true" current-in-tab="true">
|
<file leaf-file-name="json_memoize.py" pinned="false" current="true" current-in-tab="true">
|
||||||
<entry file="file://$PROJECT_DIR$/json_memoize.py">
|
<entry file="file://$PROJECT_DIR$/json_memoize.py">
|
||||||
<provider selected="true" editor-type-id="text-editor">
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
<state vertical-scroll-proportion="0.0" vertical-offset="60" max-vertical-offset="1260">
|
<state vertical-scroll-proportion="0.7096019" vertical-offset="12" max-vertical-offset="1590">
|
||||||
<caret line="4" column="8" selection-start-line="4" selection-start-column="8" selection-end-line="4" selection-end-column="8" />
|
<caret line="21" column="0" selection-start-line="21" selection-start-column="0" selection-end-line="21" selection-end-column="0" />
|
||||||
|
<folding>
|
||||||
|
<element signature="e#0#47#0" expanded="true" />
|
||||||
|
</folding>
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
|
</file>
|
||||||
|
<file leaf-file-name="sörös.py" pinned="false" current="false" current-in-tab="false">
|
||||||
|
<entry file="file://$USER_HOME$/studienarbeit/barcodedetect/experiments/sörös/sörös.py">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="3390">
|
||||||
|
<caret line="17" column="16" selection-start-line="17" selection-start-column="16" selection-end-line="17" selection-end-column="16" />
|
||||||
<folding />
|
<folding />
|
||||||
</state>
|
</state>
|
||||||
</provider>
|
</provider>
|
||||||
|
@ -411,7 +423,11 @@
|
||||||
<created>1394440608337</created>
|
<created>1394440608337</created>
|
||||||
<updated>1394440608337</updated>
|
<updated>1394440608337</updated>
|
||||||
</task>
|
</task>
|
||||||
<option name="localTasksCounter" value="4" />
|
<task id="LOCAL-00004" summary="add a decorator that caches function call results in a json file">
|
||||||
|
<created>1409340342370</created>
|
||||||
|
<updated>1409340342370</updated>
|
||||||
|
</task>
|
||||||
|
<option name="localTasksCounter" value="5" />
|
||||||
<servers />
|
<servers />
|
||||||
</component>
|
</component>
|
||||||
<component name="TodoView" selected-index="0">
|
<component name="TodoView" selected-index="0">
|
||||||
|
@ -438,6 +454,7 @@
|
||||||
<frame x="0" y="31" width="1280" height="769" extended-state="6" />
|
<frame x="0" y="31" width="1280" height="769" extended-state="6" />
|
||||||
<editor active="true" />
|
<editor active="true" />
|
||||||
<layout>
|
<layout>
|
||||||
|
<window_info id="Messages" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
|
||||||
<window_info id="Documentation" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="FLOATING" visible="false" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" x="100" y="131" width="1080" height="569" />
|
<window_info id="Documentation" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="FLOATING" visible="false" weight="0.33" sideWeight="0.5" order="-1" side_tool="false" content_ui="tabs" x="100" y="131" width="1080" height="569" />
|
||||||
<window_info id="Changes" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32876712" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
|
<window_info id="Changes" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32876712" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
|
||||||
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32876712" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
|
<window_info id="Terminal" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32876712" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
|
||||||
|
@ -453,7 +470,6 @@
|
||||||
<window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
|
<window_info id="Message" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
|
||||||
<window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
|
<window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
|
||||||
<window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32941177" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
|
<window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32941177" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
|
||||||
<window_info id="Messages" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
|
|
||||||
<window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="SLIDING" type="SLIDING" visible="false" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
|
<window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="SLIDING" type="SLIDING" visible="false" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
|
||||||
<window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
|
<window_info id="Hierarchy" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="2" side_tool="false" content_ui="combo" />
|
||||||
<window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.4" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
|
<window_info id="Inspection" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.4" sideWeight="0.5" order="5" side_tool="false" content_ui="tabs" />
|
||||||
|
@ -477,11 +493,12 @@
|
||||||
<MESSAGE value="Restructure" />
|
<MESSAGE value="Restructure" />
|
||||||
<MESSAGE value="include trigonometric function values for a step size of pi/6" />
|
<MESSAGE value="include trigonometric function values for a step size of pi/6" />
|
||||||
<MESSAGE value="reduce code line length to 80 characters max" />
|
<MESSAGE value="reduce code line length to 80 characters max" />
|
||||||
<option name="LAST_COMMIT_MESSAGE" value="reduce code line length to 80 characters max" />
|
<MESSAGE value="add a decorator that caches function call results in a json file" />
|
||||||
|
<option name="LAST_COMMIT_MESSAGE" value="add a decorator that caches function call results in a json file" />
|
||||||
</component>
|
</component>
|
||||||
<component name="XDebuggerManager">
|
<component name="XDebuggerManager">
|
||||||
<breakpoint-manager>
|
<breakpoint-manager>
|
||||||
<option name="time" value="2" />
|
<option name="time" value="4" />
|
||||||
</breakpoint-manager>
|
</breakpoint-manager>
|
||||||
</component>
|
</component>
|
||||||
<component name="editorHistoryManager">
|
<component name="editorHistoryManager">
|
||||||
|
@ -644,11 +661,21 @@
|
||||||
</state>
|
</state>
|
||||||
</provider>
|
</provider>
|
||||||
</entry>
|
</entry>
|
||||||
|
<entry file="file://$USER_HOME$/studienarbeit/barcodedetect/experiments/sörös/sörös.py">
|
||||||
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
|
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="3390">
|
||||||
|
<caret line="17" column="16" selection-start-line="17" selection-start-column="16" selection-end-line="17" selection-end-column="16" />
|
||||||
|
<folding />
|
||||||
|
</state>
|
||||||
|
</provider>
|
||||||
|
</entry>
|
||||||
<entry file="file://$PROJECT_DIR$/json_memoize.py">
|
<entry file="file://$PROJECT_DIR$/json_memoize.py">
|
||||||
<provider selected="true" editor-type-id="text-editor">
|
<provider selected="true" editor-type-id="text-editor">
|
||||||
<state vertical-scroll-proportion="0.0" vertical-offset="60" max-vertical-offset="1260">
|
<state vertical-scroll-proportion="0.7096019" vertical-offset="12" max-vertical-offset="1590">
|
||||||
<caret line="4" column="8" selection-start-line="4" selection-start-column="8" selection-end-line="4" selection-end-column="8" />
|
<caret line="21" column="0" selection-start-line="21" selection-start-column="0" selection-end-line="21" selection-end-column="0" />
|
||||||
<folding />
|
<folding>
|
||||||
|
<element signature="e#0#47#0" expanded="true" />
|
||||||
|
</folding>
|
||||||
</state>
|
</state>
|
||||||
</provider>
|
</provider>
|
||||||
</entry>
|
</entry>
|
||||||
|
|
|
@ -2,9 +2,15 @@ from __future__ import division, print_function
|
||||||
|
|
||||||
from functools import update_wrapper, wraps
|
from functools import update_wrapper, wraps
|
||||||
import json
|
import json
|
||||||
|
import operator
|
||||||
import os
|
import os
|
||||||
|
|
||||||
|
|
||||||
|
class HashableDict(dict):
|
||||||
|
def __hash__(self):
|
||||||
|
return hash(tuple(sorted(self.items())))
|
||||||
|
|
||||||
|
|
||||||
def json_memoize(filename):
|
def json_memoize(filename):
|
||||||
"""Return a decorator that memoizes function calls in a JSON file."""
|
"""Return a decorator that memoizes function calls in a JSON file."""
|
||||||
|
|
||||||
|
@ -18,16 +24,16 @@ def json_memoize(filename):
|
||||||
with open(filename, "w") as f:
|
with open(filename, "w") as f:
|
||||||
json.dump({}, f)
|
json.dump({}, f)
|
||||||
|
|
||||||
# XXX should work with **kwargs, too.
|
def __call__(self, *args, **kwargs):
|
||||||
def __call__(self, *args):
|
|
||||||
|
|
||||||
with open(filename, "r") as f:
|
with open(filename, "r") as f:
|
||||||
memo = json.load(f)
|
memo = json.load(f)
|
||||||
if repr(args) not in memo:
|
json_key = repr((args, HashableDict(kwargs)))
|
||||||
memo[repr(args)] = self.fn(*args)
|
if json_key not in memo:
|
||||||
|
memo[json_key] = self.fn(*args, **kwargs)
|
||||||
with open(filename, "w") as f:
|
with open(filename, "w") as f:
|
||||||
json.dump(memo, f)
|
json.dump(memo, f)
|
||||||
return memo[repr(args)]
|
return memo[json_key]
|
||||||
|
|
||||||
return JsonMemoize
|
return JsonMemoize
|
||||||
|
|
||||||
|
@ -66,6 +72,17 @@ def is_prime(n):
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
@json_memoize('json_memoize_tmp2.json')
|
||||||
|
def multiply(*args):
|
||||||
|
return reduce(operator.mul, args, 1)
|
||||||
|
|
||||||
|
|
||||||
|
@json_memoize('json_memoize_tmp3.json')
|
||||||
|
def some_with_kwargs(one, two, **kwargs):
|
||||||
|
print([kwargs[k] for k in kwargs])
|
||||||
|
return one + two + reduce(operator.add, [kwargs[k] for k in kwargs], 0)
|
||||||
|
|
||||||
|
|
||||||
assert (is_prime(0) is False)
|
assert (is_prime(0) is False)
|
||||||
assert (is_prime(1) is False)
|
assert (is_prime(1) is False)
|
||||||
assert (is_prime(2) is True)
|
assert (is_prime(2) is True)
|
||||||
|
@ -76,3 +93,8 @@ for c in range(1, 11):
|
||||||
time, result = is_prime_timed(86028157)
|
time, result = is_prime_timed(86028157)
|
||||||
assert (result is True)
|
assert (result is True)
|
||||||
print("Call {}: {:.2f}s".format(c, time))
|
print("Call {}: {:.2f}s".format(c, time))
|
||||||
|
|
||||||
|
assert(multiply(1, 2, 3, 4) == 24)
|
||||||
|
assert(multiply(1, 2, 3, 4, 5) == 120)
|
||||||
|
|
||||||
|
assert(some_with_kwargs(1, 2, three=3, four=4) == 10)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue