Util.py 497 B

12345678910111213141516
  1. # Future imports for Python 2.7, mandatory in 3.0
  2. from __future__ import division
  3. from __future__ import print_function
  4. from __future__ import unicode_literals
  5. def memoized(fn):
  6. """Decorator: memoize a function."""
  7. memory = {}
  8. def memoized_fn(*args, **kwargs):
  9. key = (args, tuple(sorted(kwargs.items())))
  10. try:
  11. result = memory[key]
  12. except KeyError:
  13. result = memory[key] = fn(*args, **kwargs)
  14. return result
  15. return memoized_fn