OLD | NEW |
1 # Copyright 2014 The Chromium Authors. All rights reserved. | 1 # Copyright 2014 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 import sys | 5 import sys |
6 | 6 |
7 # pylint: disable=C0301 | 7 # pylint: disable=C0301 |
8 # Based on/taken from | 8 # Based on/taken from |
9 # http://code.activestate.com/recipes/578231-probably-the-fastest-memoization-
decorator-in-the-/ | 9 # http://code.activestate.com/recipes/578231-probably-the-fastest-memoization-
decorator-in-the-/ |
10 # (with cosmetic changes). | 10 # (with cosmetic changes). |
11 # pylint: enable=C0301 | 11 # pylint: enable=C0301 |
12 def memoize(f): | 12 def memoize(f): |
13 """Memoization decorator for a function taking a single argument.""" | 13 """Memoization decorator for a function taking a single argument.""" |
14 class Memoize(dict): | 14 class Memoize(dict): |
15 def __missing__(self, key): | 15 def __missing__(self, key): |
16 rv = self[key] = f(key) | 16 rv = self[key] = f(key) |
17 return rv | 17 return rv |
18 return Memoize().__getitem__ | 18 return Memoize().__getitem__ |
OLD | NEW |