def test__setitem__selection_string_multi_axis(self): # Test setting an index-based selection string from omsi.shared.data_selection import selection_to_string dependency_object = dependency_dict() selection_string = selection_to_string((5, [0, 1, 3], slice(0, 1, 4))) dependency_object['selection'] = selection_string self.assertEquals(dependency_object['selection'], selection_string)
def test__setitem__selection_string_index(self): # Test setting an index-based selection string from omsi.shared.data_selection import selection_to_string dependency_object = dependency_dict() selection_string = selection_to_string(5) dependency_object['selection'] = selection_string self.assertEquals(dependency_object['selection'], selection_string)
def __setitem__(self, key, value): """Overwrite the __setitem__ function inherited from dict to ensure that only elements with a specific set of keys can be modified""" from omsi.analysis.base import analysis_base from omsi.dataformat.file_reader_base import file_reader_base if key in self: if key == "omsi_object": if omsi_file_common.is_managed(value): dict.__setitem__(self, key, omsi_file_common.get_omsi_object(value)) elif isinstance(value, h5py.Dataset) or isinstance(value, h5py.Group): parent = value.parent if omsi_file_common.is_managed(parent): dict.__setitem__(self, 'omsi_object', omsi_file_common.get_omsi_object(parent)) dict.__setitem__(self, 'dataname', unicode(value.name.split('/')[-1])) # print super(dependency_dict,self).__str__() else: warnings.warn("The generated dependency does not point to a managed object.") dict.__setitem__(self, 'omsi_object', omsi_file_common.get_omsi_object(value)) dict.__setitem__(self, '_data', None) # Any previously loaded date may be invalid (delete) elif isinstance(value, analysis_base): dict.__setitem__(self, 'omsi_object', value) else: raise ValueError(str(value) + " invalid omsi_object parameter for " + "dependency_dict without valid data dependency.") elif key == 'selection': if value is None or (isinstance(value, basestring) and len(value) == 0): new_value = None else: from omsi.shared.data_selection import selection_to_string new_value = unicode(selection_to_string(selection=value)) dict.__setitem__(self, key, new_value) dict.__setitem__(self, '_data', None) # Any previously loaded data may be invalid (delete) elif key == 'dataname': if not isinstance(value, basestring): raise ValueError('Dataname must be a string') dict.__setitem__(self, 'dataname', unicode(value)) dict.__setitem__(self, '_data', None) # Any previously loaded data may be invalid (delete) elif key == 'param_name': if not isinstance(value, basestring): raise ValueError('param_name must be a string') dict.__setitem__(self, 'param_name', unicode(value)) elif key == 'link_name': if not isinstance(value, basestring): raise ValueError('link_name must be a string') dict.__setitem__(self, 'link_name', unicode(value)) elif key == '_data': raise KeyError('_data key is managed by dependency_dict. Explicit definition of _data not permitted.') elif key == 'help': if isinstance(value, basestring): dict.__setitem__(self, 'help', unicode(value)) elif key == 'dependency_type': if value in self.dependency_types.values(): dict.__setitem__(self, 'dependency_type', value) else: raise ValueError('Unknown dependency type specified. Valid types are: ' + str(self.dependency_types)) else: dict.__setitem__(self, key, value) # print super(dependency_dict,self).__str__() else: raise KeyError("\'"+str(key)+'\' key not in default key set of dependency_dict')