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

Make Human work w/ both prompt_toolkit v1 and v2 #1223

Merged
merged 2 commits into from
Nov 20, 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
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
Copy link
Member

Choose a reason for hiding this comment

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

This breaks the system install of ipython which is not yet compatible with prompt-toolkit>2.0.0. #1183

Once ipython is using the latest prompt-toolkit we will migrate to just use 2.0.0+.

Copy link
Member

Choose a reason for hiding this comment

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

See also: #1217

Copy link
Member Author

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, the oldest IPython that required prompt_toolkit v1 was the 6.5.0, and the marked line is compatible with it (actually, it's compatible with both versions). /~https://github.com/ipython/ipython/blob/6.5.0/setup.py#L193

Copy link
Member

@drvinceknight drvinceknight Nov 20, 2018

Choose a reason for hiding this comment

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

Perhaps my diagnosis of the source of the issue of #1217 and the linked discussions on #1183 isn't correct. Nonetheless the fix in #1212 is preferable as it'll move us to just using prompt_toolkit v2 but any fix needs to not break the common data science tools like jupyter and ipython and (as of 3 minutes ago): updating to the conda distribution of Jupyter currently keeps ipython at 6.5.

scipy>=0.19.0
toolz>=0.8.0
tqdm>=3.4.0