def add_celem(self,var_name,active_experiment): """ Add a new output to the gfunc specification. The new celem is named as the first available name in the context with var_name root. Each celem is an instance of GeneralExpression to create it in a way that can be added "as is" to the execution model. """ o_var = OutputVariable() o_var.name = var_name o_var.name = find_next_binding(o_var,active_experiment) c_el = GeneralExpression(code=o_var.name) c_el.outputs = [o_var] self.curr_elemts.append(c_el)
def test_init(): ge = GeneralExpression(code='a = 10') assert hasattr(ge, 'inputs') assert ge.inputs == [] assert len(ge.outputs) == 1 assert ge.outputs[0].binding == 'a' assert hasattr(ge, 'uuid') assert hasattr(ge, 'call_signature') assert ge.call_signature == "a = 10" assert ge.code == "a = 10"
def collect_gen_expr(statement_info): expressions = [] # Join all of the constant expressions into a single GeneralExpression. constant_names = [] lines = [] for names, node in statement_info.constant_expressions: lines.append(unparse(node)) # Inform the statement of the variables it is writing to. constant_names.extend(names) if lines: ge = GeneralExpression() ge.code = ''.join(lines) expressions.append(ge) for node in statement_info.general_assignments: ge = GeneralExpression() ge.code = unparse(node) # Now check this expression for any inputs that are provided by the # import statements we had. If so, add that import statement to the # object and remove that input. imported_inputs = [] for iv in ge.inputs: node = statement_info.import_statements.get(iv.binding, None) if node is not None: ge.import_statements.append(unparse(node)) imported_inputs.append(iv) for iv in imported_inputs: ge.inputs.remove(iv) expressions.append(ge) return expressions
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 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 test_update_code_error(): ge = GeneralExpression() assert_raises(SyntaxError, ge.set, code="not even valid Python")