Skip to content

Commit 73f0440

Browse files
committed
removed flip_action. note 2 docstrings in strategy_transformers.py
1 parent 1947fb0 commit 73f0440

10 files changed

+21
-36
lines changed

axelrod/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .version import __version__
88
from .load_data_ import load_pso_tables, load_weights
99
from . import graph
10-
from .actions import Actions, flip_action
10+
from .actions import Actions
1111
from .random_ import random_choice, seed, Pdf
1212
from .plot import Plot
1313
from .game import DefaultGame, Game

axelrod/actions.py

-6
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,6 @@ def from_char(cls, character):
5555
Action = Actions
5656

5757

58-
def flip_action(action: Action) -> Action:
59-
if not isinstance(action, Action):
60-
raise UnknownActionError('Not an Action')
61-
return action.flip()
62-
63-
6458
def str_to_actions(actions: str) -> tuple:
6559
"""Takes a string like 'CCDD' and returns a tuple of the appropriate
6660
actions."""

axelrod/player.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import numpy as np
88

9-
from axelrod.actions import Actions, flip_action
9+
from axelrod.actions import Actions
1010
from .game import DefaultGame
1111

1212
import types
@@ -208,10 +208,10 @@ def __repr__(self):
208208
def _add_noise(noise, s1, s2):
209209
r = random.random()
210210
if r < noise:
211-
s1 = flip_action(s1)
211+
s1 = s1.flip()
212212
r = random.random()
213213
if r < noise:
214-
s2 = flip_action(s2)
214+
s2 = s2.flip()
215215
return s1, s2
216216

217217
def strategy(self, opponent):

axelrod/strategies/axelrod_first.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import random
66

7-
from axelrod.actions import Actions, flip_action, Action
7+
from axelrod.actions import Actions, Action
88
from axelrod.player import Player
99
from axelrod.random_ import random_choice
1010
from axelrod.strategy_transformers import FinalTransformer
@@ -124,7 +124,7 @@ def strategy(self, opponent: Player) -> Action:
124124
if (c >= 0 and c >= alt):
125125
move = C
126126
elif (c >= 0 and c < alt) or (alt >= 0):
127-
move = flip_action(self.history[-1])
127+
move = self.history[-1].flip()
128128
else:
129129
move = D
130130
return move

axelrod/strategies/axelrod_second.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import random
66

7-
from axelrod.actions import Actions, Action, flip_action
7+
from axelrod.actions import Actions, Action
88
from axelrod.player import Player
99
from axelrod.random_ import random_choice
1010

@@ -139,7 +139,7 @@ def strategy(self, opponent: Player) -> Action:
139139
if len(self.history) in [1, 2]:
140140
return C
141141
# Alternate C and D
142-
return flip_action(self.history[-1])
142+
return self.history[-1].flip()
143143

144144
def reset(self):
145145
super().reset()

axelrod/strategies/negation.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from axelrod.actions import Actions, flip_action, Action
1+
from axelrod.actions import Actions, Action
22
from axelrod.player import Player
33
from axelrod.random_ import random_choice
44

@@ -31,4 +31,4 @@ def strategy(self, opponent: Player) -> Action:
3131
if not self.history:
3232
return random_choice()
3333
# Act opposite of opponent otherwise
34-
return flip_action(opponent.history[-1])
34+
return opponent.history[-1].flip()

axelrod/strategy_transformers.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import inspect
1111
import random
1212
from numpy.random import choice
13-
from .actions import Actions, flip_action
13+
from .actions import Actions
1414
from .random_ import random_choice
1515

1616

