Skip to content

Commit 25514fc

Browse files
committed
Change axl_filename to take a pathlib.Path
1 parent 375a2fe commit 25514fc

File tree

9 files changed

+59
-53
lines changed

9 files changed

+59
-53
lines changed

axelrod/load_data_.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,21 @@
44
import pkg_resources
55

66

7-
def axl_filename(*axl_path: Text) -> Text:
7+
def axl_filename(path: pathlib.Path) -> pathlib.Path:
88
"""Given a path under Axelrod/, return absolute filepath.
99
1010
Parameters
1111
----------
1212
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").
13+
A pathlib.Path object with the relative directory under Axelrod/
1614
1715
Returns
1816
-------
19-
Absolute path to the given path.
17+
A pathlib.Path object with the absolute directory.
2018
"""
2119
# 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)
20+
axl_path = pathlib.Path(__file__).resolve().parent.parent
21+
return axl_path / path
2622

2723

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

axelrod/plot.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import matplotlib
55
import matplotlib.pyplot as plt
66
import matplotlib.transforms as transforms
7+
import pathlib
78
import tqdm
89
from numpy import arange, median, nan_to_num
910

@@ -324,7 +325,8 @@ def save_all_plots(
324325

325326
for method, name in plots:
326327
f = getattr(self, method)(title="{} - {}".format(title_prefix, name))
327-
f.savefig(axl_filename("{}_{}.{}".format(prefix, method, filetype)))
328+
path = pathlib.Path("{}_{}.{}".format(prefix, method, filetype))
329+
f.savefig(axl_filename(path))
328330
plt.close(f)
329331

330332
if progress_bar:

axelrod/tests/integration/test_tournament.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22

33
import filecmp
4+
import pathlib
45

56
import axelrod as axl
67
from axelrod.load_data_ import axl_filename
@@ -49,7 +50,8 @@ def setUpClass(cls):
4950
def test_big_tournaments(self, tournament):
5051
"""A test to check that tournament runs with a sample of non-cheating
5152
strategies."""
52-
filename = axl_filename("test_outputs", "test_tournament.csv")
53+
path = pathlib.Path("test_outputs/test_tournament.csv")
54+
filename = axl_filename(path)
5355
self.assertIsNone(
5456
tournament.play(progress_bar=False, filename=filename, build_results=False)
5557
)
@@ -94,9 +96,8 @@ def test_repeat_tournament_deterministic(self):
9496
turns=2,
9597
repetitions=2,
9698
)
97-
files.append(
98-
axl_filename("test_outputs", "stochastic_tournament_{}.csv".format(_))
99-
)
99+
path = pathlib.Path("test_outputs/stochastic_tournament_{}.csv".format(_))
100+
files.append(axl_filename(path))
100101
tournament.play(progress_bar=False, filename=files[-1], build_results=False)
101102
self.assertTrue(filecmp.cmp(files[0], files[1]))
102103

@@ -119,9 +120,8 @@ def test_repeat_tournament_stochastic(self):
119120
turns=2,
120121
repetitions=2,
121122
)
122-
files.append(
123-
axl_filename("test_outputs", "stochastic_tournament_{}.csv".format(_))
124-
)
123+
path = pathlib.Path("test_outputs/stochastic_tournament_{}.csv".format(_))
124+
files.append(axl_filename(path))
125125
tournament.play(progress_bar=False, filename=files[-1], build_results=False)
126126
self.assertTrue(filecmp.cmp(files[0], files[1]))
127127

axelrod/tests/unit/test_deterministic_cache.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import unittest
22
import os
3+
import pathlib
34
import pickle
45

56
import axelrod as axl
@@ -13,8 +14,10 @@ class TestDeterministicCache(unittest.TestCase):
1314
def setUpClass(cls):
1415
cls.test_key = (axl.TitForTat(), axl.Defector())
1516
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")
17+
save_path = pathlib.Path("test_outputs/test_cache_save.txt")
18+
cls.test_save_file = axl_filename(save_path)
19+
load_path = pathlib.Path("test_outputs/test_cache_load.txt")
20+
cls.test_load_file = axl_filename(load_path)
1821
test_data_to_pickle = {("Tit For Tat", "Defector"): [(C, D), (D, D), (D, D)]}
1922
cls.test_pickle = pickle.dumps(test_data_to_pickle)
2023

@@ -93,7 +96,8 @@ def test_load(self):
9396
self.assertEqual(self.cache[self.test_key], self.test_value)
9497

9598
def test_load_error_for_inccorect_format(self):
96-
filename = axl_filename("test_outputs", "test.cache")
99+
path = pathlib.Path("test_outputs/test.cache")
100+
filename = axl_filename(path)
97101
with open(filename, "wb") as io:
98102
pickle.dump(range(5), io)
99103

axelrod/tests/unit/test_fingerprint.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22
from unittest.mock import patch
33

44
import os
5-
65
from tempfile import mkstemp
7-
86
import matplotlib.pyplot
9-
107
import numpy as np
8+
import pathlib
119

