|
3 | 3 | import csv |
4 | 4 | from collections import defaultdict |
5 | 5 | import logging |
6 | | -from tempfile import NamedTemporaryFile |
7 | | - |
8 | 6 | from multiprocessing import Process, Queue, cpu_count |
9 | | - |
| 7 | +from tempfile import NamedTemporaryFile |
| 8 | +import warnings |
10 | 9 |
|
11 | 10 | from .game import Game |
12 | | -from .result_set import ResultSet, ResultSetFromFile |
13 | | -from .match_generator import RoundRobinMatches, ProbEndRoundRobinMatches |
14 | 11 | from .match import Match |
| 12 | +from .match_generator import RoundRobinMatches, ProbEndRoundRobinMatches |
| 13 | +from .result_set import ResultSet, ResultSetFromFile |
| 14 | + |
15 | 15 |
|
16 | 16 | class Tournament(object): |
17 | 17 | game = Game() |
18 | 18 |
|
19 | 19 | def __init__(self, players, match_generator=RoundRobinMatches, |
20 | 20 | name='axelrod', game=None, turns=200, repetitions=10, |
21 | | - processes=None, noise=0, with_morality=True, filename=None): |
| 21 | + processes=None, noise=0, with_morality=True): |
22 | 22 | """ |
23 | 23 | Parameters |
24 | 24 | ---------- |
@@ -51,34 +51,41 @@ def __init__(self, players, match_generator=RoundRobinMatches, |
51 | 51 | self.match_generator = match_generator(players, turns, self.game, |
52 | 52 | self.repetitions) |
53 | 53 | self._with_morality = with_morality |
54 | | - self._parallel_repetitions = repetitions |
55 | 54 | self._processes = processes |
56 | 55 | self._logger = logging.getLogger(__name__) |
57 | | - self.interactions = defaultdict(list) |
58 | 56 |
|
59 | | - if not filename: |
| 57 | + def setup_output_file(self, filename=None): |
| 58 | + """Open a CSV writer for tournament output.""" |
| 59 | + if filename: |
| 60 | + self.outputfile = open(filename, 'a') |
| 61 | + else: |
| 62 | + # Setup a temporary file |
60 | 63 | self.outputfile = NamedTemporaryFile(mode='w') |
61 | 64 | filename = self.outputfile.name |
62 | | - else: |
63 | | - self.outputfile = open(filename, 'w') |
64 | 65 | self.writer = csv.writer(self.outputfile) |
| 66 | + # Save filename for loading ResultSet later |
65 | 67 | self.filename = filename |
66 | 68 |
|
67 | | - def play(self): |
| 69 | + def play(self, build_results=True, filename=None): |
68 | 70 | """ |
69 | 71 | Plays the tournament and passes the results to the ResultSet class |
70 | 72 |
|
71 | 73 | Returns |
72 | 74 | ------- |
73 | 75 | axelrod.ResultSet |
74 | 76 | """ |
| 77 | + self.setup_output_file(filename) |
| 78 | + if build_results and not filename: |
| 79 | + warnings.warn("Tournament results will not be accessible since build_results=False and no filename was supplied.") |
| 80 | + pass |
75 | 81 | if self._processes is None: |
76 | 82 | self._run_serial() |
77 | 83 | else: |
78 | 84 | self._run_parallel() |
79 | 85 | # Make sure that python has finished writing to disk |
80 | 86 | self.outputfile.flush() |
81 | | - return self._build_result_set() |
| 87 | + if build_results: |
| 88 | + return self._build_result_set() |
82 | 89 |
|
83 | 90 | def _build_result_set(self): |
84 | 91 | """ |
|
0 commit comments