Skip to content

Add type hints to oncebitten, prober, punisher, rand #864

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

Merged
merged 7 commits into from
Feb 19, 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
14 changes: 7 additions & 7 deletions axelrod/strategies/oncebitten.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import random
from axelrod.actions import Actions
from axelrod.actions import Actions, Action
from axelrod.player import Player

C, D = Actions.C, Actions.D
Expand All @@ -22,13 +22,13 @@ class OnceBitten(Player):
'manipulates_state': False
}

def __init__(self):
def __init__(self) -> None:
super().__init__()
self.mem_length = 10
self.grudged = False
self.grudge_memory = 0

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
"""
Begins by playing C, then plays D for mem_length rounds if the opponent
ever plays D twice in a row.
Expand Down Expand Up @@ -72,7 +72,7 @@ class FoolMeOnce(Player):
}

@staticmethod
def strategy(opponent):
def strategy(opponent: Player) -> Action:
if not opponent.history:
return C
if opponent.defections > 1:
Expand All @@ -98,7 +98,7 @@ class ForgetfulFoolMeOnce(Player):
'manipulates_state': False
}

def __init__(self, forget_probability=0.05):
def __init__(self, forget_probability: float=0.05) -> None:
"""
Parameters
----------
Expand All @@ -110,7 +110,7 @@ def __init__(self, forget_probability=0.05):
self._initial = C
self.forget_probability = forget_probability

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
r = random.random()
if not opponent.history:
return self._initial
Expand Down Expand Up @@ -145,7 +145,7 @@ class FoolMeForever(Player):
}

@staticmethod
def strategy(opponent):
def strategy(opponent: Player) -> Action:
if opponent.defections > 0:
return C
return D
26 changes: 13 additions & 13 deletions axelrod/strategies/prober.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from axelrod.actions import Actions
from axelrod.actions import Actions, Action
from axelrod.player import Player
from axelrod.random_ import random_choice

Expand Down Expand Up @@ -31,7 +31,7 @@ class CollectiveStrategy(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
turn = len(self.history)
if turn == 0:
return C
Expand Down Expand Up @@ -61,7 +61,7 @@ class Prober(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
turn = len(self.history)
if turn == 0:
return D
Expand Down Expand Up @@ -94,7 +94,7 @@ class Prober2(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
turn = len(self.history)
if turn == 0:
return D
Expand Down Expand Up @@ -127,7 +127,7 @@ class Prober3(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
turn = len(self.history)
if turn == 0:
return D
Expand Down Expand Up @@ -165,7 +165,7 @@ class Prober4(Player):
'manipulates_state': False
}

def __init__(self):
def __init__(self) -> None:
super().__init__()
self.init_sequence = [
C, C, D, C, D, D, D, C, C, D, C, D, C, C, D, C, D, D, C, D
Expand All @@ -174,7 +174,7 @@ def __init__(self):
self.unjust_Ds = 0
self.turned_defector = False

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
if not self.history:
return self.init_sequence[0]
turn = len(self.history)
Expand Down Expand Up @@ -219,7 +219,7 @@ class HardProber(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
turn = len(self.history)
if turn == 0:
return D
Expand Down Expand Up @@ -258,7 +258,7 @@ class NaiveProber(Player):
'manipulates_state': False
}

def __init__(self, p=0.1):
def __init__(self, p: Player=0.1) -> None:
"""
Parameters
----------
Expand All @@ -270,7 +270,7 @@ def __init__(self, p=0.1):
if (self.p == 0) or (self.p == 1):
self.classifier['stochastic'] = False

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
# First move
if len(self.history) == 0:
return C
Expand All @@ -281,7 +281,7 @@ def strategy(self, opponent):
choice = random_choice(1 - self.p)
return choice

def __repr__(self):
def __repr__(self) -> str:
return "%s: %s" % (self.name, round(self.p, 2))


Expand Down Expand Up @@ -314,11 +314,11 @@ class RemorsefulProber(NaiveProber):
'manipulates_state': False
}

def __init__(self, p=0.1):
def __init__(self, p: float=0.1) -> None:
super().__init__(p)
self.probing = False

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
# First move
if len(self.history) == 0:
return C
Expand Down
14 changes: 7 additions & 7 deletions axelrod/strategies/punisher.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from axelrod.actions import Actions
from axelrod.actions import Actions, Action
from axelrod.player import Player

C, D = Actions.C, Actions.D
Expand Down Expand Up @@ -27,7 +27,7 @@ class Punisher(Player):
'manipulates_state': False
}

def __init__(self):
def __init__(self) -> None:
"""
Initialised the player
"""
Expand All @@ -36,7 +36,7 @@ def __init__(self):
self.grudged = False
self.grudge_memory = 1

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
"""
Begins by playing C, then plays D for an amount of rounds proportional
to the opponents historical '%' of playing D if the opponent ever
Expand Down Expand Up @@ -89,14 +89,14 @@ class InversePunisher(Player):
'manipulates_state': False
}

def __init__(self):
def __init__(self) -> None:
super().__init__()
self.history = []
self.history = [] # type: List[Action]
self.mem_length = 1
self.grudged = False
self.grudge_memory = 1

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
"""
Begins by playing C, then plays D for an amount of rounds proportional
to the opponents historical '%' of playing C if the opponent ever plays
Expand Down Expand Up @@ -147,7 +147,7 @@ class LevelPunisher(Player):
'manipulates_state': False
}

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
if len(opponent.history) < 10:
return C
elif (len(opponent.history) - opponent.cooperations) / len(opponent.history) > 0.2:
Expand Down
7 changes: 4 additions & 3 deletions axelrod/strategies/rand.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from axelrod.player import Player
from axelrod.actions import Action
from axelrod.random_ import random_choice


Expand All @@ -22,7 +23,7 @@ class Random(Player):
'manipulates_state': False
}

def __init__(self, p=0.5):
def __init__(self, p: float=0.5) -> None:
"""
Parameters
----------
Expand All @@ -39,8 +40,8 @@ def __init__(self, p=0.5):
if p in [0, 1]:
self.classifier['stochastic'] = False

def strategy(self, opponent):
def strategy(self, opponent: Player) -> Action:
return random_choice(self.p)

def __repr__(self):
def __repr__(self) -> str:
return "%s: %s" % (self.name, round(self.p, 2))
10 changes: 7 additions & 3 deletions type_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,20 @@ mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/cycler.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/darwin.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/defector.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/forgiver.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/geller.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/gradualkiller.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/grudger.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/grumpy.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/handshake.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/hunter.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/inverse.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mathematicalconstants.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/memorytwo.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mindcontrol.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mindreader.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/mutual.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/negation.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/hunter.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/geller.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/memorytwo.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/oncebitten.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/prober.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/punisher.py
mypy --ignore-missing-imports --follow-imports skip axelrod/strategies/rand.py