Skip to content

Commit 87355ce

Browse files
authored
Merge pull request #657 from Axelrod-Python/642
Implement name_prefix as opt arg for transformer
2 parents b0c6d2e + 77cede8 commit 87355ce

File tree

6 files changed

+48
-50
lines changed

6 files changed

+48
-50
lines changed

axelrod/strategies/backstabber.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
C, D = Actions.C, Actions.D
55

66

7-
@FinalTransformer((D, D)) # End with two defections
7+
@FinalTransformer((D, D), name_prefix=None) # End with two defections
88
class BackStabber(Player):
99
"""
1010
Forgives the first 3 defections but on the fourth
@@ -29,7 +29,7 @@ def strategy(opponent):
2929
return D
3030
return C
3131

32-
@FinalTransformer((D, D)) # End with two defections
32+
@FinalTransformer((D, D), name_prefix=None) # End with two defections
3333
class DoubleCrosser(Player):
3434
"""
3535
Forgives the first 3 defections but on the fourth

axelrod/strategies/gambler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
C, D = Actions.C, Actions.D
99

1010

11-
@FinalTransformer((D, D)) # End with two defections if tournament length is known
11+
@FinalTransformer((D, D), name_prefix=None) # End with two defections if tournament length is known
1212
class Gambler(LookerUp):
1313
"""
1414
A LookerUp class player which will select randomly an action in some cases.

axelrod/strategies/titfortat.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
from axelrod import Actions, Player, init_args, flip_action
2-
from axelrod.strategy_transformers import (StrategyTransformerFactory,
3-
history_track_wrapper)
1+
from axelrod import Actions, Player, init_args
2+
from axelrod.strategy_transformers import TrackHistoryTransformer
43

54
C, D = Actions.C, Actions.D
65

@@ -335,11 +334,7 @@ def reset(self):
335334
self.punishment_limit = 0
336335

337336

338-
Transformer = StrategyTransformerFactory(
339-
history_track_wrapper, name_prefix=None)()
340-
341-
342-
@Transformer
337+
@TrackHistoryTransformer(name_prefix=None)
343338
class ContriteTitForTat(Player):
344339
"""
345340
A player that corresponds to Tit For Tat if there is no noise. In the case

axelrod/strategy_transformers.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ def __call__(self, PlayerClass):
7171

7272
args = self.args
7373
kwargs = self.kwargs
74+
try:
75+
#if "name_prefix" in kwargs remove as only want dec arguments
76+
del kwargs["name_prefix"]
77+
except KeyError:
78+
pass
7479

7580
# Define the new strategy method, wrapping the existing method
7681
# with `strategy_wrapper`
@@ -144,15 +149,15 @@ def generic_strategy_wrapper(player, opponent, proposed_action, *args, **kwargs)
144149
# This example just passes through the proposed_action
145150
return proposed_action
146151

147-
IdentityTransformer = StrategyTransformerFactory(generic_strategy_wrapper)()
152+
IdentityTransformer = StrategyTransformerFactory(generic_strategy_wrapper)
148153

149154

150155
def flip_wrapper(player, opponent, action):
151156
"""Applies flip_action at the class level."""
152157
return flip_action(action)
153158

154159
FlipTransformer = StrategyTransformerFactory(
155-
flip_wrapper, name_prefix="Flipped")()
160+
flip_wrapper, name_prefix="Flipped")
156161

157162

158163
def noisy_wrapper(player, opponent, action, noise=0.05):
@@ -186,7 +191,8 @@ def initial_sequence(player, opponent, action, initial_seq):
186191
return initial_seq[index]
187192
return action
188193

189-
InitialTransformer = StrategyTransformerFactory(initial_sequence)
194+
InitialTransformer = StrategyTransformerFactory(initial_sequence,
195+
name_prefix="Initial")
190196

191197

192198
def final_sequence(player, opponent, action, seq):
@@ -210,7 +216,8 @@ def final_sequence(player, opponent, action, seq):
210216
return seq[-index]
211217
return action
212218

213-
FinalTransformer = StrategyTransformerFactory(final_sequence)
219+
FinalTransformer = StrategyTransformerFactory(final_sequence,
220+
name_prefix="Final")
214221

