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

Spruce up actions module #1193

Merged
merged 1 commit into from
Aug 15, 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
72 changes: 54 additions & 18 deletions axelrod/action.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
"""
Defines the core actions for the Prisoner's Dilemma:
* Cooperate
* Defect
"""Actions for the Prisoner's Dilemma and related utilities.

Uses the enumeration, Action.C and Action.D. For convenience you can use:
For convenience in other modules you can alias the actions:

from axelrod import Action
C, D = Action.C, Action.D
Expand All @@ -14,14 +11,20 @@


class UnknownActionError(ValueError):
"""Error indicating an unknown action was used."""
def __init__(self, *args):
super(UnknownActionError, self).__init__(*args)


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

There are only two possible actions, namely Cooperate or Defect,
which are called C and D for convenience.
"""

C = 1
D = 0
C = 1 # Cooperate
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need these two inline comments?

D = 0 # Defect

def __bool__(self):
return bool(self.value)
Expand All @@ -33,32 +36,65 @@ def __str__(self):
return '{}'.format(self.name)

def flip(self):
"""Returns the opposite Action. """
"""Returns the opposite Action."""
if self == Action.C:
return Action.D
if self == Action.D:
return Action.C

@classmethod
def from_char(cls, character):
"""Converts a single character into an Action. `Action.from_char('C')`
returns `Action.C`. `Action.from_char('CC')` raises an error. Use
`str_to_actions` instead."""
"""Converts a single character into an Action.

Parameters
----------
character: a string of length one

Returns
-------
Action
The action corresponding to the input character


Raises
------
UnknownActionError
If the input string is not 'C' or 'D'
"""
if character == 'C':
return cls.C
elif character == 'D':
if character == 'D':
return cls.D
else:
raise UnknownActionError('Character must be "C" or "D".')
raise UnknownActionError('Character must be "C" or "D".')


def str_to_actions(actions: str) -> tuple:
"""Takes a string like 'CCDD' and returns a tuple of the appropriate
actions."""
"""Converts a string to a tuple of actions.

Parameters
----------
actions: string consisting of 'C's and 'D's

Returns
-------
tuple
Each element corresponds to a letter from the input string.
"""
return tuple(Action.from_char(element) for element in actions)


def actions_to_str(actions: Iterable[Action]) -> str:
"""Takes any iterable of Action and returns a string of 'C's
and 'D's. ex: (D, D, C) -> 'DDC' """
"""Converts an iterable of actions into a string.

Example: (D, D, C) would be converted to 'DDC'

Paramteters
-----------
actions: iterable of Action

Returns
-------
str
A string of 'C's and 'D's.
"""
return "".join(map(repr, actions))
2 changes: 2 additions & 0 deletions axelrod/tests/unit/test_actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def test_from_char_error(self):
self.assertRaises(UnknownActionError, Action.from_char, 'c')
self.assertRaises(UnknownActionError, Action.from_char, 'd')
self.assertRaises(UnknownActionError, Action.from_char, 'A')
self.assertRaises(UnknownActionError, Action.from_char, 'CC')

def test_str_to_actions(self):
self.assertEqual(str_to_actions(''), ())
Expand All @@ -49,6 +50,7 @@ def test_str_to_actions_fails_fast_and_raises_value_error(self):

def test_actions_to_str(self):
self.assertEqual(actions_to_str([]), "")
self.assertEqual(actions_to_str([C]), "C")
self.assertEqual(actions_to_str([C, D, C]), "CDC")
self.assertEqual(actions_to_str((C, C, D)), "CCD")

Expand Down