Skip to content

Commit 1c8163d

Browse files
authored
Merge pull request #700 from Axelrod-Python/names_and_PEP8
Strategy names and PEP8
2 parents ffb0574 + e0a9fce commit 1c8163d

File tree

7 files changed

+76
-15
lines changed

7 files changed

+76
-15
lines changed

axelrod/player.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,19 @@ def is_basic(s):
2020
inspects_source = s.classifier['inspects_source']
2121
manipulates_source = s.classifier['manipulates_source']
2222
manipulates_state = s.classifier['manipulates_state']
23-
return (not stochastic) and (not inspects_source) and (not manipulates_source) and (not manipulates_state) and (depth in (0, 1))
23+
return (
24+
not stochastic and
25+
not inspects_source and
26+
not manipulates_source and
27+
not manipulates_state and
28+
depth in (0, 1)
29+
)
2430

2531

2632
def obey_axelrod(s):
2733
"""
28-
A function to check if a strategy obeys Axelrod's original tournament rules.
34+
A function to check if a strategy obeys Axelrod's original tournament
35+
rules.
2936
"""
3037
classifier = s.classifier
3138
return not (

axelrod/strategies/cycler.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import copy
44

5+
56
class AntiCycler(Player):
67
"""
78
A player that follows a sequence of plays that contains no cycles:
@@ -67,7 +68,7 @@ def __init__(self, cycle="CCD"):
6768
"""
6869
Player.__init__(self)
6970
self.cycle = cycle
70-
self.name += " " + cycle
71+
self.name = "Cycler {}".format(cycle)
7172
self.classifier['memory_depth'] = len(cycle) - 1
7273

7374
def strategy(self, opponent):
@@ -77,6 +78,8 @@ def strategy(self, opponent):
7778

7879

7980
class CyclerDC(Cycler):
81+
82+
name = 'Cycler DC'
8083
classifier = copy.copy(Cycler.classifier)
8184
classifier['memory_depth'] = 1
8285

@@ -86,6 +89,8 @@ def __init__(self, cycle="DC"):
8689

8790

8891
class CyclerCCD(Cycler):
92+
93+
name = 'Cycler CCD'
8994
classifier = copy.copy(Cycler.classifier)
9095
classifier['memory_depth'] = 2
9196

@@ -95,6 +100,8 @@ def __init__(self, cycle="CCD"):
95100

96101

97102
class CyclerDDC(Cycler):
103+
104+
name = 'Cycler DDC'
98105
classifier = copy.copy(Cycler.classifier)
99106
classifier['memory_depth'] = 2
100107

@@ -104,6 +111,8 @@ def __init__(self, cycle="DDC"):
104111

105112

106113
class CyclerCCCD(Cycler):
114+
115+
name = 'Cycler CCCD'
107116
classifier = copy.copy(Cycler.classifier)
108117
classifier['memory_depth'] = 3
109118

@@ -113,6 +122,8 @@ def __init__(self, cycle="CCCD"):
113122

114123

115124
class CyclerCCCCCD(Cycler):
125+
126+
name = 'Cycler CCCCCD'
116127
classifier = copy.copy(Cycler.classifier)
117128
classifier['memory_depth'] = 5
118129

axelrod/strategies/gobymajority.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class GoByMajority(Player):
1818
default this is 0)
1919
"""
2020

21+
name = 'Go By Marjority'
2122
classifier = {
2223
'stochastic': False,
2324
'inspects_source': False,
@@ -49,7 +50,8 @@ def __init__(self, memory_depth=float('inf'), soft=True):
4950
else:
5051
self.memory = 0
5152

52-
self.name = 'Go By Majority' + (self.memory > 0) * (": %i" % self.memory)
53+
self.name = (
54+
'Go By Majority' + (self.memory > 0) * (": %i" % self.memory))
5355
if self.soft:
5456
self.name = "Soft " + self.name
5557
else:
@@ -80,6 +82,7 @@ class GoByMajority40(GoByMajority):
8082
"""
8183
GoByMajority player with a memory of 40.
8284
"""
85+
name = 'Go By Majority 40'
8386
classifier = copy.copy(GoByMajority.classifier)
8487
classifier['memory_depth'] = 40
8588

@@ -93,6 +96,7 @@ class GoByMajority20(GoByMajority):
9396
"""
9497
GoByMajority player with a memory of 20.
9598
"""
99+
name = 'Go By Majority 20'
96100
classifier = copy.copy(GoByMajority.classifier)
97101
classifier['memory_depth'] = 20
98102

@@ -106,6 +110,7 @@ class GoByMajority10(GoByMajority):
106110
"""
107111
GoByMajority player with a memory of 10.
108112
"""
113+
name = 'Go By Majority 10'
109114
classifier = copy.copy(GoByMajority.classifier)
110115
classifier['memory_depth'] = 10
111116

@@ -119,6 +124,7 @@ class GoByMajority5(GoByMajority):
119124
"""
120125
GoByMajority player with a memory of 5.
121126
"""
127+
name = 'Go By Majority 5'
122128
classifier = copy.copy(GoByMajority.classifier)
123129
classifier['memory_depth'] = 5
124130

@@ -136,6 +142,7 @@ class HardGoByMajority(GoByMajority):
136142
An optional memory attribute will limit the number of turns remembered (by
137143
default this is 0)
138144
"""
145+
name = 'Hard Go By Majority'
139146

