Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_init():
c = TradingContext(base_instrument=config['base_instrument'],
instruments=config['instruments'])
assert c.shared.get('base_instrument') == 'EURO'
assert c.shared.get('instruments') == ['BTC', 'ETH']
def test_has_config_attribute():
c = TradingContext()
assert hasattr(c, 'shared')
assert hasattr(c, 'exchanges')
assert hasattr(c, 'actions')
assert hasattr(c, 'rewards')
assert hasattr(c, 'features')
def test_init_with_kwargs():
c = TradingContext(**config)
assert c.shared.get('base_instrument') == 'EURO'
assert c.shared.get('instruments') == ['BTC', 'ETH']
def get_context():
return TradingContext.get_context()
def get_contexts(cls):
if not hasattr(cls.contexts, 'stack'):
cls.contexts.stack = [TradingContext()]
return cls.contexts.stack
def __call__(cls, *args, **kwargs):
registered_name = get_registry()[cls]
tc = TradingContext.get_context()
data = tc.data.get(registered_name, {})
config = {**tc.shared, **data}
instance = cls.__new__(cls, *args, **kwargs)
setattr(instance, 'context', Context(**config))
instance.__init__(*args, **kwargs)
return instance
def from_yaml(cls, path: str):
with open(path, "rb") as fp:
config = yaml.load(fp, Loader=yaml.FullLoader)
return TradingContext(**config)
def from_json(cls, path: str):
with open(path, "rb") as fp:
config = json.load(fp)
return TradingContext(**config)