def execute(self, itoms): """Executes the function given the itoms. itoms -- 'Itoms' object, list or dictionary of itoms """ # make sure its an Itoms-object itoms = Itoms(itoms) # verify inputs if not set([v.name for v in self.__vin]).issubset( set([v.name for v in itoms.values()])): raise RuntimeError("Missing itoms to execute the function.") # generate code code = self.__enclosed_code() # execute code itom_vars = {i.codename: i for i in itoms.values()} self.__locals.update(itom_vars) exec(code, { 'Itom': Itom, 'copy': copy, 'interval': interval }, self.__locals) # local_vars may include utils and functions # -> keep only inputs and output itoms[self.vout.name] = self.__locals[self.vout.name] return itoms
def test_init_itoms(self): empty = Itoms() a = Itom('a', 0) b = Itom('b', 1, variable='b') c = Itom('c', 2.1) l = Itoms(list=[a, b, c]) self.assertEqual(len(l), 3) self.assertEqual(l['a'].v, 0) l = Itoms([a, b, c]) self.assertEqual(len(l), 3) self.assertEqual(l['b'].v, 1) # try different initial iterables and keys l = Itoms({1: a, 2: b, 3: c}) self.assertEqual(len(l), 3) self.assertEqual(l[1], a) l = Itoms(set([a, b, c])) self.assertEqual(len(l), 3) self.assertTrue(c in l.values()) # start from empty dict l = Itoms() l['av'] = a self.assertEqual(len(l), 1) self.assertEqual(l['av'], a)
def execute(self, itoms): """Execute the substitution and returns all variables. itoms -- dictionary of itom-name->'Itom'-object. These will be passed as local variables to the functions. Passes itoms through the functions. Output variables of functions may be appended (variable-name->'Itom'-object). However, the last variable assigned is 'this.vout' which is the result of the substitution given the inputs 'itoms'. The value (and possibly a timestamp) of 'this.vout' can be retrieved from the returned variables ('this.vout.v'). """ # make sure its an Itoms-object itoms = Itoms(itoms) # verify inputs if not set([v.name for v in self.vin]).issubset( set([v.name for v in itoms.values()])): raise RuntimeError("Missing itoms to execute the substitution.") # execute function after function for f in self: itoms = f.execute(itoms) return itoms