140147
@init_args
141148
def __init__(self, memory_depth=float('inf'), soft=False):
@@ -147,6 +154,7 @@ class HardGoByMajority40(HardGoByMajority):
147154
"""
148155
HardGoByMajority player with a memory of 40.
149156
"""
157+
name = 'Hard Go By Majority 40'
150158
classifier = copy.copy(GoByMajority.classifier)
151159
classifier['memory_depth'] = 40
152160

@@ -160,6 +168,7 @@ class HardGoByMajority20(HardGoByMajority):
160168
"""
161169
HardGoByMajority player with a memory of 20.
162170
"""
171+
name = 'Hard Go By Majority 20'
163172
classifier = copy.copy(GoByMajority.classifier)
164173
classifier['memory_depth'] = 20
165174

@@ -173,6 +182,7 @@ class HardGoByMajority10(HardGoByMajority):
173182
"""
174183
HardGoByMajority player with a memory of 10.
175184
"""
185+
name = 'Hard Go By Majority 10'
176186
classifier = copy.copy(GoByMajority.classifier)
177187
classifier['memory_depth'] = 10
178188

@@ -186,6 +196,7 @@ class HardGoByMajority5(HardGoByMajority):
186196
"""
187197
HardGoByMajority player with a memory of 5.
188198
"""
199+
name = 'Hard Go By Majority 5'
189200
classifier = copy.copy(GoByMajority.classifier)
190201
classifier['memory_depth'] = 5
191202

axelrod/strategies/retaliate.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Retaliate(Player):
1010
A player starts by cooperating but will retaliate once the opponent
1111
has won more than 10 percent times the number of defections the player has.
1212
"""
13+
14+
name = 'Retaliate'
1315
classifier = {
1416
'memory_depth': float('inf'), # Long memory
1517
'stochastic': False,
@@ -52,11 +54,14 @@ def reset(self):
5254
Player.reset(self)
5355
self.play_counts = defaultdict(int)
5456

57+
5558
class Retaliate2(Retaliate):
5659
"""
5760
Retaliate player with a threshold of 8 percent.
5861
"""
5962

63+
name = 'Retaliate 2'
64+
6065
def __init__(self, retaliation_threshold=0.08):
6166
super(Retaliate2, self).__init__(
6267
retaliation_threshold=retaliation_threshold)
@@ -67,6 +72,8 @@ class Retaliate3(Retaliate):
6772
Retaliate player with a threshold of 5 percent.
6873
"""
6974

75+
name = 'Retaliate 3'
76+
7077
def __init__(self, retaliation_threshold=0.05):
7178
super(Retaliate3, self).__init__(
7279
retaliation_threshold=retaliation_threshold)
@@ -80,6 +87,7 @@ class LimitedRetaliate(Player):
8087
retaliation limit (20 defections).
8188
"""
8289

90+
name = 'Limited Retaliate'
8391
classifier = {
8492
'memory_depth': float('inf'), # Long memory
8593
'stochastic': False,
@@ -156,6 +164,8 @@ class LimitedRetaliate2(LimitedRetaliate):
156164
retaliation limit of 15.
157165
"""
158166

