Skip to content

Commit ec8bb2a

Browse files
eric-s-sdrvinceknight
authored andcommitted
check for names
1 parent 2fee090 commit ec8bb2a

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

axelrod/strategy_transformers.py

+10
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,15 @@ def __repr__(self):
143143

144144
# Define a new class and wrap the strategy method
145145
# Dynamically create the new class
146+
def reducer(self_):
147+
class_module = __import__(self_.__module__)
148+
if self_.__class__.__name__ in dir(class_module):
149+
return self_.__class__, (), self_.__dict__
150+
else:
151+
print('oops {} {}'.format(
152+
self_.__class__.__name__, self_.original_class.__name__
153+
))
154+
146155
new_class = type(
147156
new_class_name, (PlayerClass,),
148157
{
@@ -153,6 +162,7 @@ def __repr__(self):
153162
"__module__": PlayerClass.__module__,
154163
"classifier": classifier,
155164
"__doc__": PlayerClass.__doc__,
165+
"__reduce__": reducer
156166
})
157167
return new_class
158168
return Decorator

axelrod/test_pickling.py

+16
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,22 @@ def test_final_transformer_called(self):
3737
results = match.play()
3838
self.assertEqual(results, [(C, C), (C, C), (D, D)])
3939

40+
def test_nice_transformer_called(self):
41+
player = axl.NMWEDeterministic()
42+
copy = pickle.loads(pickle.dumps(player))
43+
opponent_1 = axl.CyclerCCCDCD()
44+
opponent_2 = axl.CyclerCCCDCD()
45+
axl.seed(0)
46+
match_1 = axl.Match((player, opponent_1), turns=100)
47+
result_1 = match_1.play()
48+
49+
axl.seed(0)
50+
match_2 = axl.Match((copy, opponent_2), turns=100)
51+
result_2 = match_2.play()
52+
53+
54+
self.assertEqual(result_1, result_2)
55+
4056
def test_all(self):
4157
for s in axl.strategies:
4258
player = s()

0 commit comments

Comments
 (0)