215222

216223
def history_track_wrapper(player, opponent, action):
@@ -222,7 +229,7 @@ def history_track_wrapper(player, opponent, action):
222229
return action
223230

224231
TrackHistoryTransformer = StrategyTransformerFactory(
225-
history_track_wrapper, name_prefix="HistoryTracking")()
232+
history_track_wrapper, name_prefix="HistoryTracking")
226233

227234

228235
def deadlock_break_wrapper(player, opponent, action):
@@ -238,7 +245,7 @@ def deadlock_break_wrapper(player, opponent, action):
238245
return action
239246

240247
DeadlockBreakingTransformer = StrategyTransformerFactory(
241-
deadlock_break_wrapper, name_prefix="DeadlockBreaking")()
248+
deadlock_break_wrapper, name_prefix="DeadlockBreaking")
242249

243250

244251
def grudge_wrapper(player, opponent, action, grudges):
@@ -345,4 +352,4 @@ def __call__(self, player, opponent, action):
345352
return action
346353

347354
RetaliateUntilApologyTransformer = StrategyTransformerFactory(
348-
RetaliationUntilApologyWrapper(), name_prefix="RUA")()
355+
RetaliationUntilApologyWrapper(), name_prefix="RUA")

axelrod/tests/unit/test_strategy_transformers.py

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ def test_all_strategies(self):
2121
# choices (like use of super) do not cause issues
2222
for s in axelrod.ordinary_strategies:
2323
opponent = axelrod.Cooperator()
24-
player = IdentityTransformer(s)()
24+
player = IdentityTransformer()(s)()
2525
player.play(opponent)
2626

2727
def test_naming(self):
2828
"""Tests that the player and class names are properly modified."""
29-
cls = FlipTransformer(axelrod.Cooperator)
29+
cls = FlipTransformer()(axelrod.Cooperator)
3030
p1 = cls()
3131
self.assertEqual(cls.__name__, "FlippedCooperator")
3232
self.assertEqual(p1.name, "Flipped Cooperator")
@@ -45,7 +45,7 @@ def test_cloning(self):
4545
"""Tests that Player.clone preserves the application of transformations.
4646
"""
4747
p1 = axelrod.Cooperator()
48-
p2 = FlipTransformer(axelrod.Cooperator)() # Defector
48+
p2 = FlipTransformer()(axelrod.Cooperator)() # Defector
4949
p3 = p2.clone()
5050
self.assertEqual(simulate_play(p1, p3), (C, D))
5151
self.assertEqual(simulate_play(p1, p3), (C, D))
@@ -63,7 +63,7 @@ def test_generic(self):
6363
def test_flip_transformer(self):
6464
"""Tests that FlipTransformer(Cooperator) == Defector."""
6565
p1 = axelrod.Cooperator()
66-
p2 = FlipTransformer(axelrod.Cooperator)() # Defector
66+
p2 = FlipTransformer()(axelrod.Cooperator)() # Defector
6767
self.assertEqual(simulate_play(p1, p2), (C, D))
6868
self.assertEqual(simulate_play(p1, p2), (C, D))
6969
self.assertEqual(simulate_play(p1, p2), (C, D))
@@ -122,7 +122,7 @@ def test_final_transformer2(self):
122122
def test_history_track(self):
123123
"""Tests the history tracking transformer."""
124124
p1 = axelrod.Cooperator()
125-
p2 = TrackHistoryTransformer(axelrod.Random)()
125+
p2 = TrackHistoryTransformer()(axelrod.Random)()
126126
for _ in range(6):
127127
p1.play(p2)
128128
self.assertEqual(p2.history, p2._recorded_history)
@@ -181,7 +181,7 @@ def test_retailiation(self):
181181

