Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
elif key[0] == '+':
if self.omas_data is None:
key[0] = 0
elif isinstance(self.omas_data, list):
key[0] = len(self.omas_data)
# handle dynamic path creation for .code.parameters leaf
if len(key) == 1 and key[0] == 'parameters' and (self.location.endswith('.code') or not self.location) and not isinstance(value, str):
pass_on_value = value
value = CodeParameters()
value.update(pass_on_value)
# if the user has entered path rather than a single key
elif len(key) > 1:
pass_on_value = value
if key[0] == 'parameters' and (self.location.endswith('.code') or not self.location) and not isinstance(value, str):
value = CodeParameters()
value[key[1:]] = pass_on_value
else:
value = self.same_init_ods()
# full path where we want to place the data
location = l2o([self.location, key[0]])
if self.consistency_check and '.code.parameters.' not in location:
# perform consistency check with IMAS structure
structure = {}
structure_key = key[0] if not isinstance(key[0], int) else ':'
try:
if isinstance(value, ODS):
if not self.structure:
# load the json structure file
structure = load_structure(key[0], imas_version=self.imas_version)[1][key[0]]
def __setitem__(self, key, value):
key = p2l(key)
if not len(key):
return self
# go deeper
if len(key) > 1:
if key[0] not in self or not isinstance(self[key[0]], CodeParameters):
self.setraw(key[0], self.__class__())
self.getraw(key[0])[key[1:]] = value
# return leaf
else:
# convert ODSs to CodeParameters
if isinstance(value, ODS):
value = CodeParameters()
self.setraw(key[0], value)
elif isinstance(self.omas_data, list):
if not len(self.omas_data):
key[0] = 0
else:
key[0] = len(self.omas_data) + key[0]
# '+' is used to append new entry in array structure
elif key[0] == '+':
if self.omas_data is None:
key[0] = 0
elif isinstance(self.omas_data, list):
key[0] = len(self.omas_data)
# handle dynamic path creation for .code.parameters leaf
if len(key) == 1 and key[0] == 'parameters' and (self.location.endswith('.code') or not self.location) and not isinstance(value, str):
pass_on_value = value
value = CodeParameters()
value.update(pass_on_value)
# if the user has entered path rather than a single key
elif len(key) > 1:
pass_on_value = value
if key[0] == 'parameters' and (self.location.endswith('.code') or not self.location) and not isinstance(value, str):
value = CodeParameters()
value[key[1:]] = pass_on_value
else:
value = self.same_init_ods()
# full path where we want to place the data
location = l2o([self.location, key[0]])
if self.consistency_check and '.code.parameters.' not in location:
# perform consistency check with IMAS structure
structure = {}
def to_string(self):
'''
generate an XML string from this dictionary
:return: XML string
'''
import xmltodict
tmp = {'parameters': CodeParameters()}
tmp['parameters'].update(copy.deepcopy(self))
recursive_encoder(tmp)
return xmltodict.unparse(tmp, pretty=True)
def from_string(self, code_params_string):
'''
Load data from code.parameters XML string
:param code_params_string: XML string
:return: self
'''
import xmltodict
self.clear()
if not code_params_string.strip().endswith(''):
code_params_string = '' + code_params_string + ''
tmp = xmltodict.parse(code_params_string).get('parameters', '')
if tmp:
recursive_interpreter(tmp, dict_cls=CodeParameters)
self.update(tmp)
return self
self.omas_data = []
else:
if not isinstance(self.omas_data, dict):
self.omas_data = {}
elif isinstance(key[0], int) and not isinstance(self.omas_data, list):
raise TypeError('Cannot convert from dict to list once ODS has data')
elif isinstance(key[0], str) and not isinstance(self.omas_data, dict):
raise TypeError('Cannot convert from list to dict once ODS has data')
# if the value is not an ODS strucutre
if not isinstance(value, ODS):
# convert simple dict of code.parameters to CodeParameters instances
if '.code.parameters' in location and not isinstance(value, CodeParameters) and isinstance(value, (dict, ODS)):
tmp = value
value = CodeParameters()
value.update(tmp)
# now that all checks are completed we can assign the structure information
if self.consistency_check and '.code.parameters.' not in location:
ulocation = o2u(location)
# handle cocos transformations coming in
if self.cocosio and self.cocosio != self.cocos and '.' in location and ulocation in omas_physics.cocos_signals and not isinstance(value, ODS):
transform = omas_physics.cocos_signals[ulocation]
if transform == '?':
if self.consistency_check == 'warn':
printe('COCOS translation has not been setup: %s' % ulocation)
norm = 1.0
else:
raise ValueError('COCOS translation has not been setup: %s' % ulocation)
else: