Skip to content

Commit f5bad9f

Browse files
committed
Update axl_filename to use pathlib and take multiple arguments
1 parent 21a5a8f commit f5bad9f

File tree

8 files changed

+50
-32
lines changed

8 files changed

+50
-32
lines changed

axelrod/load_data_.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,28 @@
1+
import pathlib
12
from typing import Dict, List, Text, Tuple
23

3-
import os
44
import pkg_resources
55

66

7-
def axl_filename(axl_path: Text) -> Text:
8-
"""Get the path to Axelrod/<axl_path> from the working directory."""
9-
# Working directory
10-
dirname = os.path.dirname(__file__)
7+
def axl_filename(*axl_path: Text) -> Text:
8+
"""Given a path under Axelrod/, return absolute filepath.
119
12-
# We go up a dir because this code is located in Axelrod/axelrod and
13-
# axl_path is from the top-level Axelrod dir.
14-
return os.path.join(dirname, "..", axl_path)
10+
Parameters
11+
----------
12+
axl_path
13+
Path to file located under top-level Axelrod directory, with file names
14+
separated at "/". For example, to get the path to "Axelrod/xxx/yyy.py"
15+
call this function with axl_filename("xxx", "yyy.py").
16+
17+
Returns
18+
-------
19+
Absolute path to the given path.
20+
"""
21+
# We go up a dir because this code is located in Axelrod/axelrod.
22+
path = pathlib.Path(__file__).resolve().parent.parent
23+
for p in axl_path:
24+
path = path / p
25+
return str(path)
1526

1627

1728
def load_file(filename: str, directory: str) -> List[List[str]]:

axelrod/tests/integration/test_tournament.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,20 +33,22 @@ def setUpClass(cls):
3333
]
3434
cls.expected_outcome.sort()
3535

36-
@given(tournaments(
37-
strategies=axelrod.short_run_time_strategies,
38-
min_size=10,
39-
max_size=30,
40-
min_turns=2,
41-
max_turns=210,
42-
min_repetitions=1,
43-
max_repetitions=4,
44-
))
36+
@given(
37+
tournaments(
38+
strategies=axelrod.short_run_time_strategies,
39+
min_size=10,
40+
max_size=30,
41+
min_turns=2,
42+
max_turns=210,
43+
min_repetitions=1,
44+
max_repetitions=4,
45+
)
46+
)
4547
@settings(max_examples=1)
4648
def test_big_tournaments(self, tournament):
4749
"""A test to check that tournament runs with a sample of non-cheating
4850
strategies."""
49-
filename = axl_filename("test_outputs/test_tournament.csv")
51+
filename = axl_filename("test_outputs", "test_tournament.csv")
5052
self.assertIsNone(
5153
tournament.play(progress_bar=False, filename=filename, build_results=False)
5254
)
@@ -91,7 +93,9 @@ def test_repeat_tournament_deterministic(self):
9193
turns=2,
9294
repetitions=2,
9395
)
94-
files.append(axl_filename("test_outputs/stochastic_tournament_{}.csv".format(_)))
96+
files.append(
97+
axl_filename("test_outputs", "stochastic_tournament_{}.csv".format(_))
98+
)
9599
tournament.play(progress_bar=False, filename=files[-1], build_results=False)
96100
self.assertTrue(filecmp.cmp(files[0], files[1]))
97101

@@ -114,7 +118,9 @@ def test_repeat_tournament_stochastic(self):
114118
turns=2,
115119
repetitions=2,
116120
)
117-
files.append(axl_filename("test_outputs/stochastic_tournament_{}.csv".format(_)))
121+
files.append(
122+
axl_filename("test_outputs", "stochastic_tournament_{}.csv".format(_))
123+
)
118124
tournament.play(progress_bar=False, filename=files[-1], build_results=False)
119125
self.assertTrue(filecmp.cmp(files[0], files[1]))
120126

axelrod/tests/unit/test_deterministic_cache.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ class TestDeterministicCache(unittest.TestCase):
1313
def setUpClass(cls):
1414
cls.test_key = (TitForTat(), Defector())
1515
cls.test_value = [(C, D), (D, D), (D, D)]
16-
cls.test_save_file = axl_filename("test_outputs/test_cache_save.txt")
17-
cls.test_load_file = axl_filename("test_outputs/test_cache_load.txt")
16+
cls.test_save_file = axl_filename("test_outputs", "test_cache_save.txt")
17+
cls.test_load_file = axl_filename("test_outputs", "test_cache_load.txt")
1818
test_data_to_pickle = {("Tit For Tat", "Defector"): [(C, D), (D, D), (D, D)]}
1919
cls.test_pickle = pickle.dumps(test_data_to_pickle)
2020

@@ -93,7 +93,7 @@ def test_load(self):
9393
self.assertEqual(self.cache[self.test_key], self.test_value)
9494