@@ -200,8 +200,8 @@ def generic_strategy_wrapper(player, opponent, proposed_action, *args,
200200

201201

202202
def flip_wrapper(player, opponent, action):
203-
"""Applies flip_action at the class level."""
204-
return flip_action(action)
203+
"""Applies Action.flip() at the class level."""
204+
return action.flip()
205205

206206

207207
FlipTransformer = StrategyTransformerFactory(
@@ -233,17 +233,17 @@ def dual_wrapper(player, opponent, proposed_action):
233233

234234
action = player.original_player.strategy(opponent)
235235
player.original_player.history.append(action)
236-
return flip_action(action)
236+
return action.flip()
237237

238238

239239
DualTransformer = StrategyTransformerFactory(dual_wrapper, name_prefix="Dual")
240240

241241

242242
def noisy_wrapper(player, opponent, action, noise=0.05):
243-
"""Applies flip_action at the class level."""
243+
"""Applies Action.flip() at the class level."""
244244
r = random.random()
245245
if r < noise:
246-
return flip_action(action)
246+
return action.flip()
247247
return action
248248

249249
def noisy_reclassifier(original_classifier, noise):

axelrod/tests/strategies/test_calculator.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import axelrod
44
from .test_player import TestPlayer
5-
from axelrod.actions import flip_action
65
from axelrod._strategy_utils import detect_cycle
76
C, D = axelrod.Actions.C, axelrod.Actions.D
87

@@ -104,7 +103,7 @@ def get_joss_strategy_actions(opponent_moves: list, indices_to_flip: list) -> li
104103
if index == 0:
105104
out.append((C, action))
106105
elif index in indices_to_flip:
107-
out.append((flip_action(previous_action), action))
106+
out.append((previous_action.flip(), action))
108107
else:
109108
out.append((previous_action, action))
110109
return out

axelrod/tests/unit/test_actions.py

+1-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import unittest
2-
from axelrod import Actions, flip_action
2+
from axelrod import Actions
33
from axelrod.actions import str_to_actions, UnknownActionError, actions_to_str
44

55
C, D = Actions.C, Actions.D
@@ -39,13 +39,6 @@ def test_from_char_error(self):
3939
self.assertRaises(UnknownActionError, Actions.from_char, 'd')
4040
self.assertRaises(UnknownActionError, Actions.from_char, 'A')
4141

42-
def test_flip_action(self):
43-
self.assertEqual(flip_action(D), C)
44-
self.assertEqual(flip_action(C), D)
45-
46-
def test_flip_action_error(self):
47-
self.assertRaises(UnknownActionError, flip_action, 'R')
48-
4942
def test_str_to_actions(self):
5043
self.assertEqual(str_to_actions(''), ())
5144
self.assertEqual(str_to_actions('C'), (C, ))

axelrod/tests/unit/test_strategy_transformers.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import axelrod
44
from axelrod.strategy_transformers import *
5-
from axelrod.actions import flip_action
65
from axelrod.tests.strategies.test_titfortat import TestTitForTat
76
from axelrod.tests.strategies.test_cooperator import TestCooperator
87

@@ -102,7 +101,7 @@ def test_dual_wsls_transformer(self):
102101
for _ in range(10):
103102
p2.play(p3)
104103

105-
self.assertEqual(p1.history, [flip_action(x) for x in p2.history])
104+
self.assertEqual(p1.history, [x.flip() for x in p2.history])
106105

107106
def test_dual_tft_transformer(self):
108107
"""Tests that DualTransformer produces the opposite results when faced
@@ -119,7 +118,7 @@ def test_dual_tft_transformer(self):
119118
for _ in range(10):
120119
p2.play(p3)
121120

122-
self.assertEqual(p1.history, [flip_action(x) for x in p2.history])
121+
self.assertEqual(p1.history, [x.flip() for x in p2.history])
123122

124123
def test_dual_majority_transformer(self):
125124
"""Tests that DualTransformer produces the opposite results when faced
@@ -136,7 +135,7 @@ def test_dual_majority_transformer(self):
136135
for _ in range(10):
137136
p2.play(p3)
138137

139-
self.assertEqual(p1.history, [flip_action(x) for x in p2.history])
138+
self.assertEqual(p1.history, [x.flip() for x in p2.history])
140139

141140
def test_jossann_transformer(self):
142141
"""Tests the JossAnn transformer.

0 commit comments

Comments
 (0)