Skip to content

Commit c8cd217

Browse files
authored
Merge pull request #775 from drvinceknight/remove-memoized
Remove memoized for py3 caching
2 parents 4dd83e4 + e4fe842 commit c8cd217

File tree

2 files changed

+2
-66
lines changed

2 files changed

+2
-66
lines changed

axelrod/_strategy_utils.py

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""Utilities used by various strategies"""
22
import itertools
3+
from functools import lru_cache
34

45
from axelrod import update_history
56
from axelrod import Actions
@@ -82,34 +83,7 @@ def look_ahead(player_1, player_2, game, rounds=10):
8283
return strategies[results.index(max(results))]
8384

8485

85-
class Memoized(object):
86-
"""Decorator that caches a function's return value each time it is called.
87-
If called later with the same arguments, the cached value is returned
88-
(not reevaluated). From:
89-
https://wiki.python.org/moin/PythonDecoratorLibrary#Memoize
90-
"""
91-
92-
def __init__(self, func):
93-
self.func = func
94-
self.cache = {}
95-
96-
def __call__(self, *args):
97-
try:
98-
try:
99-
return self.cache[args]
100-
except KeyError:
101-
value = self.func(*args)
102-
self.cache[args] = value
103-
return value
104-
except TypeError:
105-
return self.func(*args)
106-
107-
def __repr__(self):
108-
"""Return the function's docstring."""
109-
return self.func.__doc__
110-
111-
112-
@Memoized
86+
@lru_cache()
11387
def recursive_thue_morse(n):
11488
"""The recursive definition of the Thue-Morse sequence. The first few terms
11589
of the Thue-Morse sequence are: 0 1 1 0 1 0 0 1 1 0 0 1 0 1 1 0 . . ."""

axelrod/tests/unit/test_strategy_utils.py

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
from axelrod import Actions
99
from axelrod._strategy_utils import detect_cycle
10-
from axelrod._strategy_utils import Memoized
1110

1211
C, D = Actions.C, Actions.D
1312

@@ -26,40 +25,3 @@ def test_no_cycle(self):
2625

2726
history = [D, D, C, C, C]
2827
self.assertIsNone(detect_cycle(history))
29-
30-
31-
class TestMemoized(unittest.TestCase):
32-
"""Test the Memoized class"""
33-
34-
def test_init(self):
35-
func = lambda x: x + x
36-
memoized = Memoized(func)
37-
self.assertEqual(memoized.func, func)
38-
self.assertEqual(memoized.cache, {})
39-
40-
def test_call_with_unhashable_type(self):
41-
func = lambda x: x + x
42-
memoized = Memoized(func)
43-
self.assertEqual(memoized([2]), [2, 2])
44-
self.assertEqual(memoized.cache, {})
45-
46-
def test_call(self):
47-
func = lambda x: x + x
48-
memoized = Memoized(func)
49-
self.assertEqual(memoized.cache, {})
50-
self.assertEqual(memoized(2), 4)
51-
self.assertEqual(memoized.cache, {(2,): 4})
52-
self.assertEqual(memoized(2), 4)
53-
self.assertEqual(memoized.cache, {(2,): 4})
54-
55-
def test_repr(self):
56-
func = lambda x: x + x
57-
memoized = Memoized(func)
58-
self.assertEqual(memoized.__repr__(), None)
59-
60-
def func_with_docstring(x):
61-
"""A docstring"""
62-
return x + x
63-
64-
memoized = Memoized(func_with_docstring)
65-
self.assertEqual(memoized.__repr__(), "A docstring")

0 commit comments

Comments
 (0)