def test_convert_function_proper_docstring(self):
     html1 = get_html_from_function_info(PythonFunctionInfo.from_function(proper_func1))
     html2 = get_html_from_function_info(PythonFunctionInfo.from_function(proper_func2))
     # If [unit] is in the html, it has properly identified by
     # convert_function
     self.assertNotEqual(html1.find('[km/s]'), -1)
     self.assertNotEqual(html2.find('[g/cc]'), -1)
Beispiel #2
0
 def test_convert_function_proper_docstring(self):
     html1 = get_html_from_function_info(
         PythonFunctionInfo.from_function(proper_func1))
     html2 = get_html_from_function_info(
         PythonFunctionInfo.from_function(proper_func2))
     # If [unit] is in the html, it has properly identified by
     # convert_function
     self.assertNotEqual(html1.find('[km/s]'), -1)
     self.assertNotEqual(html2.find('[g/cc]'), -1)
Beispiel #3
0
    def test_code(self):
        func = PythonFunctionInfo.from_function(simple)
        correct_code = """def simple(a,b):
    x,y=a,b
    return x,y
"""
        self.assertEqual(func.code, correct_code)
    def test_code(self):
        func = PythonFunctionInfo.from_function(simple)
        correct_code = """def simple(a,b):
    x,y=a,b
    return x,y
"""
        self.assertEqual(func.code, correct_code)
 def test_platform_specific(self):
     html = get_html_from_function_info(PythonFunctionInfo.from_function(proper_func1))
     p = platform.system()
     if p == 'Windows':
         self.assertNotEqual(html.find('<style'), -1)
     else:
         self.assertEqual(html.find('<style'), -1)
Beispiel #6
0
 def test_platform_specific(self):
     html = get_html_from_function_info(
         PythonFunctionInfo.from_function(proper_func1))
     p = platform.system()
     if p == 'Windows':
         self.assertNotEqual(html.find('<style'), -1)
     else:
         self.assertEqual(html.find('<style'), -1)
Beispiel #7
0
    def test_change_library_name(self):
        func = PythonFunctionInfo.from_function(simple)

        self.assertEqual(func.name, 'simple')
        self.assertEqual(func.library_name, 'simple')

        func.library_name = 'foo'
        self.assertEqual(func.name, 'simple')
        self.assertEqual(func.library_name, 'foo')
    def test_change_library_name(self):
        func = PythonFunctionInfo.from_function(simple)

        self.assertEqual(func.name, 'simple')
        self.assertEqual(func.library_name, 'simple')

        func.library_name = 'foo'
        self.assertEqual(func.name, 'simple')
        self.assertEqual(func.library_name, 'foo')
Beispiel #9
0
    def test_with_defaults_none(self):
        func = PythonFunctionInfo.from_function(with_defaults_none)

        # Check the inputs.
        inputs = func.inputs
        desired = (('a', None), ('b', 'None'))
        self._check_input(inputs, desired)

        # Now check the output names.
        outputs = func.outputs
        desired = ('x', 'y')
        self._check_output(outputs, desired)
    def test_with_defaults_none(self):
        func = PythonFunctionInfo.from_function(with_defaults_none)

        # Check the inputs.
        inputs = func.inputs
        desired = (('a', None), ('b', 'None'))
        self._check_input(inputs, desired)

        # Now check the output names.
        outputs = func.outputs
        desired = ('x','y')
        self._check_output(outputs, desired)
Beispiel #11
0
    def test_parse_error(self):
        # Because having a function checked in with a parse error creates problems
        # with the egg builder, we take an existing file and modify it.
        old_module = 'blockcanvas.function_tools.tests.sample_package.error_package'
        old_filename = get_module_path(old_module)
        new_filename = old_filename[:-3] + '2.py'
        new_module = old_module + '2'
        lines = open(old_filename).readlines()
        # Strip off the colon on the end of the second line to create a syntax error
        lines[1] = lines[1][:-1]
        open(new_filename, 'w').writelines(lines)

        func = PythonFunctionInfo(module=new_module, name='badfunction')
        self.assertEqual(func.load_error,
                         "failed to parse module '%s'" % new_module)
        os.unlink(new_filename)
Beispiel #12
0
 def test_convert_function_bad_docstring(self):
     html = get_html_from_function_info(
         PythonFunctionInfo.from_function(bad_func1))
     self.assertEqual(html.find('Parameters'), -1)
Beispiel #13
0
 def test_empty(self):
     """ Can we handle a function without any inputs/outputs?
     """
     func = PythonFunctionInfo.from_function(empty)
     self.assertEqual(func.doc_string, "")
 def test_empty(self):
     """ Can we handle a function without any inputs/outputs?
     """
     func = PythonFunctionInfo.from_function(empty)
     self.assertEqual(func.doc_string, "")
 def test_convert_function_bad_docstring(self):
     html = get_html_from_function_info(PythonFunctionInfo.from_function(bad_func1))
     self.assertEqual(html.find('Parameters'), -1)
Beispiel #16
0
    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