Skip to content

Commit eddbc71

Browse files
jsafyandrvinceknight
authored andcommitted
Define a total order for Action (#1199)
* Add @total_ordering to Action Enum to enable sorting. * Fix ordering * Add __hash__ to Action * Fix test for sequence player
1 parent 819e7cc commit eddbc71

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

Diff for: axelrod/action.py

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"""
88

99
from enum import Enum
10+
from functools import total_ordering
1011
from typing import Iterable
1112

1213

@@ -17,6 +18,7 @@ def __init__(self, *args):
1718
super(UnknownActionError, self).__init__(*args)
1819

1920

21+
@total_ordering
2022
class Action(Enum):
2123
"""Core actions in the Prisoner's Dilemna.
2224
@@ -30,6 +32,15 @@ class Action(Enum):
3032
def __bool__(self):
3133
return bool(self.value)
3234

35+
def __eq__(self, other):
36+
return self.value == other.value
37+
38+
def __hash__(self):
39+
return hash(self.value)
40+
41+
def __lt__(self, other):
42+
return self.value < other.value
43+
3344
def __repr__(self):
3445
return "{}".format(self.name)
3546

Diff for: axelrod/tests/strategies/test_sequence_player.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ def test_sequence_player(self):
2222
"""Basic test for SequencePlayer."""
2323

2424
def cooperate_gen():
25-
yield C
26-
25+
yield 1
2726
player = SequencePlayer(generator_function=cooperate_gen)
2827
opponent = TestOpponent()
2928
self.assertEqual(C, player.strategy(opponent))

Diff for: axelrod/tests/unit/test_actions.py

+11
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77

88

99
class TestAction(unittest.TestCase):
10+
def test_eq(self):
11+
self.assertEqual(C, C)
12+
self.assertEqual(D, D)
13+
14+
def test_hash(self):
15+
self.assertEqual(hash(C), 1)
16+
self.assertEqual(hash(D), 0)
17+
18+
def test_lt(self):
19+
self.assertLess(D, C)
20+
1021
def test_repr(self):
1122
self.assertEqual(repr(C), "C")
1223
self.assertEqual(repr(D), "D")

0 commit comments

Comments
 (0)