apply __slots__ to resolver and reintroduce type conversations

This commit is contained in:
gcarq 2018-03-24 23:32:17 +01:00
parent 99e890bc99
commit 5fb6fa38aa
2 changed files with 11 additions and 11 deletions

View File

@ -21,6 +21,9 @@ class StrategyResolver(object):
"""
This class contains all the logic to load custom strategy class
"""
__slots__ = ['strategy']
def __init__(self, config: Optional[Dict] = None) -> None:
"""
Load the custom class from config parameter
@ -39,22 +42,24 @@ class StrategyResolver(object):
logger.info("Override strategy \'minimal_roi\' with value in config file.")
if 'stoploss' in config:
self.strategy.stoploss = float(config['stoploss'])
self.strategy.stoploss = config['stoploss']
logger.info(
"Override strategy \'stoploss\' with value in config file: %s.", config['stoploss']
)
if 'ticker_interval' in config:
self.strategy.ticker_interval = int(config['ticker_interval'])
self.strategy.ticker_interval = config['ticker_interval']
logger.info(
"Override strategy \'ticker_interval\' with value in config file: %s.",
config['ticker_interval']
)
# Minimal ROI designed for the strategy
# Sort and apply type conversions
self.strategy.minimal_roi = OrderedDict(sorted(
{int(key): value for (key, value) in self.strategy.minimal_roi.items()}.items(),
key=lambda t: t[0])) # sort after converting to number
key=lambda t: t[0]))
self.strategy.stoploss = float(self.strategy.stoploss)
self.strategy.ticker_interval = int(self.strategy.ticker_interval)
def _load_strategy(self, strategy_name: str) -> Optional[IStrategy]:
"""
@ -76,16 +81,12 @@ class StrategyResolver(object):
raise ImportError('not found')
# Fallback to the default strategy
except (ImportError, TypeError) as error:
logger.error(
except (ImportError, TypeError):
logger.exception(
"Impossible to load Strategy '%s'. This class does not exist"
" or contains Python code errors",
strategy_name
)
logger.error(
"The error is:\n%s.",
error
)
return None
@staticmethod

View File

@ -115,7 +115,6 @@ def test_strategy_override_ticker_interval(caplog):
def test_strategy_fallback_default_strategy():
strategy = StrategyResolver()
strategy.logger = logging.getLogger(__name__)
assert not hasattr(StrategyResolver, 'custom_strategy')
strategy._load_strategy('../../super_duper')