Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define a total order for Action #1199

Merged
merged 6 commits into from
Aug 29, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions axelrod/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

from enum import Enum
from functools import total_ordering
from typing import Iterable


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


@total_ordering
class Action(Enum):
"""Core actions in the Prisoner's Dilemna.

Expand All @@ -30,6 +32,15 @@ class Action(Enum):
def __bool__(self):
return bool(self.value)

def __eq__(self, other):
return self.value == other.value

def __hash__(self):
return hash(self.value)

def __lt__(self, other):
return self.value < other.value

def __repr__(self):
return "{}".format(self.name)

Expand Down
3 changes: 1 addition & 2 deletions axelrod/tests/strategies/test_sequence_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,7 @@ def test_sequence_player(self):
"""Basic test for SequencePlayer."""

def cooperate_gen():
yield C

yield 1
player = SequencePlayer(generator_function=cooperate_gen)
opponent = TestOpponent()
self.assertEqual(C, player.strategy(opponent))
Expand Down
11 changes: 11 additions & 0 deletions axelrod/tests/unit/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@


class TestAction(unittest.TestCase):
def test_eq(self):
self.assertEqual(C, C)
self.assertEqual(D, D)

def test_hash(self):
self.assertEqual(hash(C), 1)
self.assertEqual(hash(D), 0)

def test_lt(self):
self.assertLess(D, C)

def test_repr(self):
self.assertEqual(repr(C), "C")
self.assertEqual(repr(D), "D")
Expand Down