Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add type hints to tournament #935

Merged
merged 9 commits into from
Mar 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions axelrod/tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
import tqdm

from axelrod import on_windows
from axelrod.player import Player
from .game import Game
from .match import Match
from .match_generator import RoundRobinMatches, ProbEndRoundRobinMatches, SpatialMatches, ProbEndSpatialMatches
from .match_generator import RoundRobinMatches, ProbEndRoundRobinMatches, SpatialMatches, ProbEndSpatialMatches, MatchGenerator
from .result_set import ResultSetFromFile, ResultSet

from typing import List

class Tournament(object):

def __init__(self, players, match_generator=RoundRobinMatches,
name='axelrod', game=None, turns=200, repetitions=10,
noise=0, with_morality=True):
def __init__(self, players: List[Player], match_generator: MatchGenerator=RoundRobinMatches,
name: str='axelrod', game: Game=None, turns: int=200, repetitions: int=10,
noise: float=0, with_morality: bool=True) -> None:
"""
Parameters
----------
Expand Down Expand Up @@ -70,9 +72,9 @@ def setup_output(self, filename=None, in_memory=False):
# Save filename for loading ResultSet later
self.filename = filename

def play(self, build_results=True, filename=None,
processes=None, progress_bar=True,
keep_interactions=False, in_memory=False):
def play(self, build_results: bool=True, filename: str=None,
processes: int=None, progress_bar: bool=True,
keep_interactions: bool=False, in_memory: bool=False) -> ResultSetFromFile:
"""
Plays the tournament and passes the results to the ResultSet class

Expand Down Expand Up @@ -130,8 +132,8 @@ def play(self, build_results=True, filename=None,
elif not in_memory:
self.outputfile.close()

def _build_result_set(self, progress_bar=True, keep_interactions=False,
in_memory=False):
def _build_result_set(self, progress_bar: bool=True, keep_interactions: bool=False,
in_memory: bool=False):
"""
Build the result set (used by the play method)

Expand All @@ -158,7 +160,7 @@ def _build_result_set(self, progress_bar=True, keep_interactions=False,
game=self.game)
return result_set

def _run_serial(self, progress_bar=False):
def _run_serial(self, progress_bar: bool=False) -> bool:
"""
Run all matches in serial

Expand Down Expand Up @@ -210,7 +212,7 @@ def _write_interactions_to_dict(self, results):
self.interactions_dict[index_pair] = [interaction]
self.num_interactions += 1

def _run_parallel(self, processes=2, progress_bar=False):
def _run_parallel(self, processes: int=2, progress_bar: bool=False) -> bool:
"""
Run all matches in parallel

Expand All @@ -236,7 +238,7 @@ def _run_parallel(self, processes=2, progress_bar=False):

return True

def _n_workers(self, processes=2):
def _n_workers(self, processes: int=2) -> int:
"""
Determines the number of parallel processes to use.

Expand All @@ -250,7 +252,7 @@ def _n_workers(self, processes=2):
n_workers = cpu_count()
return n_workers

def _start_workers(self, workers, work_queue, done_queue):
def _start_workers(self, workers: int, work_queue: Queue, done_queue: Queue) -> bool:
"""
Initiates the sub-processes to carry out parallel processing.

Expand All @@ -270,7 +272,7 @@ def _start_workers(self, workers, work_queue, done_queue):
process.start()
return True

def _process_done_queue(self, workers, done_queue, progress_bar=False):
def _process_done_queue(self, workers: int, done_queue: Queue, progress_bar: bool=False):
"""
Retrieves the matches from the parallel sub-processes

Expand All @@ -296,7 +298,7 @@ def _process_done_queue(self, workers, done_queue, progress_bar=False):
self.progress_bar.update(1)
return True

def _worker(self, work_queue, done_queue):
def _worker(self, work_queue: Queue, done_queue: Queue):
"""
The work for each parallel sub-process to execute.

Expand Down Expand Up @@ -385,8 +387,8 @@ class SpatialTournament(Tournament):
A tournament in which the players are allocated in a graph as nodes
and they players only play the others that are connected to with an edge.
"""
def __init__(self, players, edges, name='axelrod', game=None, turns=200,
repetitions=10, noise=0, with_morality=True):
def __init__(self, players: List[Player], edges, name: str='axelrod', game: Game=None, turns: int=200,
repetitions: int=10, noise: float=0, with_morality: bool=True) -> None:
"""
Parameters
----------
Expand Down Expand Up @@ -420,8 +422,8 @@ class ProbEndSpatialTournament(ProbEndTournament):
and they players only play the others that are connected to with an edge.
Players do not know the length of a given match (it is randomly sampled).
"""
def __init__(self, players, edges, name='axelrod', game=None, prob_end=.5,
repetitions=10, noise=0, with_morality=True):
def __init__(self, players: List[Player], edges, name: str='axelrod', game: Game=None, prob_end: float=.5,
repetitions: int=10, noise: float=0, with_morality: bool=True) -> None:
"""
Parameters
----------
Expand Down
1 change: 1 addition & 0 deletions run_mypy.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"axelrod/mock_player.py",
"axelrod/moran.py",
"axelrod/plot.py",
"axelrod/tournament.py",
"axelrod/strategies/adaptive.py",
"axelrod/strategies/alternator.py",
"axelrod/strategies/ann.py",
Expand Down