Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
variable = variable.strip('/')
if '/' in variable:
var = variable.split('/')
prefix = var[:-1]
var_name = var[-1]
else:
prefix = []
var_name = variable
for i, p in enumerate(prefix):
if p not in config:
config[p] = dict()
if isinstance(config[p], dict):
config = config[p]
else: # for example, we put value with key 'a/b' into `{a: c}`
value = Config({'/'.join(prefix[i+1:] + [var_name]): value})
var_name = p
break
if var_name in config and isinstance(config[var_name], dict) and isinstance(value, Config):
config[var_name] = Config(config[var_name])
config[var_name].update(value)
config[var_name] = config[var_name].config
else:
if isinstance(value, Config):
config[var_name] = value.config
else:
config[var_name] = value
""" Put a new variable into config
Parameters
----------
variable : str
variable to add. '/' is used to put value into nested dict
value : masc
config : dict, Config or None
if None value will be putted into self.config else from config
"""
if config is None:
config = self.config
elif isinstance(config, Config):
config = config.config
if isinstance(value, dict):
value = Config(value)
variable = variable.strip('/')
if '/' in variable:
var = variable.split('/')
prefix = var[:-1]
var_name = var[-1]
else:
prefix = []
var_name = variable
for i, p in enumerate(prefix):
if p not in config:
config[p] = dict()
if isinstance(config[p], dict):
config = config[p]
else: # for example, we put value with key 'a/b' into `{a: c}`
value = Config({'/'.join(prefix[i+1:] + [var_name]): value})
def pop(cls, variables, config, **kwargs):
""" Return variables and remove them from config"""
return Config().pop(variables, config, **kwargs)
var_name = var[-1]
else:
prefix = []
var_name = variable
for i, p in enumerate(prefix):
if p not in config:
config[p] = dict()
if isinstance(config[p], dict):
config = config[p]
else: # for example, we put value with key 'a/b' into `{a: c}`
value = Config({'/'.join(prefix[i+1:] + [var_name]): value})
var_name = p
break
if var_name in config and isinstance(config[var_name], dict) and isinstance(value, Config):
config[var_name] = Config(config[var_name])
config[var_name].update(value)
config[var_name] = config[var_name].config
else:
if isinstance(value, Config):
config[var_name] = value.config
else:
config[var_name] = value
def put(cls, variable, value, config):
""" Put a new variable into config """
return Config().put(variable, value, config)
for i, p in enumerate(prefix):
if p not in config:
config[p] = dict()
if isinstance(config[p], dict):
config = config[p]
else: # for example, we put value with key 'a/b' into `{a: c}`
value = Config({'/'.join(prefix[i+1:] + [var_name]): value})
var_name = p
break
if var_name in config and isinstance(config[var_name], dict) and isinstance(value, Config):
config[var_name] = Config(config[var_name])
config[var_name].update(value)
config[var_name] = config[var_name].config
else:
if isinstance(value, Config):
config[var_name] = value.config
else:
config[var_name] = value
def pop(cls, variables, config, **kwargs):
""" Return variables and remove them from config"""
return Config().pop(variables, config, **kwargs)
def __add__(self, other):
if isinstance(other, dict):
other = Config(other)
return Config([*self.flatten().items(), *other.flatten().items()])
def __lshift__(self, other):
new_p = self.from_pipeline(self)
if isinstance(other, Baseset):
new_p.dataset = other
return new_p
if isinstance(other, (Config, dict)):
new_p.set_config(other)
return new_p
raise TypeError("Pipeline might take only Dataset or Config. Use as pipeline << dataset or pipeine << config")