Skip to content

Commit 5186d3f

Browse files
authored
Merge pull request #1223 from danilobellini/human
Make Human work w/ both prompt_toolkit v1 and v2
2 parents 2c92125 + adf228d commit 5186d3f

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

axelrod/strategies/human.py

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,27 @@
33
from axelrod.action import Action
44
from axelrod.player import Player
55
from prompt_toolkit import prompt
6-
from prompt_toolkit.styles import style_from_dict
7-
from prompt_toolkit.token import Token
86
from prompt_toolkit.validation import ValidationError, Validator
97

8+
try: # pragma: no cover
9+
from prompt_toolkit.styles import style_from_dict
10+
from prompt_toolkit.token import Token
11+
12+
token_toolbar = Token.Toolbar
13+
bottom_toolbar_name = "get_bottom_toolbar_tokens"
14+
PROMPT2 = False
15+
16+
except ImportError: # prompt_toolkit v2
17+
from prompt_toolkit.styles import Style
18+
19+
style_from_dict = Style.from_dict
20+
token_toolbar = "pygments.toolbar"
21+
bottom_toolbar_name = "bottom_toolbar"
22+
PROMPT2 = True
23+
1024
C, D = Action.C, Action.D
1125

12-
toolbar_style = style_from_dict({Token.Toolbar: "#ffffff bg:#333333"})
26+
toolbar_style = style_from_dict({token_toolbar: "#ffffff bg:#333333"})
1327

1428

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

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

8296
def _status_messages(self):
8397
"""
@@ -95,7 +109,11 @@ def _status_messages(self):
95109
mapping print or toolbar to the relevant string
96110
"""
97111
if self.history:
98-
toolbar = self._history_toolbar
112+
toolbar = (
113+
self._history_toolbar
114+
if PROMPT2
115+
else lambda cli: [(token_toolbar, self._history_toolbar())]
116+
)
99117
print_statement = "{}Turn {}: {} played {}, opponent played {}".format(
100118
linesep,
101119
len(self.history),
@@ -124,8 +142,8 @@ def _get_human_input(self) -> Action: # pragma: no cover
124142
len(self.history) + 1, self.human_name
125143
),
126144
validator=ActionValidator(),
127-
get_bottom_toolbar_tokens=self.status_messages["toolbar"],
128145
style=toolbar_style,
146+
**{bottom_toolbar_name: self.status_messages["toolbar"]},
129147
)
130148

131149
return Action.from_char(action.upper())

axelrod/tests/strategies/test_human.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,13 @@ def test_init(self):
5252
def test_history_toolbar(self):
5353
human = Human()
5454
expected_content = ""
55-
actual_content = human._history_toolbar(None)[0][1]
55+
actual_content = human._history_toolbar()
5656
self.assertEqual(actual_content, expected_content)
5757

5858
human.history = [C]
5959
human.opponent_history = [C]
6060
expected_content = "History (human, opponent): [('C', 'C')]"
61-
actual_content = human._history_toolbar(None)[0][1]
61+
actual_content = human._history_toolbar()
6262
self.assertIn(actual_content, expected_content)
6363

6464
def test_status_messages(self):

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ hypothesis==3.2
44
matplotlib>=2.0.0,<3.0.0
55
numpy>=1.9.2
66
pandas>=0.18.1
7-
prompt-toolkit>=1.0.7,<2.0.0
7+
prompt-toolkit>=1.0.7
88
scipy>=0.19.0
99
toolz>=0.8.0
1010
tqdm>=3.4.0

0 commit comments

Comments
 (0)