def set_map(self, map_object): if not isinstance(map_object, dict): raise TypeError('Expected dict instead of %s' % type(map_object)) if not 'objects' in map_object: raise BVException('Expected map_object["objects"]; available keys are %s' % map_object.keys()) # get a copy (because later we remove 'texture') self.map_purified = deepcopy(map_object) # we also save a copy for calling us again after serialization self.map = deepcopy(map_object) assert_has_key(self.map_purified, 'objects') objects = self.map_purified['objects'] assert_type(objects, list) for object in objects: assert_type(object, dict) if object.has_key('texture'): texture = object.get('texture') del object['texture'] else: # FIXME make this configurable raise ValueError('texture not provided for object %s. Map was %s' \ % (object, self.map)) # texture = lambda x: 0.5 if isinstance(texture, str): texture = eval(texture) surface = object['surface'] self.surface2texture[surface] = texture
def set_map(self, world): # we will modify "map" throughout map = deepcopy(world) # also we make a copy for pickling self.map = deepcopy(world) self.all_chemicals = set() self.sources = [] assert_type(map, dict) sources_key = 'olfaction_sources' assert_has_key(map, sources_key) sources = map[sources_key] assert_type(sources, list) for source in sources: assert_type(source, dict) assert_has_key(source, 'position') assert_has_key(source, 'components') position = source['position'] assert_type(position, list) if not len(position) == 3: raise ValueError('I expect position with 3 elements, got %s' % position) position = array(position) components = source['components'] assert_type(components, dict) for chemical, value in components.items(): assert_type(chemical, str) if isinstance(value, str): value = eval(value) components[chemical] = value assert_type(value, [int, float, type(lambda x: 0) ]) #@UnusedVariable self.all_chemicals.add(chemical) if len(components.keys()) == 0: raise ValueError('Did you pass me a map without sources? %s' % world) self.sources.append((position, components))