support kwargs in the json memoize decorator

master
neingeist 10 years ago
parent f6fe86a220
commit ba7555ccc0

@ -4,6 +4,7 @@
<w>anki</w>
<w>matplotlib</w>
<w>memoize</w>
<w>memoizes</w>
</words>
</dictionary>
</component>

@ -2,7 +2,7 @@
<project version="4">
<component name="ChangeListManager">
<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/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
</list>
@ -31,8 +31,20 @@
<file leaf-file-name="json_memoize.py" pinned="false" current="true" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/json_memoize.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="60" max-vertical-offset="1260">
<caret line="4" column="8" selection-start-line="4" selection-start-column="8" selection-end-line="4" selection-end-column="8" />
<state vertical-scroll-proportion="0.7096019" vertical-offset="12" max-vertical-offset="1590">
<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 />
</state>
</provider>
@ -411,7 +423,11 @@
<created>1394440608337</created>
<updated>1394440608337</updated>
</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 />
</component>
<component name="TodoView" selected-index="0">
@ -438,6 +454,7 @@
<frame x="0" y="31" width="1280" height="769" extended-state="6" />
<editor active="true" />
<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="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" />
@ -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="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="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="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" />
@ -477,11 +493,12 @@
<MESSAGE value="Restructure" />
<MESSAGE value="include trigonometric function values for a step size of pi/6" />
<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 name="XDebuggerManager">
<breakpoint-manager>
<option name="time" value="2" />
<option name="time" value="4" />
</breakpoint-manager>
</component>
<component name="editorHistoryManager">
@ -644,14 +661,24 @@
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/json_memoize.py">
<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="60" max-vertical-offset="1260">
<caret line="4" column="8" selection-start-line="4" selection-start-column="8" selection-end-line="4" selection-end-column="8" />
<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">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.7096019" vertical-offset="12" max-vertical-offset="1590">
<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>
</component>
</project>

@ -2,9 +2,15 @@ from __future__ import division, print_function
from functools import update_wrapper, wraps
import json
import operator
import os
class HashableDict(dict):
def __hash__(self):
return hash(tuple(sorted(self.items())))
def json_memoize(filename):
"""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:
json.dump({}, f)
# XXX should work with **kwargs, too.
def __call__(self, *args):
def __call__(self, *args, **kwargs):
with open(filename, "r") as f:
memo = json.load(f)
if repr(args) not in memo:
memo[repr(args)] = self.fn(*args)
json_key = repr((args, HashableDict(kwargs)))
if json_key not in memo:
memo[json_key] = self.fn(*args, **kwargs)
with open(filename, "w") as f:
json.dump(memo, f)
return memo[repr(args)]
return memo[json_key]
return JsonMemoize
@ -66,6 +72,17 @@ def is_prime(n):
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(1) is False)
assert (is_prime(2) is True)
@ -76,3 +93,8 @@ for c in range(1, 11):
time, result = is_prime_timed(86028157)
assert (result is True)
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…
Cancel
Save