1210
import axelrod as axl
1311
from axelrod.fingerprint import AshlockFingerprint, Point, TransitiveFingerprint
@@ -201,7 +199,8 @@ def test_temp_file_creation(self):
201199

202200
RecordedMksTemp.reset_record()
203201
af = AshlockFingerprint(axl.TitForTat)
204-
filename = axl_filename("test_outputs/test_fingerprint.csv")
202+
path = pathlib.Path("test_outputs/test_fingerprint.csv")
203+
filename = axl_filename(path)
205204

206205
self.assertEqual(RecordedMksTemp.record, [])
207206

@@ -217,7 +216,8 @@ def test_temp_file_creation(self):
217216
self.assertFalse(os.path.isfile(filename))
218217

219218
def test_fingerprint_with_filename(self):
220-
filename = axl_filename("test_outputs", "test_fingerprint.csv")
219+
path = pathlib.Path("test_outputs/test_fingerprint.csv")
220+
filename = axl_filename(path)
221221
af = AshlockFingerprint(axl.TitForTat)
222222
af.fingerprint(
223223
turns=1, repetitions=1, step=0.5, progress_bar=False, filename=filename
@@ -431,7 +431,8 @@ def test_init_with_not_default_number(self):
431431
)
432432

433433
def test_fingerprint_with_filename(self):
434-
filename = axl_filename("test_outputs", "test_fingerprint.csv")
434+
path = pathlib.Path("test_outputs/test_fingerprint.csv")
435+
filename = axl_filename(path)
435436
strategy = axl.TitForTat()
436437
tf = TransitiveFingerprint(strategy)
437438
tf.fingerprint(turns=1, repetitions=1, progress_bar=False, filename=filename)
@@ -442,10 +443,11 @@ def test_fingerprint_with_filename(self):
442443
def test_serial_fingerprint(self):
443444
strategy = axl.TitForTat()
444445
tf = TransitiveFingerprint(strategy)
446+
path = pathlib.Path("test_outputs/test_fingerprint.csv")
445447
tf.fingerprint(
446448
repetitions=1,
447449
progress_bar=False,
448-
filename=axl_filename("test_outputs", "tran_fin.csv"),
450+
filename=axl_filename(path),
449451
)
450452
self.assertEqual(tf.data.shape, (50, 50))
451453

@@ -458,7 +460,8 @@ def test_parallel_fingerprint(self):
458460