182182
def test_retaliation_until_apology(self):
183183
"""Tests the RetaliateUntilApologyTransformer."""
184-
TFT = RetaliateUntilApologyTransformer(axelrod.Cooperator)
184+
TFT = RetaliateUntilApologyTransformer()(axelrod.Cooperator)
185185
p1 = TFT()
186186
p2 = axelrod.Cooperator()
187187
p1.play(p2)
@@ -276,7 +276,7 @@ def test_deadlock(self):
276276
# Now let's use the transformer to break the deadlock to achieve
277277
# Mutual cooperation
278278
p1 = axelrod.TitForTat()
279-
p2 = DeadlockBreakingTransformer(InitialTransformer([D])(axelrod.TitForTat))()
279+
p2 = DeadlockBreakingTransformer()(InitialTransformer([D])(axelrod.TitForTat))()
280280
for _ in range(4):
281281
p1.play(p2)
282282
self.assertEqual(p1.history, [C, D, C, C])
@@ -301,9 +301,9 @@ def test_grudging(self):
301301
def test_nilpotency(self):
302302
"""Show that some of the transformers are (sometimes) nilpotent, i.e.
303303
that transfomer(transformer(PlayerClass)) == PlayerClass"""
304-
for transformer in [IdentityTransformer,
305-
FlipTransformer,
306-
TrackHistoryTransformer]:
304+
for transformer in [IdentityTransformer(),
305+
FlipTransformer(),
306+
TrackHistoryTransformer()]:
307307
for PlayerClass in [axelrod.Cooperator, axelrod.Defector]:
308308
for third_player in [axelrod.Cooperator(), axelrod.Defector()]:
309309
player = PlayerClass()
@@ -320,13 +320,13 @@ def test_idempotency(self):
320320
transfomer(transformer(PlayerClass)) == transformer(PlayerClass).
321321
That means that the transformer is a projection on the set of
322322
strategies."""
323-
for transformer in [IdentityTransformer, GrudgeTransformer(1),
323+
for transformer in [IdentityTransformer(), GrudgeTransformer(1),
324324
FinalTransformer([C]), FinalTransformer([D]),
325325
InitialTransformer([C]), InitialTransformer([D]),
326-
DeadlockBreakingTransformer,
326+
DeadlockBreakingTransformer(),
327327
RetaliationTransformer(1),
328-
RetaliateUntilApologyTransformer,
329-
TrackHistoryTransformer,
328+
RetaliateUntilApologyTransformer(),
329+
TrackHistoryTransformer(),
330330
ApologyTransformer([D], [C])]:
331331
for PlayerClass in [axelrod.Cooperator, axelrod.Defector]:
332332
for third_player in [axelrod.Cooperator(), axelrod.Defector()]:
@@ -345,25 +345,19 @@ def test_implementation(self):
345345
the implementation matters, not just the outcomes."""
346346
# Difference between Alternator and CyclerCD
347347
p1 = axelrod.Cycler(cycle="CD")
348-
p2 = FlipTransformer(axelrod.Cycler)(cycle="CD")
348+
p2 = FlipTransformer()(axelrod.Cycler)(cycle="CD")
349349
for _ in range(5):
350350
p1.play(p2)
351351
self.assertEqual(p1.history, [C, D, C, D, C])
352352
self.assertEqual(p2.history, [D, C, D, C, D])
353353

354354
p1 = axelrod.Alternator()
355-
p2 = FlipTransformer(axelrod.Alternator)()
355+
p2 = FlipTransformer()(axelrod.Alternator)()
356356
for _ in range(5):
357357
p1.play(p2)
358358
self.assertEqual(p1.history, [C, D, C, D, C])
359359
self.assertEqual(p2.history, [D, D, D, D, D])
360360

361-
def test_namespace(self):
362-
test_object = TestClass()
363-
self.assertEqual(
364-
test_object.__class__,
365-
axelrod.tests.unit.test_strategy_transformers.TestClass)
366-
367361

368362
# Test that RUA(Cooperator) is the same as TitForTat
369363
# reusing the TFT tests. Since TFT is completely specified by its tests,
@@ -372,7 +366,7 @@ def test_namespace(self):
372366
# this alters Cooperator's class variable, and causes its test to fail
373367
# So for now this is commented out.
374368

375-
TFT = RetaliateUntilApologyTransformer(axelrod.Cooperator)
369+
TFT = RetaliateUntilApologyTransformer()(axelrod.Cooperator)
376370

