add a decorator that caches function call results in a json file

master
neingeist 10 years ago
parent 9e142a8cd9
commit f6fe86a220

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

@ -2,11 +2,8 @@
<project version="4">
<component name="ChangeListManager">
<list default="true" id="7ab214b9-b539-4600-9149-e54a1f884b51" name="Default" comment="">
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/codeStyleSettings.xml" afterPath="$PROJECT_DIR$/.idea/codeStyleSettings.xml" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/least-squares.py" afterPath="$PROJECT_DIR$/least-squares.py" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/misc.xml" afterPath="$PROJECT_DIR$/.idea/misc.xml" />
<change type="NEW" beforePath="" 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/python-exercises.iml" afterPath="$PROJECT_DIR$/.idea/python-exercises.iml" />
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
</list>
<list id="22da2c6d-250c-493f-ac9c-eb57568746ab" name="New changelist" comment="" />
@ -31,13 +28,12 @@
</component>
<component name="FileEditorManager">
<leaf>
<file leaf-file-name="least-squares.py" pinned="false" current="true" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/least-squares.py">
<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 line="12" column="28" selection-start="329" selection-end="329" vertical-scroll-proportion="0.1744186" vertical-offset="105" max-vertical-offset="690">
<folding>
<element signature="e#21#68#0" expanded="true" />
</folding>
<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" />
<folding />
</state>
</provider>
</entry>
@ -57,6 +53,12 @@
<list>
<option value="$PROJECT_DIR$/anki-sin-cos-table.py" />
<option value="$PROJECT_DIR$/least-squares.py" />
<option value="$PROJECT_DIR$/test-swig.c" />
<option value="$PROJECT_DIR$/test-swig.i" />
<option value="$PROJECT_DIR$/colorama.py" />
<option value="$PROJECT_DIR$/test_colorama.py" />
<option value="$PROJECT_DIR$/convex-hull.py" />
<option value="$PROJECT_DIR$/json_memoize.py" />
</list>
</option>
</component>
@ -101,7 +103,7 @@
<option name="STATE" value="0" />
</component>
<component name="ProjectView">
<navigator currentView="ProjectPane" proportions="" version="1" splitterProportion="0.5">
<navigator currentView="ProjectPane" proportions="" version="1">
<flattenPackages />
<showMembers />
<showModules />
@ -139,7 +141,7 @@
<component name="PropertiesComponent">
<property name="recentsLimit" value="5" />
<property name="restartRequiresConfirmation" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/least-squares.py" />
<property name="last_opened_file_path" value="$PROJECT_DIR$/colorama.py" />
<property name="LayoutCode.rearrangeEntriesPython" value="false" />
<property name="options.lastSelected" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
<property name="options.splitter.main.proportions" value="0.3" />
@ -152,7 +154,45 @@
<console-settings />
</option>
</component>
<component name="RunManager" selected="Python.Unnamed">
<component name="RunManager" selected="Python.json_memoize">
<configuration default="false" name="convex-hull" type="PythonConfigurationType" factoryName="Python" temporary="true">
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<module name="python-exercises" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/convex-hull.py" />
<option name="PARAMETERS" value="" />
<RunnerSettings RunnerId="PythonRunner" />
<ConfigurationWrapper RunnerId="PythonRunner" />
<method />
</configuration>
<configuration default="false" name="json_memoize" type="PythonConfigurationType" factoryName="Python" temporary="true">
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<module name="python-exercises" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/json_memoize.py" />
<option name="PARAMETERS" value="" />
<RunnerSettings RunnerId="PyDebugRunner" />
<RunnerSettings RunnerId="PythonRunner" />
<ConfigurationWrapper RunnerId="PyDebugRunner" />
<ConfigurationWrapper RunnerId="PythonRunner" />
<method />
</configuration>
<configuration default="true" type="tests" factoryName="py.test">
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
@ -274,7 +314,7 @@
<option name="USE_PATTERN" value="false" />
<method />
</configuration>
<configuration default="false" name="Unnamed" type="PythonConfigurationType" factoryName="Python">
<configuration default="false" name="least-squares" type="PythonConfigurationType" factoryName="Python">
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
@ -294,27 +334,63 @@
<ConfigurationWrapper RunnerId="PythonRunner" />
<method />
</configuration>
<list size="1">
<item index="0" class="java.lang.String" itemvalue="Python.Unnamed" />
<configuration default="false" name="test_swig_test" type="PythonConfigurationType" factoryName="Python">
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="/usr/bin/python2.7" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<module name="python-exercises" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/test_swig_test.py" />
<option name="PARAMETERS" value="" />
<RunnerSettings RunnerId="PyDebugRunner" />
<RunnerSettings RunnerId="PythonRunner" />
<ConfigurationWrapper RunnerId="PyDebugRunner" />
<ConfigurationWrapper RunnerId="PythonRunner" />
<method>
<option name="ToolBeforeRunTask" enabled="true" actionId="Tool_make" />
</method>
</configuration>
<configuration default="false" name="colorama" type="PythonConfigurationType" factoryName="Python" singleton="true">
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="/usr/bin/python2.7" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="false" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<module name="python-exercises" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/test_colorama.py" />
<option name="PARAMETERS" value="" />
<RunnerSettings RunnerId="PythonRunner" />
<ConfigurationWrapper RunnerId="PythonRunner" />
<method />
</configuration>
<list size="5">
<item index="0" class="java.lang.String" itemvalue="Python.least-squares" />
<item index="1" class="java.lang.String" itemvalue="Python.test_swig_test" />
<item index="2" class="java.lang.String" itemvalue="Python.colorama" />
<item index="3" class="java.lang.String" itemvalue="Python.convex-hull" />
<item index="4" class="java.lang.String" itemvalue="Python.json_memoize" />
</list>
<recent_temporary>
<list size="2">
<item index="0" class="java.lang.String" itemvalue="Python.json_memoize" />
<item index="1" class="java.lang.String" itemvalue="Python.convex-hull" />
</list>
</recent_temporary>
</component>
<component name="ShelveChangesManager" show_recycled="false" />
<component name="SvnConfiguration" maxAnnotateRevisions="500" myUseAcceleration="nothing" myAutoUpdateAfterCommit="false" cleanupOnStartRun="false" SSL_PROTOCOLS="all">
<option name="USER" value="" />
<option name="PASSWORD" value="" />
<option name="mySSHConnectionTimeout" value="30000" />
<option name="mySSHReadTimeout" value="30000" />
<option name="LAST_MERGED_REVISION" />
<option name="MERGE_DRY_RUN" value="false" />
<option name="MERGE_DIFF_USE_ANCESTRY" value="true" />
<option name="UPDATE_LOCK_ON_DEMAND" value="false" />
<option name="IGNORE_SPACES_IN_MERGE" value="false" />
<option name="CHECK_NESTED_FOR_QUICK_MERGE" value="false" />
<option name="IGNORE_SPACES_IN_ANNOTATE" value="true" />
<option name="SHOW_MERGE_SOURCES_IN_ANNOTATE" value="true" />
<option name="FORCE_UPDATE" value="false" />
<option name="IGNORE_EXTERNALS" value="false" />
<myIsUseDefaultProxy>false</myIsUseDefaultProxy>
<component name="SvnConfiguration" myUseAcceleration="nothing">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
@ -362,30 +438,34 @@
<frame x="0" y="31" width="1280" height="769" extended-state="6" />
<editor active="true" />
<layout>
<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" />
<window_info id="TODO" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32876712" sideWeight="0.5" order="6" side_tool="false" content_ui="tabs" />
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.24980111" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.1627543" sideWeight="0.5" order="0" side_tool="false" content_ui="combo" />
<window_info id="Structure" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.24919094" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
<window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.16744915" sideWeight="0.4988399" order="0" side_tool="false" content_ui="combo" />
<window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="2" side_tool="true" content_ui="tabs" />
<window_info id="Favorites" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.16747573" sideWeight="0.5011601" order="2" side_tool="true" content_ui="tabs" />
<window_info id="Event Log" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.32941177" sideWeight="0.5" order="7" side_tool="true" content_ui="tabs" />
<window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.32794118" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Run" active="true" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.33235294" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
<window_info id="Version Control" 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="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="4" 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="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="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="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="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" />
</layout>
</component>
<component name="Vcs.Log.UiProperties">
<option name="RECENTLY_FILTERED_USER_GROUPS">
<collection />
</option>
<option name="RECENTLY_FILTERED_BRANCH_GROUPS">
<collection />
</option>
</component>
<component name="VcsContentAnnotationSettings">
<option name="myLimit" value="2678400000" />
@ -401,73 +481,174 @@
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<option name="time" value="1" />
<option name="time" value="2" />
</breakpoint-manager>
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/test_swig_test.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="300">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/convex-hull.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="750" max-vertical-offset="1530">
<caret line="50" column="11" selection-start-line="50" selection-start-column="11" selection-end-line="50" selection-end-column="11" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/test_swig_test.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="300">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/convex-hull.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="750" max-vertical-offset="1530">
<caret line="50" column="11" selection-start-line="50" selection-start-column="11" selection-end-line="50" selection-end-column="11" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/test_swig_test.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="315">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/convex-hull.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="240" max-vertical-offset="1365">
<caret line="43" column="11" selection-start-line="43" selection-start-column="11" selection-end-line="43" selection-end-column="11" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/test_swig_test.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="315">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/convex-hull.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="240" max-vertical-offset="1365">
<caret line="43" column="11" selection-start-line="43" selection-start-column="11" selection-end-line="43" selection-end-column="11" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/test_swig_test.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="315">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/test_swig_test.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="315">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/least-squares.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="555" max-vertical-offset="705">
<caret line="37" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/anki-sin-cos-table.py">
<provider selected="true" editor-type-id="text-editor">
<state line="22" column="28" selection-start="487" selection-end="487" vertical-scroll-proportion="0.0" vertical-offset="270" max-vertical-offset="525">
<folding>
<element signature="e#23#58#0" expanded="false" />
</folding>
<state vertical-scroll-proportion="0.0" vertical-offset="270" max-vertical-offset="525">
<caret line="22" column="28" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
</state>
</provider>
</entry>
<entry file="file://$APPLICATION_PLUGINS_DIR$/scratches/scratch.txt">
<provider selected="true" editor-type-id="text-editor">
<state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="90">
<folding />
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="90">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/anki-sin-cos-table.py">
<provider selected="true" editor-type-id="text-editor">
<state line="22" column="28" selection-start="487" selection-end="487" vertical-scroll-proportion="0.0" vertical-offset="270" max-vertical-offset="525">
<folding>
<element signature="e#23#58#0" expanded="false" />
</folding>
<state vertical-scroll-proportion="0.0" vertical-offset="270" max-vertical-offset="525">
<caret line="22" column="28" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
</state>
</provider>
</entry>
<entry file="file://$APPLICATION_PLUGINS_DIR$/scratches/scratch.txt">
<provider selected="true" editor-type-id="text-editor">
<state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="90">
<folding />
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="90">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/anki-sin-cos-table.py">
<provider selected="true" editor-type-id="text-editor">
<state line="26" column="59" selection-start="669" selection-end="669" vertical-scroll-proportion="0.0" vertical-offset="60" max-vertical-offset="510">
<folding>
<element signature="e#23#58#0" expanded="false" />
</folding>
<state vertical-scroll-proportion="0.0" vertical-offset="60" max-vertical-offset="510">
<caret line="26" column="59" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/anki-sin-cos-table.py">
<provider selected="true" editor-type-id="text-editor">
<state line="22" column="28" selection-start="487" selection-end="487" vertical-scroll-proportion="0.5254777" vertical-offset="0" max-vertical-offset="628">
<folding>
<element signature="e#23#58#0" expanded="false" />
</folding>
<state vertical-scroll-proportion="0.5254777" vertical-offset="0" max-vertical-offset="628">
<caret line="22" column="28" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
</state>
</provider>
</entry>
<entry file="file://$APPLICATION_PLUGINS_DIR$/scratches/scratch.txt">
<provider selected="true" editor-type-id="text-editor">
<state line="0" column="0" selection-start="0" selection-end="0" vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="630">
<folding />
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="630">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/least-squares.py">
<provider selected="true" editor-type-id="text-editor">
<state line="12" column="28" selection-start="329" selection-end="329" vertical-scroll-proportion="0.1744186" vertical-offset="105" max-vertical-offset="690">
<folding>
<element signature="e#21#68#0" expanded="true" />
</folding>
<state vertical-scroll-proportion="0.0" vertical-offset="52" max-vertical-offset="705">
<caret line="37" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/convex-hull.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="345" max-vertical-offset="1530">
<caret line="50" column="11" selection-start-line="50" selection-start-column="11" selection-end-line="50" selection-end-column="11" />
<folding />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/test_swig_test.py">
<provider selected="true" editor-type-id="text-editor">
<state vertical-scroll-proportion="0.0" vertical-offset="0" max-vertical-offset="653">
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
<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.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" />
<folding />
</state>
</provider>
</entry>

