def setUp(self): # Add this directory to sys.path. self.dir = os.path.abspath(os.path.dirname(__file__)) sys.path.append(self.dir) library = FunctionLibrary(modules=['os', 'sample_package']) self.fs = FunctionSearch(all_functions=library.functions) unittest.TestCase.setUp(self)
def test_changing_library_initializes_function_searh(): library = FunctionLibrary(modules=['os']) b = Application() b.function_library = library assert_equal(b.function_search.all_functions, library.functions)
def test_changing_library_functions_initializes_function_searh(): library = FunctionLibrary(modules=['os']) b = Application() b.function_library = library assert_equal(b.function_search.all_functions, library.functions) # This forces the library to recalculate its functions. library.modules = ['os', 'telnetlib'] assert_equal(b.function_search.all_functions, library.functions)
def test_pickling(self): library = FunctionLibrary(modules=[]) library.modules = ['cp', 'xml', '_socket', 'sys', 'os'] old_functions = library.functions 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_functions), len(library.functions)) os.unlink('test_library.pickle')
def test_appending_library_functions_initializes_function_searh(): library = FunctionLibrary(modules=['os']) b = Application() b.function_library = library assert_equal(b.function_search.all_functions, library.functions) # This is sorta cheating, but add an item to the function list # and ensure that we are updating. library.functions.append(MinimalFunctionInfo()) assert_equal(b.function_search.all_functions, library.functions)
def test_initializing_with_library_initializes_function_searh(): library = FunctionLibrary(modules=['os']) b = Application(function_library=library) raise nose.SkipTest # FIXME: # The assertion below is failing because in # enthought/block_canvas/app/app.py line 541 # post_init is set to True, which means that function_library # is not updated upon initialization. The post_init change # was made in a number of locations in this file at changeset 19002. assert_equal(b.function_search.all_functions, library.functions)
def test_update_all_functions_forces_new_search(self): library = FunctionLibrary() fs = FunctionSearch(all_functions=library.functions) self.assertEqual(len(fs.search_results), 0) library.modules = ['os', 'sample_package'] # Ensure we found some functions. This isn't a test of # FunctionSearch, but we want it to be true to ensure our # test is valid. self.assertNotEqual(len(library.functions), 0) # Now ensure the search results are updated and have something # in them. fs.all_functions = library.functions self.assertNotEqual(len(fs.search_results), 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 = FunctionLibrary(modules=[]) library.modules.append('xml') t2 = time.clock() self.assertEqual(t2 - t1 < 2., True)
), VGroup( Label('Function name filters'), Item('name_filters', show_label=False), Label('Module/Package name filters'), Item('module_filters', show_label=False), ), HGroup( Item('search_name'), Item('search_module'), ), title='Function Preferences', width=300, height=400, resizable=True, buttons=OKCancelButtons, handler=AppFunctionSearchPreferencesUIHandler, ) if __name__ == "__main__": from blockcanvas.function_tools.function_library import FunctionLibrary from blockcanvas.app import app library = FunctionLibrary(modules=['os','cp.rockphysics']) this_app = app.Application(function_library=library) # Now create the UI. this_app.function_search.edit_traits(function_search_preferences_view)
def _app_default(self): """ Create an application with a minimal library in it... """ function_library = FunctionLibrary(modules=['os']) return Application(function_library=function_library)
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)
### Trait handlers ####################################################### def _context_items_modified(self, event): """ Trigger the update on the context UI. Since the events from the lower levels of the wrapped contexts get vetoed by their wrappers, and since we only want to look down at the lower level instead of looking at the functions, too, we do this instead of making the editor just listen to the changes. """ self.context_viewer.update_variables() if __name__ == '__main__': code = "from blockcanvas.debug.my_operator import add, mul\n" \ "def foo(x, y=3):\n" \ " z = x + y\n" \ " 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" library = FunctionLibrary(modules=['os']) app = Application(code=code, function_library=library) app.configure_traits()
" 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()