167+
name = 'Limited Retaliate 2'
168+
159169
def __init__(self, retaliation_threshold=0.08, retaliation_limit=15):
160170
super(LimitedRetaliate2, self).__init__(
161171
retaliation_threshold=retaliation_threshold,
@@ -168,6 +178,8 @@ class LimitedRetaliate3(LimitedRetaliate):
168178
retaliation limit of 20.
169179
"""
170180

181+
name = 'Limited Retaliate 3'
182+
171183
def __init__(self, retaliation_threshold=0.05, retaliation_limit=20):
172184
super(LimitedRetaliate3, self).__init__(
173185
retaliation_threshold=retaliation_threshold,
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import unittest
2+
from axelrod import all_strategies
3+
4+
5+
class TestNames(unittest.TestCase):
6+
7+
def test_all_strategies_have_names(self):
8+
names = [s.name for s in all_strategies if s.name]
9+
self.assertEqual(len(names), len(all_strategies))
10+
11+
def test_all_names_are_unique(self):
12+
names = set(s.name for s in all_strategies)
13+
self.assertEqual(len(names), len(all_strategies))

axelrod/tests/unit/test_classification.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def test_known_classifiers(self):
1717

1818
for s in axelrod.all_strategies:
1919
s = s()
20-
self.assertTrue(None not in [s.classifier[key] for key in known_keys])
20+
self.assertTrue(
21+
None not in [s.classifier[key] for key in known_keys])
2122

2223
def test_multiple_instances(self):
2324
"""Certain instances of classes of strategies will have different
@@ -184,7 +185,8 @@ def test_meta_inclusion(self):
184185
self.assertTrue(axelrod.MetaMajority in axelrod.strategies)
185186

186187
self.assertTrue(axelrod.MetaHunter in axelrod.strategies)
187-
self.assertFalse(axelrod.MetaHunter in axelrod.long_run_time_strategies)
188+
self.assertFalse(
189+
axelrod.MetaHunter in axelrod.long_run_time_strategies)
188190

189191
def test_demo_strategies(self):
190192
demo_strategies = [axelrod.Cooperator,

axelrod/tests/unit/test_player.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
# -*- coding: utf-8 -*-
12
import copy
23
import inspect
34
import random
45
import unittest
56

67
import axelrod
7-
from axelrod import DefaultGame, Game, Player, simulate_play
8+
from axelrod import DefaultGame, Player, simulate_play
89

910

1011
C, D = axelrod.Actions.C, axelrod.Actions.D
@@ -13,6 +14,7 @@
1314
def cooperate(self):
1415
return C
1516

17+
1618
def defect(self):
1719
return D
1820

@@ -71,7 +73,9 @@ def test_noisy_play(self):
7173
self.assertEqual(p2.history[0], D)
7274

7375
def test_strategy(self):
74-
self.assertRaises(NotImplementedError, self.player().strategy, self.player())
76+
self.assertRaises(
77+
NotImplementedError, self.player().strategy, self.player())
78+
7579

7680
def test_responses(test_class, P1, P2, history_1, history_2, responses,
7781
random_seed=None, attrs=None):
@@ -128,8 +132,9 @@ def test_initialisation(self):
128132
if self.__class__ != TestPlayer:
129133
player = self.player()
130134
self.assertEqual(player.history, [])
131-
self.assertEqual(player.match_attributes,
132-
{'length': -1, 'game': DefaultGame, 'noise': 0})
135+
self.assertEqual(
136+
player.match_attributes,
137+
{'length': -1, 'game': DefaultGame, 'noise': 0})
133138
self.assertEqual(player.cooperations, 0)
134139
self.assertEqual(player.defections, 0)
135140
self.classifier_test(self.expected_class_classifier)
@@ -242,7 +247,6 @@ def responses_test(self, history_1, history_2, responses, random_seed=None,
242247
self, P1, P2, history_1, history_2, responses,
243248
random_seed=random_seed, attrs=attrs)
244249

245-
246250
def classifier_test(self, expected_class_classifier=None):
247251
"""Test that the keys in the expected_classifier dictionary give the
248252
expected values in the player classifier dictionary. Also checks that
@@ -261,10 +265,11 @@ def classifier_test(self, expected_class_classifier=None):
261265
self.assertTrue('stochastic' in player.classifier,
262266
msg="stochastic not in classifier")
263267
for key in TestOpponent.classifier:
264-
self.assertEqual(player.classifier[key],
265-
self.expected_classifier[key],
266-
msg="%s - Behaviour: %s != Expected Behaviour: %s" %
267-
(key, player.classifier[key], self.expected_classifier[key]))
268+
self.assertEqual(
269+
player.classifier[key],
270+
self.expected_classifier[key],
271+
msg="%s - Behaviour: %s != Expected Behaviour: %s" %
272+
(key, player.classifier[key], self.expected_classifier[key]))
268273

269274

270275
class TestHeadsUp(unittest.TestCase):

0 commit comments

Comments
 (0)