@ -0,0 +1,78 @@
from __future__ import division, print_function
from functools import update_wrapper, wraps
import json
import os
def json_memoize(filename):
"""Return a decorator that memoizes function calls in a JSON file."""
class JsonMemoize:
def __init__(self, fn):
self.fn = fn
update_wrapper(self, fn)
if not os.path.exists(filename):
with open(filename, "w") as f:
json.dump({}, f)
# XXX should work with **kwargs, too.
def __call__(self, *args):
with open(filename, "r") as f:
memo = json.load(f)
if repr(args) not in memo:
memo[repr(args)] = self.fn(*args)
with open(filename, "w") as f:
json.dump(memo, f)
return memo[repr(args)]
return JsonMemoize
def timed(f):
"""Return a timed version of function f.
The returned function returns a tuple of (time, real return value)
:param f: function to time
"""
@wraps(f)
def wrapper(*args, **kwargs):
import time
time_start = time.time()
return_ = f(*args, **kwargs)
time_end = time.time()
time_delta = time_end - time_start
return time_delta, return_
return wrapper
@json_memoize('json_memoize_tmp.json')
def is_prime(n):
"""Really inefficiently check if n is a prime number."""
if n == 0 or n == 1:
return False
for i in xrange(2, n):
if n % i == 0:
return False
return True
assert (is_prime(0) is False)
assert (is_prime(1) is False)
assert (is_prime(2) is True)
assert (is_prime(2 * 3 * 4 * 5 * 6 * 7 * 8 * 9 * 10) is False)
is_prime_timed = timed(is_prime)
for c in range(1, 11):
time, result = is_prime_timed(86028157)
assert (result is True)
print("Call {}: {:.2f}s".format(c, time))
Loading…
Cancel
Save