Skip to content

Commit 8b659a6

Browse files
authored
Merge pull request #1456 from gaffney2010/match_generator
Create MatchChunk dataclass
2 parents 75f6e77 + d0e9984 commit 8b659a6

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

axelrod/match_generator.py

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,21 @@
1+
from dataclasses import dataclass
2+
from typing import Any, Dict, Iterator, Tuple
3+
14
from axelrod.random_ import BulkRandomGenerator
25

36

7+
@dataclass
8+
class MatchChunk(object):
9+
index_pair: Tuple[int]
10+
match_params: Dict[str, Any]
11+
repetitions: int
12+
seed: BulkRandomGenerator
13+
14+
def as_tuple(self) -> Tuple:
15+
"""Kept for legacy reasons"""
16+
return (self.index_pair, self.match_params, self.repetitions, self.seed)
17+
18+
419
class MatchGenerator(object):
520
def __init__(
621
self,
@@ -62,7 +77,7 @@ def __init__(
6277
def __len__(self):
6378
return self.size
6479

65-
def build_match_chunks(self):
80+
def build_match_chunks(self) -> Iterator[MatchChunk]:
6681
"""
6782
A generator that returns player index pairs and match parameters for a
6883
round robin tournament.
@@ -80,7 +95,12 @@ def build_match_chunks(self):
8095
for index_pair in edges:
8196
match_params = self.build_single_match_params()
8297
r = next(self.random_generator)
83-
yield (index_pair, match_params, self.repetitions, r)
98+
yield MatchChunk(
99+
index_pair=index_pair,
100+
match_params=match_params,
101+
repetitions=self.repetitions,
102+
seed=r,
103+
)
84104

85105
def build_single_match_params(self):
86106
"""

axelrod/tests/unit/test_match_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def test_build_match_chunks(self, repetitions):
171171
game=test_game,
172172
repetitions=repetitions,
173173
)
174-
chunks = list(rr.build_match_chunks())
174+
chunks = [chunk.as_tuple() for chunk in rr.build_match_chunks()]
175175
match_definitions = [
176176
tuple(list(index_pair) + [repetitions])
177177
for (index_pair, match_params, repetitions, _) in chunks
@@ -239,7 +239,7 @@ def test_spatial_build_match_chunks(self, repetitions):
239239
edges=cycle,
240240
repetitions=repetitions,
241241
)
242-
chunks = list(rr.build_match_chunks())
242+
chunks = [chunk.as_tuple() for chunk in rr.build_match_chunks()]
243243
match_definitions = [
244244
tuple(list(index_pair) + [repetitions])
245245
for (index_pair, match_params, repetitions, _) in chunks

axelrod/tests/unit/test_tournament.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
strategy_lists,
2525
tournaments,
2626
)
27-
from axelrod.tournament import _close_objects
27+
from axelrod.tournament import MatchChunk, _close_objects
2828

2929
C, D = axl.Action.C, axl.Action.D
3030

@@ -663,7 +663,9 @@ def make_chunk_generator():
663663
for player2_index in range(player1_index, len(self.players)):
664664
index_pair = (player1_index, player2_index)
665665
match_params = {"turns": turns, "game": self.game}
666-
yield (index_pair, match_params, self.test_repetitions, 0)
666+
yield MatchChunk(
667+
index_pair, match_params, self.test_repetitions, 0
668+
)
667669

668670
chunk_generator = make_chunk_generator()
669671
interactions = {}

axelrod/tournament.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
from .game import Game
1818
from .match import Match
19-
from .match_generator import MatchGenerator
19+
from .match_generator import MatchChunk, MatchGenerator
2020
from .result_set import ResultSet
2121

2222
C, D = Action.C, Action.D
@@ -427,7 +427,7 @@ def _worker(
427427
done_queue.put("STOP")
428428
return True
429429

430-
def _play_matches(self, chunk, build_results=True):
430+
def _play_matches(self, chunk: Match, build_results: bool = True):
431431
"""
432432
Play matches in a given chunk.
433433
@@ -446,22 +446,21 @@ def _play_matches(self, chunk, build_results=True):
446446
(0, 1) -> [(C, D), (D, C),...]
447447
"""
448448
interactions = defaultdict(list)
449-
index_pair, match_params, repetitions, seed = chunk
450-
p1_index, p2_index = index_pair
449+
p1_index, p2_index = chunk.index_pair
451450
player1 = self.players[p1_index].clone()
452451
player2 = self.players[p2_index].clone()
453-
match_params["players"] = (player1, player2)
454-
match_params["seed"] = seed
455-
match = Match(**match_params)
456-
for _ in range(repetitions):
452+
chunk.match_params["players"] = (player1, player2)
453+
chunk.match_params["seed"] = chunk.seed
454+
match = Match(**chunk.match_params)
455+
for _ in range(chunk.repetitions):
457456
match.play()
458457

459458
if build_results:
460459
results = self._calculate_results(match.result)
461460
else:
462461
results = None
463462

464-
interactions[index_pair].append([match.result, results])
463+
interactions[chunk.index_pair].append([match.result, results])
465464
return interactions
466465

467466
def _calculate_results(self, interactions):

0 commit comments

Comments
 (0)