9595
def test_load_error_for_inccorect_format(self):
96-
filename = axl_filename("test_outputs/test.cache")
96+
filename = axl_filename("test_outputs", "test.cache")
9797
with open(filename, "wb") as io:
9898
pickle.dump(range(5), io)
9999

axelrod/tests/unit/test_fingerprint.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ def test_temp_file_creation(self):
212212
self.assertFalse(os.path.isfile(filename))
213213

214214
def test_fingerprint_with_filename(self):
215-
filename = axl_filename("test_outputs/test_fingerprint.csv")
215+
filename = axl_filename("test_outputs", "test_fingerprint.csv")
216216
af = AshlockFingerprint(axl.TitForTat)
217217
af.fingerprint(
218218
turns=1, repetitions=1, step=0.5, progress_bar=False, filename=filename
@@ -426,7 +426,7 @@ def test_init_with_not_default_number(self):
426426
)
427427

428428
def test_fingerprint_with_filename(self):
429-
filename = axl_filename("test_outputs/test_fingerprint.csv")
429+
filename = axl_filename("test_outputs", "test_fingerprint.csv")
430430
strategy = axl.TitForTat()
431431
tf = TransitiveFingerprint(strategy)
432432
tf.fingerprint(turns=1, repetitions=1, progress_bar=False, filename=filename)
@@ -440,7 +440,7 @@ def test_serial_fingerprint(self):
440440
tf.fingerprint(
441441
repetitions=1,
442442
progress_bar=False,
443-
filename=axl_filename("test_outputs/tran_fin.csv"),
443+
filename=axl_filename("test_outputs", "tran_fin.csv"),
444444
)
445445
self.assertEqual(tf.data.shape, (50, 50))
446446

@@ -453,7 +453,7 @@ def test_parallel_fingerprint(self):
453453

454454
def test_analyse_cooperation_ratio(self):
455455
tf = TransitiveFingerprint(axl.TitForTat)
456-
filename = axl_filename("test_outputs/test_fingerprint.csv")
456+
filename = axl_filename("test_outputs", "test_fingerprint.csv")
457457
with open(filename, "w") as f:
458458
f.write(
459459
"""Interaction index,Player index,Opponent index,Repetition,Player name,Opponent name,Actions

axelrod/tests/unit/test_plot.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
class TestPlot(unittest.TestCase):
1212
@classmethod
1313
def setUpClass(cls):
14-
cls.filename = axl_filename("test_outputs/test_results.csv")
14+
cls.filename = axl_filename("test_outputs", "test_results.csv")
1515

1616
cls.players = [axelrod.Alternator(), axelrod.TitForTat(), axelrod.Defector()]
1717
cls.repetitions = 3

axelrod/tests/unit/test_resultset.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -648,7 +648,7 @@ class TestResultSetSpatialStructure(TestResultSet):
648648
@classmethod
649649
def setUpClass(cls):
650650

651-
cls.filename = axl_filename("test_outputs/test_results_spatial.csv")
651+
cls.filename = axl_filename("test_outputs", "test_results_spatial.csv")
652652
cls.players = [axelrod.Alternator(), axelrod.TitForTat(), axelrod.Defector()]
653653
cls.turns = 5
654654
cls.edges = [(0, 1), (0, 2)]

axelrod/tests/unit/test_tournament.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ def setUpClass(cls):
8989
[200, 200, 1, 200, 200],
9090
]
9191

92-
cls.filename = axl_filename("test_outputs/test_tournament.csv")
92+
cls.filename = axl_filename("test_outputs", "test_tournament.csv")
9393

9494
def setUp(self):
9595
self.test_tournament = axelrod.Tournament(
@@ -735,7 +735,7 @@ def test_write_to_csv_with_results(self):
735735
tournament.play(filename=self.filename, progress_bar=False)
736736
df = pd.read_csv(self.filename)
737737
expected_df = pd.read_csv(
738-
axl_filename("test_outputs/expected_test_tournament.csv")
738+
axl_filename("test_outputs", "expected_test_tournament.csv")
739739
)
740740
self.assertTrue(df.equals(expected_df))
741741

@@ -750,7 +750,7 @@ def test_write_to_csv_without_results(self):
750750
tournament.play(filename=self.filename, progress_bar=False, build_results=False)
751751
df = pd.read_csv(self.filename)
752752
expected_df = pd.read_csv(
753-
axl_filename("test_outputs/expected_test_tournament_no_results.csv")
753+
axl_filename("test_outputs", "expected_test_tournament_no_results.csv")
754754
)
755755
self.assertTrue(df.equals(expected_df))
756756

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ hypothesis==3.2
55
matplotlib>=2.0.0
66
numpy>=1.9.2
77
pandas>=0.18.1
8+
pathlib>=1.0.1
89
prompt-toolkit>=1.0.7
910
scipy>=0.19.0
1011
toolz>=0.8.0

0 commit comments

Comments
 (0)