377371
class TestRUAisTFT(TestTitForTat):
378372
# This runs the 7 TFT tests when unittest is invoked
@@ -388,7 +382,7 @@ class TestRUAisTFT(TestTitForTat):
388382
}
389383

390384
# Test that FlipTransformer(Defector) == Cooperator
391-
Cooperator2 = FlipTransformer(axelrod.Defector)
385+
Cooperator2 = FlipTransformer()(axelrod.Defector)
392386

393387
class TestFlipDefector(TestCooperator):
394388
# This runs the 7 TFT tests when unittest is invoked

docs/tutorials/advanced/strategy_transformers.rst

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ C to D and D to C::
1212

1313
>>> import axelrod
1414
>>> from axelrod.strategy_transformers import *
15-
>>> FlippedCooperator = FlipTransformer(axelrod.Cooperator)
15+
>>> FlippedCooperator = FlipTransformer()(axelrod.Cooperator)
1616
>>> player = FlippedCooperator()
1717
>>> opponent = axelrod.Cooperator()
1818
>>> player.strategy(opponent)
@@ -29,15 +29,17 @@ class and player::
2929
>>> FlippedCooperator.name
3030
'Flipped Cooperator'
3131

32-
This behavor can be supressed by setting the :code:`name_prefix` argument::
32+
This behavior can be suppressed by setting the :code:`name_prefix` argument::
3333

34-
FlipTransformer = StrategyTransformerFactory(flip_wrapper, name_prefix="")()
34+
>>> FlippedCooperator = FlipTransformer(name_prefix=None)(axelrod.Cooperator)
35+
>>> player = FlippedCooperator()
36+
>>> player.name
37+
'Cooperator'
3538

3639
Note carefully that the transformer returns a class, not an instance of a class.
3740
This means that you need to use the Transformed class as you would normally to
3841
create a new instance::
3942

40-
>>> import axelrod
4143
>>> from axelrod.strategy_transformers import NoisyTransformer
4244
>>> player = NoisyTransformer(0.5)(axelrod.Cooperator)()
4345

@@ -52,7 +54,7 @@ The library includes the following transformers:
5254

5355
>>> import axelrod
5456
>>> from axelrod.strategy_transformers import FlipTransformer
55-
>>> FlippedCooperator = FlipTransformer(axelrod.Cooperator)
57+
>>> FlippedCooperator = FlipTransformer()(axelrod.Cooperator)
5658
>>> player = FlippedCooperator()
5759

5860
* :code:`NoisyTransformer(noise)`: Flips actions with probability :code:`noise`::
@@ -94,7 +96,7 @@ The library includes the following transformers:
9496

9597
>>> import axelrod
9698
>>> from axelrod.strategy_transformers import RetaliateUntilApologyTransformer
97-
>>> TFT = RetaliateUntilApologyTransformer(axelrod.Cooperator)
99+
>>> TFT = RetaliateUntilApologyTransformer()(axelrod.Cooperator)
98100
>>> player = TFT()
99101

100102
* :code:`ApologizingTransformer`: Apologizes after a round of :code:`(D, C)`::
@@ -116,7 +118,7 @@ The library includes the following transformers:
116118

117119
>>> import axelrod
118120
>>> from axelrod.strategy_transformers import DeadlockBreakingTransformer
119-
>>> DeadlockBreakingTFT = DeadlockBreakingTransformer(axelrod.TitForTat)
121+
>>> DeadlockBreakingTFT = DeadlockBreakingTransformer()(axelrod.TitForTat)
120122
>>> player = DeadlockBreakingTFT()
121123

122124
* :code:`GrudgeTransformer(N)`: Defections unconditionally after more than N defections::
@@ -130,7 +132,7 @@ The library includes the following transformers:
130132

131133
>>> import axelrod
132134
>>> from axelrod.strategy_transformers import TrackHistoryTransformer
133-
>>> player = TrackHistoryTransformer(axelrod.Random)()
135+
>>> player = TrackHistoryTransformer()(axelrod.Random)()
134136

135137
* :code:`MixedTransformer`: Randomly plays a mutation to another strategy (or
136138
set of strategies. Here is the syntax to do this with a set of strategies::

0 commit comments

Comments
 (0)