def test_adaptation(self): """ adaptation """ class IFoo(Interface): pass class Foo(HasTraits): pass @provides(IFoo) class FooToIFooAdapter(Adapter): pass register_factory(FooToIFooAdapter, Foo, IFoo) f = Foo() # Make sure adaptation works. Adaptation via direct instantiation of # Interface classes is deprecated, so suppress the warning. with warnings.catch_warnings(record=True) as warn_msgs: warnings.simplefilter("always", DeprecationWarning) i_foo = IFoo(f) self.assertNotEqual(None, i_foo) self.assertEqual(FooToIFooAdapter, type(i_foo)) self.assertEqual(len(warn_msgs), 1) warn_msg = warn_msgs[0] self.assertIn('use "adapt(adaptee, protocol)" instead', str(warn_msg.message)) self.assertIn("test_interface_checker", warn_msg.filename)
def setUp(self): reset_global_adaptation_manager() # Register adapters. register_factory(SampleListAdapter, Sample, IList) register_factory(ListAverageAdapter, IList, IAverage) register_factory(SampleFooAdapter, Sample, IFoo) register_factory(FooPlusAdapter, IFoo, IFooPlus)
def test_list_trait_automatic_adaptation(self): bar = Bar() foo_container = FooContainer() # Before a Bar->Foo adapter is registered. with self.assertRaises(TraitError): foo_container.not_adapting_foo_list = [bar] with self.assertRaises(TraitError): foo_container.adapting_foo_list = [bar] # After a Bar->Foo adapter is registered. register_factory(bar_to_foo_adapter, Bar, Foo) with self.assertRaises(TraitError): foo_container.not_adapting_foo_list = [bar] foo_container.adapting_foo_list = [bar] self.assertIsInstance(foo_container.adapting_foo_list[0], Foo)
def test_adaptation(self): """ adaptation """ class IFoo(Interface): pass class Foo(HasTraits): pass @provides(IFoo) class FooToIFooAdapter(Adapter): pass register_factory(FooToIFooAdapter, Foo, IFoo) f = Foo() # Make sure adaptation works. i_foo = IFoo(f) self.assertNotEqual(None, i_foo) self.assertEqual(FooToIFooAdapter, type(i_foo)) return
Item('root', editor=TreeEditor(editable=False, auto_open=1) ), show_labels=False ), width=0.33, height=0.50, resizable=True ) #-- Traits Default Value Methods ----------------------------------------- def _root_path_default(self): return getcwd() #-- Property Implementations --------------------------------------------- @property_depends_on('root_path') def _get_root(self): return File(path=self.root_path) #-- Create and run the demo ---------------------------------------------- register_factory(FileAdapter, File, ITreeNode) demo = FileTreeDemo() # Run the demo (if invoked form the command line): if __name__ == '__main__': demo.configure_traits()
def compute_step(self, point, time): x, y, z = point return array([ self.prandtl * (y - x), x * (self.rayleigh - z) - y, x * y - self.beta * z ]) ########################################################################### # Protected interface. ########################################################################### @cached_property def _get_points(self): return odeint(self.compute_step, self.initial_point, self.times) @cached_property def _get_times(self): return arange(self.time_start, self.time_stop, self.time_step) @provides(IPlottable2d) class LorenzIPlottable2dAdapter(Adapter, IModel3dIPlottable2dMixin): adaptee = Instance(Lorenz) plot_type = Str('line') register_factory(LorenzIPlottable2dAdapter, Lorenz, IPlottable2d)
########################################################################### # 'Rossler' interface. ########################################################################### def compute_step(self, point, time): x, y, z = point return array([-y - z, x + self.a * y, self.b + z * (x - self.c)]) ########################################################################### # Protected interface. ########################################################################### @cached_property def _get_points(self): return odeint(self.compute_step, self.initial_point, self.times) @cached_property def _get_times(self): return arange(self.time_start, self.time_stop, self.time_step) @provides(IPlottable2d) class RosslerIPlottable2dAdapter(Adapter, IModel3dIPlottable2dMixin): adaptee = Instance(Rossler) plot_type = Str("line") register_factory(RosslerIPlottable2dAdapter, Rossler, IPlottable2d)
class EmptyProject(Project): pass class EmptyProjectAdapter(ITreeNodeAdapter): """ Adapter for our EmptyProject. """ #-- ITreeNodeAdapter Method Overrides -------------------------------------- def get_label(self): """ Gets the label to display for a specified object. """ return 'No project loaded.' register_factory(EmptyProjectAdapter, EmptyProject, ITreeNode) class ProjectAdapter(ITreeNodeAdapter): """ Base ProjectAdapter for the root of the tree. """ #-- ITreeNodeAdapter Method Overrides -------------------------------------- def allows_children(self): """ Returns whether this object can have children. """ return False def has_children(self): """ Returns whether the object has children. """
def register_all_data_views(): """ Register all ModelView classes for all data models. """ register_factory(StudyView, Study, ModelView) register_factory(ResinView, Resin, ModelView) register_factory(ComponentView, Component, ModelView) register_factory(ColumnView, Column, ModelView) register_factory(ColumnTypeView, ColumnType, ModelView) register_factory(ChemicalView, Chemical, ModelView) register_factory(ProductView, Product, ModelView) register_factory(GeneralRateModelView, GeneralRateModel, ModelView) register_factory(SystemView, System, ModelView) register_factory(SolverView, Solver, ModelView) register_factory(DiscretizationView, Discretization, ModelView) register_factory(SystemView, System, ModelView) register_factory(SystemTypeView, SystemType, ModelView) register_factory(StericMassActionModelView, StericMassAction, ModelView) register_factory(PhDependentStericMassActionModelView, PhDependentStericMassAction, ModelView) register_factory(ExternalLangmuirView, ExternalLangmuir, ModelView) register_factory(LangmuirView, Langmuir, ModelView) register_factory(MethodModelView, Method, ModelView) register_factory(BufferView, Buffer, ModelView) register_factory(SolutionWithProductView, SolutionWithProduct, ModelView) register_factory(SimulationGroupView, SimulationGroup, ModelView) register_factory(SimulationView, Simulation, ModelView) register_factory(BruteForceOptimizerView, BruteForceOptimizer, ModelView) register_factory(BruteForceOptimizerStepView, BruteForceOptimizerStep, ModelView) register_factory(ProductComponentView, ProductComponent, ModelView)
object = self.object return object.s1 + object.s2 + object.s3 class FooPlusAdapter(object): def __init__(self, obj): self.obj = obj def get_foo(self): return self.obj.get_foo() def get_foo_plus(self): return self.obj.get_foo() + 1 register_factory(SampleListAdapter, Sample, IList) register_factory(ListAverageAdapter, IList, IAverage) register_factory(SampleFooAdapter, Sample, IFoo) register_factory(FooPlusAdapter, IFoo, IFooPlus) # ------------------------------------------------------------------------------- # 'InterfacesTest' unit test class: # ------------------------------------------------------------------------------- class InterfacesTest(unittest.TestCase): # --------------------------------------------------------------------------- # Individual unit test methods: # ---------------------------------------------------------------------------
########################################################################### # 'Rossler' interface. ########################################################################### def compute_step(self, point, time): x, y, z = point return array([ -y - z, x + self.a * y, self.b + z * (x - self.c) ]) ########################################################################### # Protected interface. ########################################################################### @cached_property def _get_points(self): return odeint(self.compute_step, self.initial_point, self.times) @cached_property def _get_times(self): return arange(self.time_start, self.time_stop, self.time_step) @provides(IPlottable2d) class RosslerIPlottable2dAdapter(Adapter, IModel3dIPlottable2dMixin): adaptee = Instance(Rossler) plot_type = Str('line') register_factory(RosslerIPlottable2dAdapter, Rossler, IPlottable2d)
########################################################################### def compute_step(self, point, time): x, y, z = point return array([ self.prandtl * (y - x), x * (self.rayleigh - z) - y, x * y - self.beta * z ]) ########################################################################### # Protected interface. ########################################################################### @cached_property def _get_points(self): return odeint(self.compute_step, self.initial_point, self.times) @cached_property def _get_times(self): return arange(self.time_start, self.time_stop, self.time_step) @provides(IPlottable2d) class LorenzIPlottable2dAdapter(Adapter, IModel3dIPlottable2dMixin): adaptee = Instance(Lorenz) plot_type = Str('line') register_factory(LorenzIPlottable2dAdapter, Lorenz, IPlottable2d)
def register_all_tree_node_adapters(): """ Register in Traits all ITreeNode adapters so that a DataManager can be displayed automatically using a TreeEditor. """ register_factory(BaseChromatographyDataToITreeNode, ChromatographyData, ITreeNode) register_factory(DataManagerToITreeNode, DataManager, ITreeNode) register_factory(StudyToITreeNode, Study, ITreeNode) register_factory(SimpleDataSourceToITreeNode, SimpleDataSource, ITreeNode) register_factory(DataElementToITreeNode, DataElement, ITreeNode) register_factory(TraitListObjectToTreeNode, TraitListObject, ITreeNode) register_factory(TraitDictObjectToTreeNode, TraitDictObject, ITreeNode) register_factory(SimulationToITreeNode, Simulation, ITreeNode) register_factory(SimulationGroupToITreeNode, SimulationGroup, ITreeNode) register_factory(StudyAnalysisToolsToITreeNode, StudyAnalysisTools, ITreeNode) register_factory(BruteForceBindingModelOptimizerToITreeNode, BruteForce2StepBindingModelOptimizer, ITreeNode) register_factory(ExperimentOptimizerToITreeNode, ExperimentOptimizer, ITreeNode) register_factory(ExperimentOptimizerStepToITreeNode, ExperimentOptimizerStep, ITreeNode)
def get_foo ( self ): object = self.object return (object.s1 + object.s2 + object.s3) class FooPlusAdapter ( object ): def __init__ ( self, obj ): self.obj = obj def get_foo ( self ): return self.obj.get_foo() def get_foo_plus ( self ): return (self.obj.get_foo() + 1) register_factory(SampleListAdapter, Sample, IList) register_factory(ListAverageAdapter, IList, IAverage) register_factory(SampleFooAdapter, Sample, IFoo) register_factory(FooPlusAdapter, IFoo, IFooPlus) #------------------------------------------------------------------------------- # 'InterfacesTest' unit test class: #------------------------------------------------------------------------------- class InterfacesTest ( unittest.TestCase ): #--------------------------------------------------------------------------- # Individual unit test methods: #--------------------------------------------------------------------------- def test_provides_none ( self ):