Skip to content

Commit

Permalink
Fix PEP8 errors and redundant imports
Browse files Browse the repository at this point in the history
  • Loading branch information
meatballs committed May 31, 2016
1 parent 7b8b470 commit 5148e64
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion axelrod/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ def flip_action(action):
elif action == Actions.D:
return Actions.C
else:
raise ValueError("Encountered a invalid action")
raise ValueError("Encountered a invalid action")
1 change: 1 addition & 0 deletions axelrod/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

C, D = Actions.C, Actions.D


class Game(object):
"""A class to hold the game matrix and to score a game accordingly."""

Expand Down
2 changes: 1 addition & 1 deletion axelrod/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def _cache_update_required(self):
not self.noise and
self._cache.mutable and not (
any(p.classifier['stochastic'] for p in self.players)
)
)
)

def play(self):
Expand Down
2 changes: 0 additions & 2 deletions axelrod/match_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
from math import ceil, log
import random

from .match import Match


class MatchGenerator(object):

Expand Down
3 changes: 0 additions & 3 deletions axelrod/mock_player.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import copy

import axelrod

from axelrod import Player, update_history, Actions

C, D = Actions.C, Actions.D
Expand Down
10 changes: 5 additions & 5 deletions axelrod/moran.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from .deterministic_cache import DeterministicCache
from .match import Match, is_stochastic
from .player import Player
from .random_ import randrange


Expand All @@ -30,11 +29,12 @@ def fitness_proportionate_selection(scores):
if x >= r:
return i


class MoranProcess(object):
def __init__(self, players, turns=100, noise=0, deterministic_cache=None):
self.turns = turns
self.noise = noise
self.initial_players = players # save initial population
self.initial_players = players # save initial population
self.players = []
self.populations = []
self.set_players()
Expand Down Expand Up @@ -70,7 +70,6 @@ def __next__(self):
- update the population
"""
# Check the exit condition, that all players are of the same type.
population = self.populations[-1]
classes = set(p.__class__ for p in self.players)
if len(classes) == 1:
self.winning_strategy_name = str(self.players[0])
Expand All @@ -94,8 +93,9 @@ def _play_next_round(self):
for j in range(i + 1, N):
player1 = self.players[i]
player2 = self.players[j]
match = Match((player1, player2), turns=self.turns, noise=self.noise,
deterministic_cache=self.deterministic_cache)
match = Match(
(player1, player2), turns=self.turns, noise=self.noise,
deterministic_cache=self.deterministic_cache)
match.play()
match_scores = np.sum(match.scores(), axis=0) / float(self.turns)
scores[i] += match_scores[0]
Expand Down
11 changes: 7 additions & 4 deletions axelrod/player.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from functools import wraps
import inspect
import random
import copy

Expand All @@ -23,14 +22,17 @@ def is_basic(s):
manipulates_state = s.classifier['manipulates_state']
return (not stochastic) and (not inspects_source) and (not manipulates_source) and (not manipulates_state) and (depth in (0, 1))


def obey_axelrod(s):
"""
A function to check if a strategy obeys Axelrod's original tournament rules.
"""
classifier = s.classifier
return not (classifier['inspects_source'] or\
classifier['manipulates_source'] or\
classifier['manipulates_state'])
return not (
classifier['inspects_source'] or
classifier['manipulates_source'] or
classifier['manipulates_state'])


def update_history(player, move):
"""Updates histories and cooperation / defections counts following play."""
Expand All @@ -42,6 +44,7 @@ def update_history(player, move):
elif move == D:
player.defections += 1


def init_args(func):
"""Decorator to simplify the handling of init_args. Use whenever overriding
Player.__init__ in subclasses of Player that require arguments as follows:
Expand Down
3 changes: 1 addition & 2 deletions axelrod/plot.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from numpy import arange, median, nan_to_num
from warnings import warn

