Skip to content

Commit d80fcaf

Browse files
committed
Merge pull request #402 from marcharper/super
Some necessary changes for transformers to be applied to all strategies; additional test to catch implementation issues for new strategies.
2 parents 09e0608 + b85cfcc commit d80fcaf

File tree

6 files changed

+31
-20
lines changed

6 files changed

+31
-20
lines changed

axelrod/strategies/axelrod_first.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ def __init__(self, p=0.9):
223223
"""
224224
four_vector = (p, 0, p, 0)
225225
self.p = p
226-
super(self.__class__, self).__init__(four_vector)
226+
super(Joss, self).__init__(four_vector)
227227
self.init_args = (p,)
228228

229229
def __repr__(self):
@@ -265,7 +265,7 @@ def __init__(self):
265265
(C, D): 2,
266266
(D, C): 1,
267267
(D, D): 3}
268-
super(self.__class__, self).__init__()
268+
super(Nydegger, self).__init__()
269269

270270
@staticmethod
271271
def score_history(my_history, opponent_history, score_map):

axelrod/strategies/gobymajority.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class GoByMajority40(GoByMajority):
7272
"""
7373

7474
def __init__(self, memory_depth=40, soft=True):
75-
super(self.__class__, self).__init__(memory_depth=memory_depth,
75+
super(GoByMajority40, self).__init__(memory_depth=memory_depth,
7676
soft=soft)
7777

7878

@@ -82,7 +82,7 @@ class GoByMajority20(GoByMajority):
8282
"""
8383

8484
def __init__(self, memory_depth=20, soft=True):
85-
super(self.__class__, self).__init__(memory_depth=memory_depth,
85+
super(GoByMajority20, self).__init__(memory_depth=memory_depth,
8686
soft=soft)
8787

8888
class GoByMajority10(GoByMajority):
@@ -91,7 +91,7 @@ class GoByMajority10(GoByMajority):
9191
"""
9292

9393
def __init__(self, memory_depth=10, soft=True):
94-
super(self.__class__, self).__init__(memory_depth=memory_depth,
94+
super(GoByMajority10, self).__init__(memory_depth=memory_depth,
9595
soft=soft)
9696

9797
class GoByMajority5(GoByMajority):
@@ -100,5 +100,5 @@ class GoByMajority5(GoByMajority):
100100
"""
101101

102102
def __init__(self, memory_depth=5, soft=True):
103-
super(self.__class__, self).__init__(memory_depth=memory_depth,
103+
super(GoByMajority5, self).__init__(memory_depth=memory_depth,
104104
soft=soft)

axelrod/strategies/memoryone.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def __init__(self, p=None):
9595
TitForTat is equivalent to GTFT(0)
9696
"""
9797
self.p = p
98-
super(self.__class__, self).__init__()
98+
super(GTFT, self).__init__()
9999
self.init_args = (p,)
100100

101101
def receive_tournament_attributes(self):
@@ -141,7 +141,7 @@ def __init__(self, ep=0.05):
141141

142142
self.ep = ep
143143
four_vector = (1.-ep, ep, ep, 1.-ep)
144-
super(self.__class__, self).__init__(four_vector)
144+
super(StochasticWSLS, self).__init__(four_vector)
145145
self.init_args = (ep,)
146146
self.set_four_vector(four_vector)
147147

@@ -199,13 +199,13 @@ def __init__(self, phi=0.25, s=0.5):
199199
"""
200200
self.phi = phi
201201
self.s = s
202-
super(self.__class__, self).__init__()
202+
super(ZDGTFT2, self).__init__()
203203
self.init_args = (phi, s)
204204

205205
def receive_tournament_attributes(self):
206206
(R, P, S, T) = self.tournament_attributes["game"].RPST()
207207
self.l = R
208-
super(self.__class__, self).receive_tournament_attributes(self.phi,
208+
super(ZDGTFT2, self).receive_tournament_attributes(self.phi,
209209
self.s,
210210
self.l)
211211

@@ -224,13 +224,13 @@ def __init__(self, phi=1./9, s=0.5):
224224
"""
225225
self.phi = phi
226226
self.s = s
227-
super(self.__class__, self).__init__()
227+
super(ZDExtort2, self).__init__()
228228
self.init_args = (phi, s)
229229

230230
def receive_tournament_attributes(self):
231231
(R, P, S, T) = self.tournament_attributes["game"].RPST()
232232
self.l = P
233-
super(self.__class__, self).receive_tournament_attributes(self.phi,
233+
super(ZDExtort2, self).receive_tournament_attributes(self.phi,
234234
self.s,
235235
self.l)
236236

@@ -260,7 +260,7 @@ def __init__(self, q=0.9):
260260
"""
261261
self.q = q
262262
four_vector = (1., 1 - q, 1, 1 - q)
263-
super(self.__class__, self).__init__(four_vector)
263+
super(SoftJoss, self).__init__(four_vector)
264264
self.init_args = (q,)
265265

266266
def __repr__(self):

axelrod/strategies/retaliate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Retaliate2(Retaliate):
5555
"""
5656

5757
def __init__(self, retaliation_threshold=0.08):
58-
super(self.__class__, self).__init__(
58+
super(Retaliate2, self).__init__(
5959
retaliation_threshold=retaliation_threshold)
6060

6161

@@ -65,7 +65,7 @@ class Retaliate3(Retaliate):
6565
"""
6666

6767
def __init__(self, retaliation_threshold=0.05):
68-
super(self.__class__, self).__init__(
68+
super(Retaliate3, self).__init__(
6969
retaliation_threshold=retaliation_threshold)
7070

7171

@@ -151,7 +151,7 @@ class LimitedRetaliate2(LimitedRetaliate):
151151
"""
152152

153153
def __init__(self, retaliation_threshold=0.08, retaliation_limit=15):
154-
super(self.__class__, self).__init__(
154+
super(LimitedRetaliate2, self).__init__(
155155
retaliation_threshold=retaliation_threshold,
156156
retaliation_limit=retaliation_limit)
157157

@@ -163,6 +163,6 @@ class LimitedRetaliate3(LimitedRetaliate):
163163
"""
164164

165165
def __init__(self, retaliation_threshold=0.05, retaliation_limit=20):
166-
super(self.__class__, self).__init__(
166+
super(LimitedRetaliate3, self).__init__(
167167
retaliation_threshold=retaliation_threshold,
168168
retaliation_limit=retaliation_limit)

axelrod/strategy_transformers.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
See the various Meta strategies for another type of transformation.
77
"""
88

9+
import inspect
910
import random
1011
from types import FunctionType
1112

@@ -74,10 +75,12 @@ def __call__(self, PlayerClass):
7475
# with `strategy_wrapper`
7576
def strategy(self, opponent):
7677
# Is the original strategy method a static method?
77-
if isinstance(PlayerClass.__dict__["strategy"], staticmethod):
78-
proposed_action = PlayerClass.strategy(opponent)
79-
else:
78+
argspec = inspect.getargspec(getattr(PlayerClass, "strategy"))
79+
if 'self' in argspec.args:
80+
# it's not a static method
8081
proposed_action = PlayerClass.strategy(self, opponent)
82+
else:
83+
proposed_action = PlayerClass.strategy(opponent)
8184
# Apply the wrapper
8285
return strategy_wrapper(self, opponent, proposed_action,
8386
*args, **kwargs)

axelrod/tests/unit/test_strategy_transformers.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010

1111
class TestTransformers(unittest.TestCase):
1212

13+
def test_all_strategies(self):
14+
# Attempt to transform each strategy to ensure that implemenation
15+
# choices (like use of super) do not cause issues
16+
for s in axelrod.ordinary_strategies:
17+
opponent = axelrod.Cooperator()
18+
player = IdentityTransformer(s)()
19+
player.play(opponent)
20+
1321
def test_naming(self):
1422
"""Tests that the player and class names are properly modified."""
1523
cls = FlipTransformer(axelrod.Cooperator)

0 commit comments

Comments
 (0)