Skip to content

Commit 9621756

Browse files
eric-s-sdrvinceknight
authored andcommitted
tests pass. still a mess
1 parent 08bb03e commit 9621756

File tree

3 files changed

+47
-36
lines changed

3 files changed

+47
-36
lines changed

axelrod/strategy_transformers.py

+36-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
from numpy.random import choice
1313
from .action import Action
1414
from .random_ import random_choice
15+
from importlib import import_module
16+
from pydoc import locate
17+
import axelrod
1518

1619

1720
C, D = Action.C, Action.D
@@ -57,6 +60,10 @@ def __init__(self, *args, **kwargs):
5760
else:
5861
self.name_prefix = name_prefix
5962

63+
def __reduce__(self):
64+
return StrategyTransformerFactory, (
65+
strategy_wrapper, name_prefix, reclassifier)
66+
6067
def __call__(self, PlayerClass):
6168
"""
6269
Parameters
@@ -144,22 +151,33 @@ def __repr__(self):
144151
# Define a new class and wrap the strategy method
145152
# Dynamically create the new class
146153
def reducer(self_):
147-
class_module = __import__(self_.__module__)
154+
class_module = import_module(self_.__module__)
148155
if self_.__class__.__name__ in dir(class_module):
149156
return self_.__class__, (), self_.__dict__
150157

151-
152158
else:
153-
print('oops {} {}'.format(
154-
self_.__class__.__name__, self_.original_class.__name__
155-
))
159+
160+
decorators = [self_.decorator]
161+
pc = self_.original_class
162+
original_name = pc.__name__
163+
for klass in pc.mro():
164+
if hasattr(klass, 'decorator'):
165+
decorators.append(klass.decorator)
166+
if klass in axelrod.strategies:
167+
pc = klass
168+
break
169+
return (Reconstitutor(),
170+
(decorators, pc, original_name),
171+
self_.__dict__)
172+
156173

157174
new_class = type(
158175
new_class_name, (PlayerClass,),
159176
{
160177
"name": name,
161178
"original_class": PlayerClass,
162179
"strategy": strategy,
180+
"decorator": (self, self.args, self.kwargs),
163181
"__repr__": __repr__,
164182
"__module__": PlayerClass.__module__,
165183
"classifier": classifier,
@@ -169,6 +187,19 @@ def reducer(self_):
169187
return new_class
170188
return Decorator
171189

190+
class Reconstitutor(object):
191+
def __init__(self):
192+
193+
pass
194+
195+
def __call__(self, decorators, player_class, original_name):
196+
use_class = player_class
197+
198+
for decorator, args, kwargs in decorators:
199+
use_class = decorator(*args, **kwargs)(use_class)
200+
obj = use_class()
201+
obj.__class__.__name__ = original_name
202+
return obj
172203

173204
def compose_transformers(t1, t2):
174205
"""Compose transformers without having to invoke the first on

axelrod/my_t.py axelrod/tests/classes_for_testing_pickling.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,14 @@
66

77
@st.FlipTransformer()
88
@st.FlipTransformer()
9-
class TestFlip(axl.Cooperator):
9+
class DoubleFlip(axl.Cooperator):
1010
pass
1111

1212

13+
@st.FlipTransformer()
14+
class SingleFlip(axl.Cooperator):
15+
pass
16+
1317
@st.ApologyTransformer([D], [C])
1418
class Apology(axl.Cooperator):
1519
pass

axelrod/test_pickling.py axelrod/tests/unit/test_pickling.py

+6-30
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import unittest
21
import pickle
2+
import unittest
3+
34
import axelrod as axl
45
from axelrod.strategy_transformers import FlipTransformer
5-
from axelrod.my_t import transformed, TestFlip
6-
6+
from axelrod.tests.classes_for_testing_pickling import transformed, DoubleFlip
77

88
C, D = axl.Action.C, axl.Action.D
99

@@ -68,47 +68,23 @@ def test_all(self):
6868
self.assertEqual(reconstituted, player)
6969

7070
def test_pickling_transformers(self):
71-
player = transformed[0]()
72-
print(player.__dict__)
73-
print(player.__class__.original_class)
7471

7572
for s in transformed:
7673
player = s()
7774
player.play(axl.Cooperator())
78-
# print(player.name, player.__class__.__name__)
7975
reconstituted = pickle.loads(pickle.dumps(player))
80-
8176
self.assertEqual(reconstituted, player)
8277

8378
def test_created(self):
8479
x = FlipTransformer()(axl.Cooperator)
8580
x = FlipTransformer()(x)
8681
x = FlipTransformer()(x)()
87-
# print(x.__reduce__())
88-
# print('created')
89-
# print(repr(pickle.dumps(x)))
9082
z = pickle.loads(pickle.dumps(x))
91-
# print(z)
92-
# print(z.__dict__)
83+
9384
self.assertEqual(x, z)
9485

9586
def test_created_two(self):
96-
# print('created_two')
97-
x = TestFlip()
98-
# print(x.__dict__)
99-
# print(x.__class__.__dict__)
100-
# print(x.__class__.__name__)
101-
# print(x.__dir__())
87+
x = DoubleFlip()
10288
z = pickle.dumps(x)
10389
self.assertEqual(x, pickle.loads(z))
104-
105-
# def test_reconsitutor(self):
106-
# x = TestFlip()
107-
# print('reconstructor')
108-
# print(x.decorator, x.original_class)
109-
# y = Reconstitutor()(x.decorator, x.original_class, x.__class__.__name__)
110-
# print(type(y))
111-
# y.__dict__.update(x.__dict__)
112-
# print(y.name)
113-
# print(y.__class__.__name__)
114-
# self.assertEqual(y, x)
90+
self.assert_original_acts_same_as_pickled(x, turns=10)

0 commit comments

Comments
 (0)