Skip to content

Commit 68c357e

Browse files
eric-s-sdrvinceknight
authored andcommitted
ready to push
1 parent 46eebed commit 68c357e

File tree

4 files changed

+272
-193
lines changed

4 files changed

+272
-193
lines changed

axelrod/strategy_transformers.py

+36-23
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,11 @@ def __init__(self, *args, **kwargs):
5959
self.name_prefix = name_prefix
6060

6161
def __reduce__(self):
62-
# Creates only the Decorator class and not the instance
63-
return StrategyTransformerFactory, (
64-
strategy_wrapper, name_prefix, reclassifier)
62+
return (
63+
DecoratorReBuilder(),
64+
(strategy_wrapper, name_prefix, reclassifier,
65+
self.args, self.kwargs, self.name_prefix)
66+
)
6567

6668
def __call__(self, PlayerClass):
6769
"""
@@ -152,13 +154,13 @@ def reduce_for_decorated_class(self_):
152154
decorators = []
153155
for klass in self_.__class__.mro():
154156
if hasattr(klass, 'decorator'):
155-
decorators.append(klass.decorator)
157+
decorators.insert(0, klass.decorator)
156158
else:
157159
import_name = klass.__name__
158160
break
159161

160162
return (
161-
ReConstructor(),
163+
StrategyReBuilder(),
162164
(decorators, import_name, self_.__module__),
163165
self_.__dict__
164166
)
@@ -171,7 +173,7 @@ def reduce_for_decorated_class(self_):
171173
"name": name,
172174
"original_class": PlayerClass,
173175
"strategy": strategy,
174-
"decorator": (self, self.args, self.kwargs),
176+
"decorator": self,
175177
"__repr__": __repr__,
176178
"__module__": PlayerClass.__module__,
177179
"classifier": classifier,
@@ -183,7 +185,32 @@ def reduce_for_decorated_class(self_):
183185
return Decorator
184186

185187

186-
class ReConstructor(object):
188+
def is_strategy_static(player_class):
189+
"""
190+
191+
:param player_class: Any class that inherits from axelrod.Player
192+
:return: bool
193+
"""
194+
for klass in player_class.mro():
195+
method = inspect.getattr_static(klass, 'strategy', default=None)
196+
if method is not None:
197+
return isinstance(method, staticmethod)
198+
199+
200+
class DecoratorReBuilder(object):
201+
def __init__(self):
202+
pass
203+
204+
def __call__(self, strategy_wrapper, name_prefix, reclassifier,
205+
args, kwargs, instance_name_prefix):
206+
decorator_class = StrategyTransformerFactory(
207+
strategy_wrapper, name_prefix, reclassifier
208+
)
209+
kwargs['name_prefix'] = instance_name_prefix
210+
return decorator_class(*args, **kwargs)
211+
212+
213+
class StrategyReBuilder(object):
187214
def __init__(self):
188215
pass
189216

@@ -195,8 +222,8 @@ def __call__(self, decorators, import_name, module_name):
195222
return import_class()
196223
else:
197224
generated_class = import_class
198-
for decorator, args, kwargs in decorators:
199-
generated_class = decorator(*args, **kwargs)(generated_class)
225+
for decorator in decorators:
226+
generated_class = decorator(generated_class)
200227
return generated_class()
201228

202229

@@ -288,20 +315,6 @@ def dual_wrapper(player, opponent, proposed_action):
288315
return action.flip()
289316

290317

291-
def is_strategy_static(player_class):
292-
# signature = inspect.signature(player_class.strategy)
293-
# strategy_args = [p.name for p in signature.parameters.values()
294-
# if p.kind == inspect.Parameter.POSITIONAL_OR_KEYWORD]
295-
# is_static = True
296-
# if len(strategy_args) > 1:
297-
# is_static = False
298-
# return is_static
299-
for klass in player_class.mro():
300-
method = inspect.getattr_static(klass, 'strategy', default=None)
301-
if method is not None:
302-
return isinstance(method, staticmethod)
303-
304-
305318
DualTransformer = StrategyTransformerFactory(dual_wrapper, name_prefix="Dual")
306319

307320

axelrod/tests/classes_for_testing_pickling.py

-126
This file was deleted.

0 commit comments

Comments
 (0)