def instance_from_dict(d): """Returns a new DLite instance created from dict `d`, which should be of the same form as returned by the Instance.asdict() method. """ meta = dlite.get_instance(d['meta']) if meta.is_metameta: try: return dlite.get_instance(d['uri']) except RuntimeError: pass dimensions = [ dlite.Dimension(d['name'], d.get('description')) for d in d['dimensions'] ] props = [] dimmap = {dim['name']: i for i, dim in enumerate(d['dimensions'])} for p in d['properties']: if 'dims' in p: dims = [dimmap[d] for d in p['dims']] else: dims = None props.append( dlite.Property(p['name'], p['type'], dims, p.get('unit'), p.get('description'))) inst = dlite.Instance(d['uri'], dimensions, props, d.get('description')) else: dims = list(d['dimensions'].values()) inst = dlite.Instance(meta.uri, dims, d.get('uuid', None)) for p in meta['properties']: inst[p.name] = d['properties'][p.name] return inst
def load(self, uuid): """Loads `uuid` from current storage and return it as a new instance.""" uuid = dlite.get_uuid(uuid) q = sql.SQL('SELECT meta FROM uuidtable WHERE uuid = %s') self.cur.execute(q, [uuid]) metaid, = self.cur.fetchone() q = sql.SQL('SELECT * FROM {} WHERE uuid = %s').format( sql.Identifier(metaid)) self.cur.execute(q, [uuid]) tokens = self.cur.fetchone() uuid_, uri, metaid_, dims = tokens[:4] values = tokens[4:] assert uuid_ == uuid assert metaid_ == metaid inst = dlite.Instance(metaid, dims, uri) for i, p in enumerate(inst.meta['properties']): inst.set_property(p.name, values[i]) return inst
dims = None props.append( dlite.Property(p['name'], p['type'], dims, p.get('unit'), p.get('description'))) inst = dlite.Instance(d['uri'], dimensions, props, d.get('description')) else: dims = list(d['dimensions'].values()) inst = dlite.Instance(meta.uri, dims, d.get('uuid', None)) for p in meta['properties']: inst[p.name] = d['properties'][p.name] return inst if __name__ == '__main__': url = 'json://' + os.path.join(thisdir, 'tests', 'Person.json') #+ "?mode=r" Person = dlite.Instance(url) person = Person([2]) person.name = 'Ada' person.age = 12.5 person.skills = ['skiing', 'jumping'] d1 = person.asdict() inst1 = instance_from_dict(d1) d2 = Person.asdict() inst2 = instance_from_dict(d2)
def map(self, instances): inst3 = instances[0] print(inst3) inst1 = dlite.Instance(self.output_uri, []) inst1.a = int(inst3.c - 12.0 + 0.5) return inst1
def map(self, instances): inst1 = instances[0] inst3 = dlite.Instance(self.output_uri, []) inst3.c = inst1.a + 12.0 return inst3