def test_update_output_binding(self): gfunc = GroupSpec(type='for1') func_call_group = FunctionCallGroup(gfunc=gfunc, statements=self.statements) func_call_group.outputs[0].binding = 'k' func_call1 = self.statements[0] self.assertEqual(func_call1.outputs[0].binding, 'k')
def test_for_tuple_celem(self): gfunc = GroupSpec(type='for2') func_call_group = FunctionCallGroup(gfunc=gfunc, statements=self.statements) desired_call_signature = """for (tuple_x,tuple_y) in iterable: x, y = simple(a, b) x, y = with_defaults(a, b=b)""" self.assertEqual(func_call_group.call_signature, desired_call_signature)
def test_for_single_celem_enum_iterable(self): gfunc = GroupSpec(type='for1f') func_call_group = FunctionCallGroup(gfunc=gfunc, statements=self.statements) desired_call_signature = """for (i,celem) in enumerate(iterable): x, y = simple(a, b) x, y = with_defaults(a, b=b)""" self.assertEqual(func_call_group.call_signature, desired_call_signature)
def test_plain(self): gfunc = GroupSpec(type='plain') func_call_group = FunctionCallGroup(gfunc=gfunc, statements=self.statements) desired_call_signature = """ x, y = simple(a, b) x, y = with_defaults(a, b=b)""" self.assertEqual(func_call_group.call_signature, desired_call_signature)
def test_nested_groups(self): statements = [] gfunc = GroupSpec(type='for1') func_call_group = FunctionCallGroup(gfunc=gfunc, statements=self.statements) statements.extend(self.statements) statements.append(func_call_group) gfunc = GroupSpec(type='for2') func_call_group2 = FunctionCallGroup(gfunc=gfunc, statements=statements) desired_call_signature = """for (tuple_x,tuple_y) in iterable: x, y = simple(a, b) x, y = with_defaults(a, b=b) for celem in iterable: x, y = simple(a, b) x, y = with_defaults(a, b=b)""" self.assertEqual(func_call_group2.call_signature, desired_call_signature)
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
def handle_group(group_struct,info): gfunc = GroupSpec.from_ast(group_struct['type'],group_struct['gfunc'],None) statements = parse_stmts_info(group_struct['stmts'],info) return FunctionCallGroup(gfunc,statements=statements)