Skip to content

Commit

Permalink
Merge pull request #1223 from danilobellini/human
Browse files Browse the repository at this point in the history
Make Human work w/ both prompt_toolkit v1 and v2
  • Loading branch information
meatballs committed Nov 20, 2018
2 parents 2c92125 + adf228d commit 5186d3f
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
32 changes: 25 additions & 7 deletions axelrod/strategies/human.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,27 @@
from axelrod.action import Action
from axelrod.player import Player
from prompt_toolkit import prompt
from prompt_toolkit.styles import style_from_dict
from prompt_toolkit.token import Token
from prompt_toolkit.validation import ValidationError, Validator

try: # pragma: no cover
from prompt_toolkit.styles import style_from_dict
from prompt_toolkit.token import Token

token_toolbar = Token.Toolbar
bottom_toolbar_name = "get_bottom_toolbar_tokens"
PROMPT2 = False

except ImportError: # prompt_toolkit v2
from prompt_toolkit.styles import Style

style_from_dict = Style.from_dict
token_toolbar = "pygments.toolbar"
bottom_toolbar_name = "bottom_toolbar"
PROMPT2 = True

C, D = Action.C, Action.D

toolbar_style = style_from_dict({Token.Toolbar: "#ffffff bg:#333333"})
toolbar_style = style_from_dict({token_toolbar: "#ffffff bg:#333333"})


class ActionValidator(Validator):
Expand Down Expand Up @@ -65,7 +79,7 @@ def __init__(self, name="human", c_symbol="C", d_symbol="D"):
self.symbols = {C: c_symbol, D: d_symbol}
self.opponent_history = []

def _history_toolbar(self, cli):
def _history_toolbar(self):
"""
A prompt-toolkit function to define the bottom toolbar.
Described at http://python-prompt-toolkit.readthedocs.io/en/latest/pages/building_prompts.html#adding-a-bottom-toolbar
Expand All @@ -77,7 +91,7 @@ def _history_toolbar(self, cli):
content = "History ({}, opponent): {}".format(self.human_name, history)
else:
content = ""
return [(Token.Toolbar, content)]
return content

def _status_messages(self):
"""
Expand All @@ -95,7 +109,11 @@ def _status_messages(self):
mapping print or toolbar to the relevant string
"""
if self.history:
toolbar = self._history_toolbar
toolbar = (
self._history_toolbar
if PROMPT2
else lambda cli: [(token_toolbar, self._history_toolbar())]
)
print_statement = "{}Turn {}: {} played {}, opponent played {}".format(
linesep,
len(self.history),
Expand Down Expand Up @@ -124,8 +142,8 @@ def _get_human_input(self) -> Action: # pragma: no cover
len(self.history) + 1, self.human_name
),
validator=ActionValidator(),
get_bottom_toolbar_tokens=self.status_messages["toolbar"],
style=toolbar_style,
**{bottom_toolbar_name: self.status_messages["toolbar"]},
)

return Action.from_char(action.upper())
Expand Down
4 changes: 2 additions & 2 deletions axelrod/tests/strategies/test_human.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ def test_init(self):
def test_history_toolbar(self):
human = Human()
expected_content = ""
actual_content = human._history_toolbar(None)[0][1]
actual_content = human._history_toolbar()
self.assertEqual(actual_content, expected_content)

human.history = [C]
human.opponent_history = [C]
expected_content = "History (human, opponent): [('C', 'C')]"
actual_content = human._history_toolbar(None)[0][1]
actual_content = human._history_toolbar()
self.assertIn(actual_content, expected_content)

def test_status_messages(self):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ hypothesis==3.2
matplotlib>=2.0.0,<3.0.0
numpy>=1.9.2
pandas>=0.18.1
prompt-toolkit>=1.0.7,<2.0.0
prompt-toolkit>=1.0.7
scipy>=0.19.0
toolz>=0.8.0
tqdm>=3.4.0

0 comments on commit 5186d3f

Please sign in to comment.