def test_update_changes_settings(self): """ When the code changes, do the inputs, outputs, name, etc update? """ # Initialize function with code. code = "def foo():\n" \ " pass" ast_code = compiler.parse(code) local_funcs = find_local_defs(ast_code) func = LocalFunctionInfo.from_function_ast(local_funcs['foo']) self.assertEqual(func.name, "foo") inputs = [(x.name, x.default) for x in func.inputs] self.assertEqual(inputs, []) outputs = [x.name for x in func.outputs] self.assertEqual(outputs, []) self.assertEqual(func.doc_string, "") self.assertTrue(func.is_valid) self.assertFalse(func.load_error) # Now update the function with new code. func.code = "def bar(x):\n" \ " 'doc string'\n" \ " return y" self.assertEqual(func.name, "bar") inputs = [(x.name, x.default) for x in func.inputs] self.assertEqual(inputs, [('x',None)]) outputs = [x.name for x in func.outputs] self.assertEqual(outputs, ['y']) self.assertEqual(func.doc_string, 'doc string') self.assertTrue(func.is_valid) self.assertFalse(func.load_error)
def test_update_changes_settings(self): """ When the code changes, do the inputs, outputs, name, etc update? """ # Initialize function with code. code = "def foo():\n" \ " pass" ast_code = compiler.parse(code) local_funcs = find_local_defs(ast_code) func = LocalFunctionInfo.from_function_ast(local_funcs['foo']) self.assertEqual(func.name, "foo") inputs = [(x.name, x.default) for x in func.inputs] self.assertEqual(inputs, []) outputs = [x.name for x in func.outputs] self.assertEqual(outputs, []) self.assertEqual(func.doc_string, "") self.assertTrue(func.is_valid) self.assertFalse(func.load_error) # Now update the function with new code. func.code = "def bar(x):\n" \ " 'doc string'\n" \ " return y" self.assertEqual(func.name, "bar") inputs = [(x.name, x.default) for x in func.inputs] self.assertEqual(inputs, [('x', None)]) outputs = [x.name for x in func.outputs] self.assertEqual(outputs, ['y']) self.assertEqual(func.doc_string, 'doc string') self.assertTrue(func.is_valid) self.assertFalse(func.load_error)
def test_empty(self): """ Do we handle bad code safely? """ code = "" func = LocalFunctionInfo(code=code) self.assertEqual(func.inputs, []) self.assertEqual(func.outputs, []) self.assertFalse(func.is_valid) self.assertFalse(func.load_error) # And if we update, do all the values change as desired. func.code = "def bar(x):\n" \ " return y" self.assertEqual(func.name, "bar") inputs = [(x.name, x.default) for x in func.inputs] self.assertEqual(inputs, [('x',None)]) outputs = [x.name for x in func.outputs] self.assertEqual(outputs, ['y']) self.assertTrue(func.is_valid) self.assertFalse(func.load_error)
def test_empty(self): """ Do we handle bad code safely? """ code = "" func = LocalFunctionInfo(code=code) self.assertEqual(func.inputs, []) self.assertEqual(func.outputs, []) self.assertFalse(func.is_valid) self.assertFalse(func.load_error) # And if we update, do all the values change as desired. func.code = "def bar(x):\n" \ " return y" self.assertEqual(func.name, "bar") inputs = [(x.name, x.default) for x in func.inputs] self.assertEqual(inputs, [('x', None)]) outputs = [x.name for x in func.outputs] self.assertEqual(outputs, ['y']) self.assertTrue(func.is_valid) self.assertFalse(func.load_error)
self._uuid_list.append(statement.uuid) return self._uuid_list if __name__ == '__main__': from blockcanvas.function_tools.local_function_info import LocalFunctionInfo f1code = "def foo(x=3, y=4):\n" \ " z = x + y\n" \ " return z\n" f2code = "def bar(k='ciao'):\n" \ " print 'K=%s' % k\n" f1 = LocalFunctionInfo(code=f1code) f2 = LocalFunctionInfo(code=f2code) F1 = FunctionCall.from_callable_object(f1) F2 = FunctionCall.from_callable_object(f2) group_type = 'for1' lsp = GroupSpec(type=group_type) node = FunctionCallGroup(lsp, statements=[F1, F2], gname='test_group') print "CODE:\n" print node.code print "\nUUID:%s\n" % node.uuid print "\nUUID_LIST:\n" for u in node.uuid_list: print 'uuid:%s\n' % u
def add_function_object_to_model(self, item, x=None, y=None): """ Add the double clicked or dropped FunctionCall object to the code/canvas. The added function is also marked as selected. If "New Function" is clicked, we hand back a FunctionCall based on a LocalFunctionInfo. Otherwise, we hand back a FunctionCall based on a PythonFunctionInfo. # fixme: We need to add LocalFunctionInfo objects to the # FunctionLibrary somehow. """ # Prevent the execution of the code when adding a new block. self.project.active_experiment.exec_model.allow_execute = False group = False if item == NEW_EXPR_ENTRY: node = GeneralExpression() elif item == NEW_FUNCTION_ENTRY: exp = self.project.active_experiment base_name = 'new_function' func_name = exp.exec_model.generate_unique_function_name(base_name = base_name) # This should create a LocalFunctionInfo... # fixme: Generate a unique name that isn't on the canvas. code_template = "def %(name)s(a, b):\n" \ " return x, y\n" code = code_template % {'name':func_name} # fixme: Short term for testing. Remove imports in future and # replace with FunctionCall UI. function = LocalFunctionInfo(code=code) traits_class = self.match_function_to_has_traits_class(item.name) node = FunctionCall.from_callable_object(function, traits_class, exp) elif item == NEW_LOOP_ENTRY: group = True exp = self.project.active_experiment base_name = 'group' group_name = exp.exec_model.generate_unique_function_name(base_name = base_name) selection = SelGType() is_ok = selection.configure_traits(kind='modal') if is_ok: group_type = selection.check_list[0] lsp = GroupSpec(type=group_type,active_experiment=self.project.active_experiment) node = FunctionCallGroup(lsp, gname=group_name); else: return else: function = PythonFunctionInfo(name=item.name, module=item.module) traits_class = self.match_function_to_has_traits_class(item.name) node = FunctionCall.from_callable_object(function, traits_class, self.project.active_experiment) if group: res = node.configure_traits(kind="livemodal") # FIXME: It will disappear when the creation of loops will be # properly managed ("graphically") in the canvas node.update_from_UI() its_OK = res else: # Bring up the dialog box to edit it res = node.edit_traits(kind="modal") its_OK = res.result if its_OK: t_in = time.time() self.add_function_to_execution_model(node, x, y) t_out = time.time() print '%f seconds: add func to execution model' % (t_out-t_in) self.select_function_on_canvas(node) self.project.active_experiment.exec_model.allow_execute = True return