459461
def test_analyse_cooperation_ratio(self):
460462
tf = TransitiveFingerprint(axl.TitForTat)
461-
filename = axl_filename("test_outputs", "test_fingerprint.csv")
463+
path = pathlib.Path("test_outputs/test_fingerprint.csv")
464+
filename = axl_filename(path)
462465
with open(filename, "w") as f:
463466
f.write(
464467
"""Interaction index,Player index,Opponent index,Repetition,Player name,Opponent name,Actions

axelrod/tests/unit/test_load_data.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import os
2+
import pathlib
23
import unittest
34

45
from axelrod.load_data_ import axl_filename
56

67

78
class TestLoadData(unittest.TestCase):
89
def test_axl_filename(self):
9-
actual_fn = axl_filename("axelrod/strategies/titfortat.py")
10+
path = pathlib.Path("axelrod/strategies/titfortat.py")
11+
actual_fn = axl_filename(path)
1012

1113
# First go from "unit" up to "tests", then up to "axelrod"
1214
dirname = os.path.dirname(__file__)

axelrod/tests/unit/test_plot.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import tempfile
44
import matplotlib
55
import matplotlib.pyplot as plt
6+
import pathlib
67

78
from numpy import mean
89

@@ -13,7 +14,8 @@
1314
class TestPlot(unittest.TestCase):
1415
@classmethod
1516
def setUpClass(cls):
16-
cls.filename = axl_filename("test_outputs", "test_results.csv")
17+
path = pathlib.Path("test_outputs/test_results.csv")
18+
cls.filename = axl_filename(path)
1719

1820
cls.players = [axl.Alternator(), axl.TitForTat(), axl.Defector()]
1921
cls.repetitions = 3

axelrod/tests/unit/test_resultset.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import pandas as pd
55
from dask.dataframe.core import DataFrame
66
from numpy import mean, nanmedian, std
7+
import pathlib
78

89
import axelrod as axl
910
from axelrod.load_data_ import axl_filename
@@ -19,7 +20,8 @@ class TestResultSet(unittest.TestCase):
1920
@classmethod
2021
def setUpClass(cls):
2122

22-
cls.filename = axl_filename("test_outputs", "test_results.csv")
23+
path = pathlib.Path("test_outputs/test_results.csv")
24+
cls.filename = str(axl_filename(path))
2325

2426
cls.players = [axl.Alternator(), axl.TitForTat(), axl.Defector()]
2527
cls.repetitions = 3
@@ -677,7 +679,8 @@ class TestResultSetSpatialStructure(TestResultSet):
677679
@classmethod
678680
def setUpClass(cls):
679681

680-
cls.filename = axl_filename("test_outputs", "test_results_spatial.csv")
682+
path = pathlib.Path("test_outputs/test_results_spatial.csv")
683+
cls.filename = str(axl_filename(path))
681684
cls.players = [axl.Alternator(), axl.TitForTat(), axl.Defector()]
682685
cls.turns = 5
683686
cls.edges = [(0, 1), (0, 2)]
@@ -857,7 +860,8 @@ class TestResultSetSpatialStructureTwo(TestResultSetSpatialStructure):
857860
@classmethod
858861
def setUpClass(cls):
859862

860-
cls.filename = axl_filename("test_outputs", "test_results_spatial_two.csv")
863+
path = pathlib.Path("test_outputs/test_results_spatial_two.csv")
864+
cls.filename = str(axl_filename(path))
861865
cls.players = [
862866
axl.Alternator(),
863867
axl.TitForTat(),
@@ -1058,7 +1062,8 @@ class TestResultSetSpatialStructureThree(TestResultSetSpatialStructure):
10581062
@classmethod
10591063
def setUpClass(cls):
10601064

1061-
cls.filename = axl_filename("test_outputs", "test_results_spatial_three.csv")
1065+
path = pathlib.Path("test_outputs/test_results_spatial_three.csv")
1066+
cls.filename = str(axl_filename(path))
10621067
cls.players = [
10631068
axl.Alternator(),
10641069
axl.TitForTat(),

axelrod/tests/unit/test_tournament.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import io
88
import logging
99
import os
10+
import pathlib
1011
import pickle
1112
import warnings
1213
from multiprocessing import Queue, cpu_count
@@ -90,7 +91,8 @@ def setUpClass(cls):
9091
[200, 200, 1, 200, 200],
9192
]
9293

93-
cls.filename = axl_filename("test_outputs", "test_tournament.csv")
94+
path = pathlib.Path("test_outputs/test_tournament.csv")
95+
cls.filename = axl_filename(path)
9496

9597
def setUp(self):
9698
self.test_tournament = axl.Tournament(
@@ -110,9 +112,7 @@ def test_init(self):
110112
noise=0.2,
111113
)
112114
self.assertEqual(len(tournament.players), len(test_strategies))
113-
self.assertIsInstance(
114-
tournament.players[0].match_attributes["game"], axl.Game
115-
)
115+
self.assertIsInstance(tournament.players[0].match_attributes["game"], axl.Game)
116116
self.assertEqual(tournament.game.score((C, C)), (3, 3))
117117
self.assertEqual(tournament.turns, self.test_turns)
118118
self.assertEqual(tournament.repetitions, 10)
@@ -415,9 +415,7 @@ def test_progress_bar_play_parallel(self):
415415
# these two examples were identified by hypothesis.
416416
@example(
417417
tournament=axl.Tournament(
418-
players=[axl.BackStabber(), axl.MindReader()],
419-
turns=2,
420-
repetitions=1,
418+
players=[axl.BackStabber(), axl.MindReader()], turns=2, repetitions=1,
421419
)
422420
)
423421
@example(
@@ -735,9 +733,8 @@ def test_write_to_csv_with_results(self):
735733
)
736734
tournament.play(filename=self.filename, progress_bar=False)
737735
df = pd.read_csv(self.filename)
738-
expected_df = pd.read_csv(
739-
axl_filename("test_outputs", "expected_test_tournament.csv")
740-
)
736+
path = pathlib.Path("test_outputs/expected_test_tournament.csv")
737+
expected_df = pd.read_csv(axl_filename(path))
741738
self.assertTrue(df.equals(expected_df))
742739

743740
def test_write_to_csv_without_results(self):
@@ -750,9 +747,8 @@ def test_write_to_csv_without_results(self):
750747
)
751748
tournament.play(filename=self.filename, progress_bar=False, build_results=False)
752749
df = pd.read_csv(self.filename)
753-
expected_df = pd.read_csv(
754-
axl_filename("test_outputs", "expected_test_tournament_no_results.csv")
755-
)
750+
path = pathlib.Path("test_outputs/expected_test_tournament_no_results.csv")
751+
expected_df = pd.read_csv(axl_filename(path))
756752
self.assertTrue(df.equals(expected_df))
757753

758754

@@ -808,16 +804,12 @@ def test_init(self):
808804
# these two examples were identified by hypothesis.
809805
@example(
810806
tournament=axl.Tournament(
811-
players=[axl.BackStabber(), axl.MindReader()],
812-
prob_end=0.2,
813-
repetitions=1,
807+
players=[axl.BackStabber(), axl.MindReader()], prob_end=0.2, repetitions=1,
814808
)
815809
)
816810
@example(
817811
tournament=axl.Tournament(
818-
players=[axl.ThueMorse(), axl.MindReader()],
819-
prob_end=0.2,
820-
repetitions=1,
812+
players=[axl.ThueMorse(), axl.MindReader()], prob_end=0.2, repetitions=1,
821813
)
822814
)
823815
def test_property_serial_play(self, tournament):

0 commit comments

Comments
 (0)