Skip to content

Fix deprecation warnings #793

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 1 commit into from
Jan 1, 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
2 changes: 1 addition & 1 deletion axelrod/strategies/memoryone.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,7 @@ class ZDGTFT2(LRPlayer):

name = 'ZD-GTFT-2'

@init_args
def __init__(self, phi=0.25, s=0.5):
"""
Parameters
Expand All @@ -391,7 +392,6 @@ def __init__(self, phi=0.25, s=0.5):
self.phi = phi
self.s = s
super(ZDGTFT2, self).__init__()
self.init_args = (phi, s)

def receive_match_attributes(self):
(R, P, S, T) = self.match_attributes["game"].RPST()
Expand Down
2 changes: 1 addition & 1 deletion axelrod/strategies/prober.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ class Prober4(Player):
'manipulates_state': False
}

@init_args
def __init__(self):
Player.__init__(self)
self.init_sequence = [
Expand Down Expand Up @@ -277,6 +276,7 @@ class RemorsefulProber(NaiveProber):
'manipulates_state': False
}

@init_args
def __init__(self, p=0.1):
NaiveProber.__init__(self, p)
self.probing = False
Expand Down
1 change: 0 additions & 1 deletion axelrod/strategies/retaliate.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,6 @@ def __init__(self, retaliation_threshold=0.1, retaliation_limit=20):
self.retaliation_threshold = retaliation_threshold
self.retaliation_limit = retaliation_limit
self.play_counts = defaultdict(int)
self.init_args = (retaliation_threshold, retaliation_limit)

self.name = (
'Limited Retaliate (' +
Expand Down
1 change: 0 additions & 1 deletion axelrod/strategies/titfortat.py
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,6 @@ class AdaptiveTitForTat(Player):

@init_args
def __init__(self, rate=0.5):

Player.__init__(self)
self.rate, self.starting_rate = rate, rate

Expand Down
23 changes: 16 additions & 7 deletions axelrod/strategy_transformers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
See the various Meta strategies for another type of transformation.
"""

import collections
import inspect
import random
import collections

from numpy.random import choice
from .actions import Actions, flip_action
from .random_ import random_choice
Expand Down Expand Up @@ -78,16 +79,24 @@ def __call__(self, PlayerClass):
except KeyError:
pass

# Is the original strategy method a static method?
signature = inspect.signature(PlayerClass.strategy)
strategy_args = [p.name for p in signature.parameters.values()
if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD]
is_static = True
if len(strategy_args) > 1:
is_static = False

# Define the new strategy method, wrapping the existing method
# with `strategy_wrapper`
def strategy(self, opponent):
# Is the original strategy method a static method?
argspec = inspect.getargspec(getattr(PlayerClass, "strategy"))
if 'self' in argspec.args:
# it's not a static method
proposed_action = PlayerClass.strategy(self, opponent)
else:

if is_static:
# static method
proposed_action = PlayerClass.strategy(opponent)
else:
proposed_action = PlayerClass.strategy(self, opponent)

# Apply the wrapper
return strategy_wrapper(self, opponent, proposed_action,
*args, **kwargs)
Expand Down
50 changes: 36 additions & 14 deletions axelrod/tests/unit/test_player.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
import inspect
import random
import unittest

Expand All @@ -22,6 +20,7 @@ def randomize(self):
return random.choice([C, D])



class TestPlayerClass(unittest.TestCase):

name = "Player"
Expand Down Expand Up @@ -98,6 +97,23 @@ def test_strategy(self):
self.assertRaises(
NotImplementedError, self.player().strategy, self.player())

def test_clone(self):
"""Tests player cloning."""
p1 = axelrod.Random(0.75) # 0.5 is the default
p2 = p1.clone()
turns = 50
for op in [axelrod.Cooperator(), axelrod.Defector(),
axelrod.TitForTat()]:
p1.reset()
p2.reset()
seed = random.randint(0, 10 ** 6)
for p in [p1, p2]:
axelrod.seed(seed)
m = axelrod.Match((p, op), turns=turns)
m.play()
self.assertEqual(len(p1.history), turns)
self.assertEqual(p1.history, p2.history)


def test_responses(test_class, P1, P2, history_1, history_2, responses,
random_seed=None, attrs=None):
Expand Down Expand Up @@ -203,20 +219,12 @@ def test_reset(self):
for k, v in p_clone.__dict__.items():
self.assertEqual(v, getattr(p_clone, k))


def test_clone(self):
# Make sure that self.init_args has the right number of arguments
PlayerClass = self.player
argspec = inspect.getargspec(PlayerClass.__init__)
# Does the class use the init_args decorator?
if argspec.varargs == "args":
self.assertEqual(len(argspec.args), 1)
else:
player = PlayerClass()
self.assertEqual(len(argspec.args) - 1, len(player.init_args))

# Test that the player is cloned correctly
# Test that the cloned player produces identical play
p1 = self.player()
if str(p1) in ["Darwin", "Human"]:
# Known exceptions
return
p2 = p1.clone()
self.assertEqual(len(p2.history), 0)
self.assertEqual(p2.cooperations, 0)
Expand All @@ -225,6 +233,20 @@ def test_clone(self):
self.assertEqual(p2.classifier, p1.classifier)
self.assertEqual(p2.match_attributes, p1.match_attributes)

turns = 50
r = random.random()
for op in [axelrod.Cooperator(), axelrod.Defector(),
axelrod.TitForTat(), axelrod.Random(r)]:
p1.reset()
p2.reset()
seed = random.randint(0, 10 ** 6)
for p in [p1, p2]:
axelrod.seed(seed)
m = axelrod.Match((p, op), turns=turns)
m.play()
self.assertEqual(len(p1.history), turns)
self.assertEqual(p1.history, p2.history)

def first_play_test(self, play, random_seed=None):
"""Tests first move of a strategy."""
P1 = self.player()
Expand Down