matplotlib_installed = True
try:
Expand Down Expand Up @@ -36,7 +35,7 @@ def _violinplot(self, data, names, title=None):
figure = plt.figure(figsize=(width, height))
spacing = 4
positions = spacing * arange(1, nplayers + 1, 1)
plt.violinplot(data, positions=positions, widths=spacing/2,
plt.violinplot(data, positions=positions, widths=spacing / 2,
showmedians=True, showextrema=False)
plt.xticks(positions, names, rotation=90)
plt.xlim(0, spacing * (nplayers + 1))
Expand Down
2 changes: 2 additions & 0 deletions axelrod/random_.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import random
from axelrod import Actions


def random_choice(p=0.5):
"""
Return 'C' with probability `p`, else return 'D'
Expand All @@ -13,6 +14,7 @@ def random_choice(p=0.5):
return Actions.C
return Actions.D


def randrange(a, b):
"""Python 2 / 3 compatible randrange. Returns a random integer uniformly
between a and b (inclusive)"""
Expand Down
8 changes: 4 additions & 4 deletions axelrod/result_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def __init__(self, players, interactions, progress_bar=True):
self.players = players
self.nplayers = len(players)
self.interactions = interactions
self.nrepetitions = max([len(rep) for rep in list(interactions.values())])
self.nrepetitions = max(
[len(rep) for rep in list(interactions.values())])

if progress_bar:
self.progress_bar = tqdm.tqdm(total=19, desc="Analysing results")
Expand All @@ -58,7 +59,6 @@ def __init__(self, players, interactions, progress_bar=True):
# Calculate all attributes:
self.build_all()


def build_all(self):
"""Build all the results. In a seperate method to make inheritance more
straightforward"""
Expand Down Expand Up @@ -665,7 +665,7 @@ def build_eigenjesus_rating(self):
http://www.scottaaronson.com/morality.pdf
"""
eigenvector, eigenvalue = eigen.principal_eigenvector(
self.normalised_cooperation)
self.normalised_cooperation)

return eigenvector.tolist()

Expand All @@ -679,7 +679,7 @@ def build_eigenmoses_rating(self):
http://www.scottaaronson.com/morality.pdf
"""
eigenvector, eigenvalue = eigen.principal_eigenvector(
self.vengeful_cooperation)
self.vengeful_cooperation)

return eigenvector.tolist()

Expand Down
6 changes: 4 additions & 2 deletions axelrod/strategy_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def forgiver_wrapper(player, opponent, action, p):
ForgiverTransformer = StrategyTransformerFactory(
forgiver_wrapper, name_prefix="Forgiving")


def initial_sequence(player, opponent, action, initial_seq):
"""Play the moves in `seq` first (must be a list), ignoring the strategy's
moves until the list is exhausted."""
Expand Down Expand Up @@ -220,8 +221,8 @@ def history_track_wrapper(player, opponent, action):
player._recorded_history = [action]
return action

TrackHistoryTransformer = StrategyTransformerFactory(history_track_wrapper,
name_prefix="HistoryTracking")()
TrackHistoryTransformer = StrategyTransformerFactory(
history_track_wrapper, name_prefix="HistoryTracking")()


def deadlock_break_wrapper(player, opponent, action):
Expand All @@ -239,6 +240,7 @@ def deadlock_break_wrapper(player, opponent, action):
DeadlockBreakingTransformer = StrategyTransformerFactory(
deadlock_break_wrapper, name_prefix="DeadlockBreaking")()


def grudge_wrapper(player, opponent, action, grudges):
"""After `grudges` defections, defect forever."""
if opponent.defections > grudges:
Expand Down
2 changes: 1 addition & 1 deletion axelrod/tournament.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .game import Game
from .match import Match
from .match_generator import RoundRobinMatches, ProbEndRoundRobinMatches
from .result_set import ResultSet, ResultSetFromFile
from .result_set import ResultSetFromFile


class Tournament(object):
Expand Down

0 comments on commit 5148e64

Please sign in to comment.