Skip to content

Commit 46eebed

Browse files
eric-s-sdrvinceknight
authored andcommitted
before change to decorator __reduce__
1 parent 47516d1 commit 46eebed

File tree

2 files changed

+29
-47
lines changed

2 files changed

+29
-47
lines changed

axelrod/strategy_transformers.py

+10-33
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313
from .action import Action
1414
from .random_ import random_choice
1515
from importlib import import_module
16-
from pydoc import locate
17-
import axelrod
1816

1917

2018
C, D = Action.C, Action.D
@@ -61,6 +59,7 @@ def __init__(self, *args, **kwargs):
6159
self.name_prefix = name_prefix
6260

6361
def __reduce__(self):
62+
# Creates only the Decorator class and not the instance
6463
return StrategyTransformerFactory, (
6564
strategy_wrapper, name_prefix, reclassifier)
6665

@@ -91,26 +90,18 @@ def __call__(self, PlayerClass):
9190
except KeyError:
9291
pass
9392

94-
# Is the original strategy method a static method?
95-
# signature = inspect.signature(PlayerClass.strategy)
96-
# strategy_args = [p.name for p in signature.parameters.values()
97-
# if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD]
98-
# is_static = True
99-
# if len(strategy_args) > 1:
100-
# is_static = False
101-
10293
# Define the new strategy method, wrapping the existing method
10394
# with `strategy_wrapper`
10495
def strategy(self, opponent):
105-
if strategy_wrapper != dual_wrapper:
96+
if strategy_wrapper == dual_wrapper:
97+
# Dummy Action for dual_wrapper.
98+
# This is to avoid calling the strategy twice.
99+
proposed_action = C
100+
else:
106101
if is_strategy_static(PlayerClass):
107-
# static method
108102
proposed_action = PlayerClass.strategy(opponent)
109103
else:
110104
proposed_action = PlayerClass.strategy(self, opponent)
111-
else:
112-
# dummy Action for dual_wrapper to avoid calling class
113-
proposed_action = C
114105

115106
# Apply the wrapper
116107
return strategy_wrapper(self, opponent, proposed_action,
@@ -151,7 +142,7 @@ def __repr__(self):
151142
prefix = ', '
152143
return name
153144

154-
def new_class_reduce(self_):
145+
def reduce_for_decorated_class(self_):
155146
class_module = import_module(self_.__module__)
156147
import_name = self_.__class__.__name__
157148
if import_name in dir(class_module):
@@ -167,7 +158,7 @@ def new_class_reduce(self_):
167158
break
168159

169160
return (
170-
NewRecon(),
161+
ReConstructor(),
171162
(decorators, import_name, self_.__module__),
172163
self_.__dict__
173164
)
@@ -185,14 +176,14 @@ def new_class_reduce(self_):
185176
"__module__": PlayerClass.__module__,
186177
"classifier": classifier,
187178
"__doc__": PlayerClass.__doc__,
188-
"__reduce__": new_class_reduce,
179+
"__reduce__": reduce_for_decorated_class,
189180
})
190181

191182
return new_class
192183
return Decorator
193184

194185

195-
class NewRecon(object):
186+
class ReConstructor(object):
196187
def __init__(self):
197188
pass
198189

@@ -209,20 +200,6 @@ def __call__(self, decorators, import_name, module_name):
209200
return generated_class()
210201

211202

212-
# class Reconstitutor(object):
213-
# def __init__(self):
214-
#
215-
# pass
216-
#
217-
# def __call__(self, decorators, player_class, original_name):
218-
# use_class = player_class
219-
#
220-
# for decorator, args, kwargs in decorators:
221-
# use_class = decorator(*args, **kwargs)(use_class)
222-
# obj = use_class()
223-
# obj.__class__.__name__ = original_name
224-
# return obj
225-
226203
def compose_transformers(t1, t2):
227204
"""Compose transformers without having to invoke the first on
228205
a PlayerClass."""

axelrod/tests/unit/test_pickling.py

+19-14
Original file line numberDiff line numberDiff line change
@@ -144,26 +144,31 @@ def test_pointer_to_class_derived_from_Player(self):
144144
)
145145

146146
self.assert_original_plays_same_as_pickled(player, turns=10)
147-
148147
self.assert_instance_with_history_equality(player)
149148

150149
def test_regression_dual_transformer_with_lookerup(self):
151-
player = DualTransformer()(LookerUp)()
152-
self.assert_instance_with_history_equality(player)
150+
self.assert_dual_wrapper_correct(axl.LookerUp)
151+
self.assert_dual_wrapper_correct(axl.EvolvedLookerUp2_2_2)
152+
153+
def test_regression_dual_transformer_with_fsm(self):
154+
self.assert_dual_wrapper_correct(axl.Fortress3)
155+
self.assert_dual_wrapper_correct(axl.Fortress4)
156+
157+
def assert_dual_wrapper_correct(self, player_class):
158+
p1 = player_class()
159+
p2 = DualTransformer()(player_class)()
160+
p3 = axl.CyclerCCD() # Cycles 'CCD'
161+
for _ in range(10):
162+
p1.play(p3)
163+
164+
p3.reset()
165+
166+
for _ in range(10):
167+
p2.play(p3)
153168

154-
fp = AshlockFingerprint(axl.Cooperator, probe=LookerUp())
155-
fp.fingerprint(turns=10, repetitions=2, step=0.2)
169+
self.assertEqual(p1.history, [x.flip() for x in p2.history])
156170

157-
fp = AshlockFingerprint(EvolvedLookerUp1_1_1)
158-
fp.fingerprint(turns=10, repetitions=2, step=0.2)
159171

160-
strategy = axl.WinStayLoseShift
161-
probe = axl.TitForTat
162-
af = axl.AshlockFingerprint(strategy, probe)
163-
data = af.fingerprint(turns=50, repetitions=2, step=0.01)
164-
# data = af.fingerprint(turns=5, repetitions=2, step=0.1)
165-
p = af.plot()
166-
p.savefig('omfg')
167172

168173

169174

0 commit comments

Comments
 (0)