예제 #1
0
    def test_pickling(self):
        library = ClassLibrary(modules=[])
        library.modules = ['cp','xml','_socket','sys','os']
        old_classes = library.classes

        import cPickle
        cPickle.dump(library, open(r'test_library.pickle','wb'))
        library = cPickle.load(open(r'test_library.pickle','rb'))
        library.modules = ['cp','xml','_socket','sys','os']
        self.assertEqual(len(old_classes), len(library.classes))
        os.unlink('test_library.pickle')
예제 #2
0
    def test_pickling(self):
        library = ClassLibrary(modules=[])
        library.modules = ['cp', 'xml', '_socket', 'sys', 'os']
        old_classes = library.classes

        import cPickle
        cPickle.dump(library, open(r'test_library.pickle', 'wb'))
        library = cPickle.load(open(r'test_library.pickle', 'rb'))
        library.modules = ['cp', 'xml', '_socket', 'sys', 'os']
        self.assertEqual(len(old_classes), len(library.classes))
        os.unlink('test_library.pickle')
예제 #3
0
    def test_update_all_classes_forces_new_search(self):
        library = ClassLibrary()
        cs = ClassSearch(all_classes=library.classes)
        self.assertEqual(len(cs.search_results), 0)

        library.modules = ['datetime', 'sample_package']
        # Ensure we found some classes.  This isn't a test of
        # ClassSearch, but we want it to be true to ensure our
        # test is valid.
        self.assertNotEqual(len(library.classes), 0)

        # Now ensure the search results are updated and have something
        # in them.
        cs.all_classes = library.classes
        self.assertNotEqual(len(cs.search_results), 0)
예제 #4
0
    def test_update_all_classes_forces_new_search(self):
        library = ClassLibrary()
        cs = ClassSearch(all_classes=library.classes)
        self.assertEqual(len(cs.search_results), 0)

        library.modules=['datetime', 'sample_package']
        # Ensure we found some classes.  This isn't a test of
        # ClassSearch, but we want it to be true to ensure our
        # test is valid.
        self.assertNotEqual(len(library.classes), 0)

        # Now ensure the search results are updated and have something
        # in them.
        cs.all_classes = library.classes
        self.assertNotEqual(len(cs.search_results), 0)
예제 #5
0
 def setUp(self):
     # Add this directory to sys.path.
     self.dir = os.path.abspath(os.path.dirname(__file__))
     sys.path.append(self.dir)
     library = ClassLibrary(modules=['datetime', 'sample_package'])
     self.cs = ClassSearch(all_classes=library.classes)
     unittest.TestCase.setUp(self)
예제 #6
0
    def test_xml_timings(self):
        """Test the time it takes to import the xml module, not applicable
        if xml not present"""

        try:
            import xml
        except:
            # Skip this test if cp not available
            import nose
            raise nose.SkipTest()

        import time
        t1 = time.clock()
        library = ClassLibrary(modules=[])

        library.modules.append('xml')
        t2 = time.clock()
        self.assertEqual(t2 - t1 < 2., True)
예제 #7
0
    def trait_view(self, view):
        """
        This implementation should go away immediately when groups are managed
        "graphically". A possible way to create a new "world" where the 
        execution is still serial is to create a new instance of LoopApp 
        that shares with the "main" one all the basic data 
        (contexts, class_library, func_library, func_search) but not the 
        same experiment. 
        """

        # To avoid circular imports
        from blockcanvas.app.app import Application

        code = None
        app_exist = False

        if hasattr(scripting, 'app'):

            app_exist = True

            # If the methods is called by another app that wants to add a group,
            # retrieve it from the scripting module recursively.
            app = scripting.app

            # Save the current reference in the scripting module
            if not hasattr(scripting, 'app_tree'):
                scripting.app_tree = []

            scripting.app_tree.append(app)

            class_library = app.class_library
            func_library = app.function_library
            func_search = app.function_search

        else:
            # It is more or less a test if the method works well so the module
            # is chosen by hand.
            # It should never happen that this method is called outside an
            # instance of the BlockCanvas app.
            modules = ['os']
            class_library = ClassLibrary(modules=modules)
            func_library = FunctionLibrary(modules=modules)
            func_search = FunctionSearch(all_functions=func_library.functions)

        # TODO: This new object overwrite the scripting.app reference so there
        # should be a way to call a "fix_scripting_app" method when this view
        # is closed. FIXED: Now this step is done by update_from_UI
        self.function_view_instance = Application(
            code=code,
            class_library=class_library,
            function_library=func_library,
            function_search=func_search)

        if app_exist:
            # Share the context with the Parent app if it exist
            self.function_view_instance.project.active_experiment.context.subcontext = \
            app.project.active_experiment.context.subcontext

            # During the editing of the group fired events due to change in the context
            # are deferred now and completely cleaned after the group creation to
            # avoid useless execution.
            # Those parameters must be fixed after the group is Created/Updated.
            # This step is done by "update_from_UI" method
            self._prev_defer_execution = app.project.active_experiment.context.defer_execution
            app.project.active_experiment.context.defer_execution = True

            # To save some time is also nice to stop the execution in the calling app due to
            # events fired every time a statement is added to the execution model.
            # It must be noticed that allow_execute must be saved instead of just put it to
            # False, to properly manage nested groups/loops.
            self._prev_allow_execute = app.project.active_experiment.exec_model.allow_execute
            app.project.active_experiment.exec_model.allow_execute = False

        # No execution in the sub_app during the editing of the group
        self.function_view_instance.project.active_experiment.exec_model.allow_execute = False

        # Add current element/s to the view
        if self.gfunc is not None:
            for curr_elem in self.gfunc.curr_elemts:
                self.function_view_instance.add_function_to_execution_model(
                    curr_elem, x=None, y=None)

        # When reopening an existing group add its statements
        for stmt in self.group_statements:
            self.function_view_instance.add_function_to_execution_model(stmt,
                                                                        x=None,
                                                                        y=None)

        return create_view(model=self.function_view_instance)
예제 #8
0
       "    return z\n" \
       "def bar():\n" \
       "    pass\n" \
       "c = add(2, 3)\n" \
       "d = mul(2, 2)\n" \
       "e = mul(5, 3)\n" \
       "f = add(4, 2)\n" \
       "g = foo(2)\n"

    
    # Enable logging
    import logging
    logging.getLogger().addHandler(logging.StreamHandler())
    logging.getLogger().setLevel(logging.DEBUG)

    modules = ['os']

    class_library = ClassLibrary(modules=modules)
    func_library = FunctionLibrary(modules=modules)
    func_search = FunctionSearch(all_functions=func_library.functions)

    app = Application(
        code=code,
        data_context=DataContext(name='data'),
        class_library=class_library,
        function_library=func_library,
        function_search=func_search,
        )

    app.configure_traits()