You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
106 lines
2.7 KiB
Python
106 lines
2.7 KiB
Python
10 years ago
|
from __future__ import division, print_function
|
||
|
|
||
|
from functools import update_wrapper, wraps
|
||
|
import json
|
||
10 years ago
|
import operator
|
||
10 years ago
|
import os
|
||
|
|
||
|
|
||
10 years ago
|
class HashableDict(dict):
|
||
10 years ago
|
|
||
10 years ago
|
def __hash__(self):
|
||
|
return hash(tuple(sorted(self.items())))
|
||
|
|
||
|
|
||
10 years ago
|
def json_memoize(filename):
|
||
10 years ago
|
"""A decorator that memoizes function calls in a JSON file."""
|
||
10 years ago
|
|
||
|
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)
|
||
|
|
||
10 years ago
|
def __call__(self, *args, **kwargs):
|
||
10 years ago
|
with open(filename, "r") as f:
|
||
|
memo = json.load(f)
|
||
10 years ago
|
json_key = repr((self.fn.__name__, args, HashableDict(kwargs)))
|
||
10 years ago
|
if json_key not in memo:
|
||
|
memo[json_key] = self.fn(*args, **kwargs)
|
||
10 years ago
|
with open(filename, "w") as f:
|
||
10 years ago
|
json.dump(memo, f,
|
||
|
indent=4,
|
||
|
separators=(',', ': '),
|
||
|
sort_keys=True)
|
||
|
f.write("\n")
|
||
10 years ago
|
return memo[json_key]
|
||
10 years ago
|
|
||
|
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
|
||
|
|
||
|
|
||
10 years ago
|
@json_memoize('json_memoize_tmp.json')
|
||
10 years ago
|
def multiply(*args):
|
||
10 years ago
|
"""Multiply all args."""
|
||
10 years ago
|
return reduce(operator.mul, args, 1)
|
||
|
|
||
|
|
||
10 years ago
|
@json_memoize('json_memoize_tmp.json')
|
||
10 years ago
|
def some_with_kwargs(one, two, **kwargs):
|
||
10 years ago
|
"""Add 'one' and 'two' and all the values of the keyword arguments."""
|
||
10 years ago
|
return one + two + reduce(operator.add, [kwargs[k] for k in kwargs], 0)
|
||
|
|
||
|
|
||
10 years ago
|
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)
|
||
10 years ago
|
|
||
|
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))
|
||
10 years ago
|
|
||
|
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)
|