Skip to content

Commit 09e48cb

Browse files
committed
Lookup classifiers from yaml when no trivial initializer
1 parent 1fe2431 commit 09e48cb

File tree

2 files changed

+30
-8
lines changed

2 files changed

+30
-8
lines changed

axelrod/classifier.py

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,22 @@ def __getitem__(
179179

180180
def classify_player_for_this_classifier(
181181
player: Union[Player, Type[Player]]) -> Any:
182+
def try_lookup() -> Any:
183+
try:
184+
player_classifiers = cls.all_player_dicts[player.name]
185+
except:
186+
return None
187+
188+
return player_classifiers.get(key, None)
189+
182190
# If the passed player is not an instance, then try to initialize an
183191
# instance without arguments.
184192
if not isinstance(player, Player):
185193
try:
186194
player = player()
187195
except:
188-
pass
196+
# Can't use the instances, so just go by name.
197+
return try_lookup()
189198

190199
# Factory-generated players won't exist in the table. As well, some
191200
# players, like Random, may change classifiers at construction time;
@@ -194,12 +203,8 @@ def classify_player_for_this_classifier(
194203
if key in player.classifier:
195204
return player.classifier[key]
196205

197-
try:
198-
player_classifiers = cls.all_player_dicts[player.name]
199-
except:
200-
return None
201-
202-
return player_classifiers.get(key, None)
206+
# Try to find the name in the all_player_dicts, read from disk.
207+
return try_lookup()
203208

204209
return classify_player_for_this_classifier
205210

axelrod/tests/unit/test_classification.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import os
44
import unittest
5-
from typing import Text
5+
from typing import Any, Text
66

77
import yaml
88

@@ -27,6 +27,20 @@ class TitForTatWithEmptyClassifier(Player):
2727
classifier = {}
2828

2929

30+
class TitForTatWithNonTrivialInitialzer(Player):
31+
"""
32+
Same name as TitForTat, but with empty classifier.
33+
"""
34+
35+
def __init__(self, x: Any):
36+
self.x = x # Doesn't actually do anything.
37+
super(TitForTatWithNonTrivialInitialzer).__init__()
38+
39+
# Classifiers are looked up by name, so only the name matters.
40+
name = "Tit For Tat"
41+
classifier = {}
42+
43+
3044
class TestClassification(unittest.TestCase):
3145
def test_classifier_build(self):
3246
dirname = os.path.dirname(__file__)
@@ -67,6 +81,9 @@ def test_key_error_on_uknown_classifier(self):
6781
def test_will_lookup_key_in_dict(self):
6882
self.assertEqual(Classifiers["memory_depth"](TitForTatWithEmptyClassifier), 1)
6983

84+
def test_will_lookup_key_for_classes_that_cant_init(self):
85+
self.assertEqual(Classifiers["memory_depth"](TitForTatWithNonTrivialInitialzer), 1)
86+
7087
def test_known_classifiers(self):
7188
# A set of dimensions that are known to have been fully applied
7289
known_keys = [

0 commit comments

Comments
 (0)