def test_function_existing_function_can_be_unsubscribed(self):
     FunctionFactory.subscribe(TestFunctionCorrectForm)
     nfuncs_before = len(FunctionFactory.getFunctionNames())
     FunctionFactory.unsubscribe("TestFunctionCorrectForm")
     available_functions = FunctionFactory.getFunctionNames()
     self.assertEquals(nfuncs_before - 1, len(available_functions))
     self.assertTrue("TestFunctionCorrectForm" not in available_functions)
示例#2
0
 def test_function_existing_function_can_be_unsubscribed(self):
     FunctionFactory.subscribe(TestFunctionCorrectForm)
     nfuncs_before = len(FunctionFactory.getFunctionNames())
     FunctionFactory.unsubscribe("TestFunctionCorrectForm")
     available_functions = FunctionFactory.getFunctionNames()
     self.assertEquals(nfuncs_before - 1, len(available_functions))
     self.assertTrue("TestFunctionCorrectForm" not in available_functions)
 def test_function_subscription(self):
     nfuncs_orig = len(FunctionFactory.getFunctionNames())
     FunctionFactory.subscribe(TestFunction)
     new_funcs = FunctionFactory.getFunctionNames()
     self.assertEquals(nfuncs_orig+1, len(new_funcs))
     self.assertTrue("TestFunction" in new_funcs)
     
     FunctionFactory.unsubscribe("TestFunction")
     new_funcs = FunctionFactory.getFunctionNames()
     self.assertEquals(nfuncs_orig, len(new_funcs))
     self.assertTrue("TestFunction" not in new_funcs)
示例#4
0
 def test_function_subscription_without_required_attrs_fails(self):
     self.assertRaises(RuntimeError,
                       FunctionFactory.Instance().subscribe,
                       TestFunctionNoAttrs)
     self.assertTrue(
         "TestFunctionNoAttrs" not in FunctionFactory.getFunctionNames())
     self.assertRaises(RuntimeError,
                       FunctionFactory.Instance().subscribe,
                       TestFunctionOnlyInit)
     self.assertTrue(
         "TestFunctionOnlyInit" not in FunctionFactory.getFunctionNames())
示例#5
0
    def test_function_subscription(self):
        nfuncs_orig = len(FunctionFactory.getFunctionNames())
        FunctionFactory.subscribe(TestFunction)
        new_funcs = FunctionFactory.getFunctionNames()
        self.assertEquals(nfuncs_orig+1, len(new_funcs))
        self.assertTrue("TestFunction" in new_funcs)

        FunctionFactory.unsubscribe("TestFunction")
        new_funcs = FunctionFactory.getFunctionNames()
        self.assertEquals(nfuncs_orig, len(new_funcs))
        self.assertTrue("TestFunction" not in new_funcs)
示例#6
0
文件: base.py 项目: liyulun/mantid
    def skip(self):
        """
        Override and return a string depending on whether the directive
        should be skipped. If empty then the directive should be processed
        otherwise the string should contain the error message
        The default is to skip (and warn) if the algorithm is not known.

        Returns:
          str: Return error mesage string if the directive should be skipped
        """
        from mantid.api import AlgorithmFactory, FunctionFactory

        name, version = self.algorithm_name(), self.algorithm_version()
        msg = ""
        if version is None: # it is a fit function
            if name in FunctionFactory.getFunctionNames():
                return ""
            else:
                msg = "No fit function '%s', skipping directive" % name
        else:
            if AlgorithmFactory.exists(name, version):
                return ""
            else:
                msg = "No algorithm '%s' version '%d', skipping directive" % (name, version)

        # warn the user
        if len(msg) > 0:
            env = self.state.document.settings.env
            env.app.verbose(msg)
        return msg
示例#7
0
    def skip(self):
        """
        Override and return a string depending on whether the directive
        should be skipped. If empty then the directive should be processed
        otherwise the string should contain the error message
        The default is to skip (and warn) if the algorithm is not known.

        Returns:
          str: Return error message string if the directive should be skipped
        """
        from mantid.api import FunctionFactory

        name, version = self.algorithm_name(), self.algorithm_version()
        msg = ""
        if version is None:  # it is a fit function
            if name in FunctionFactory.getFunctionNames():
                return ""
            else:
                msg = "No fit function '%s', skipping directive" % name
        else:
            if AlgorithmFactory.exists(name, version):
                return ""
            else:
                msg = "No algorithm '%s' version '%d', skipping directive" % (
                    name, version)

        # warn the user
        if len(msg) > 0:
            logger = get_logger(__name__, self.state.document.settings.env.app)
            logger.verbose(msg)
        return msg
 def test_function_subscription_without_required_attrs_fails(self):
     self.assertRaises(RuntimeError, FunctionFactory.Instance().subscribe, TestFunctionNoAttrs)
     self.assertTrue("TestFunctionNoAttrs" not in FunctionFactory.getFunctionNames())
     self.assertRaises(RuntimeError, FunctionFactory.Instance().subscribe, TestFunctionOnlyInit)
     self.assertTrue("TestFunctionOnlyInit" not in FunctionFactory.getFunctionNames())
示例#9
0
 def test_function_with_expected_attrs_subscribes_successfully(self):
     nfuncs_orig = len(FunctionFactory.getFunctionNames())
     FunctionFactory.subscribe(TestFunctionCorrectForm)
     new_funcs = FunctionFactory.getFunctionNames()
     self.assertEquals(nfuncs_orig + 1, len(new_funcs))
     self.assertTrue("TestFunctionCorrectForm" in new_funcs)
示例#10
0
 def test_get_functions(self):
     all_funcs = FunctionFactory.getFunctionNames()
     self.assertTrue(len(all_funcs) > 0)
     self.assertTrue("Gaussian" in all_funcs)
示例#11
0
def _attach_wrappers(source_module):
    for name in FunctionFactory.getFunctionNames():
        # Wrap all registered functions which are not in the black list
        if name not in _do_not_wrap:
            setattr(source_module, name, _create_wrapper_function(name))
示例#12
0
def _wrappers():
    for name in FunctionFactory.getFunctionNames():
        # Wrap all registered functions which are not in the black list
        if name not in _do_not_wrap:
            yield name, _create_wrapper_function(name)
示例#13
0
 def test_function_with_expected_attrs_subscribes_successfully(self):
     nfuncs_orig = len(FunctionFactory.getFunctionNames())
     FunctionFactory.subscribe(TestFunctionCorrectForm)
     new_funcs = FunctionFactory.getFunctionNames()
     self.assertEquals(nfuncs_orig+1, len(new_funcs))
     self.assertTrue("TestFunctionCorrectForm" in new_funcs)
示例#14
0
 def test_get_functions(self):
     all_funcs = FunctionFactory.getFunctionNames()
     self.assertTrue( len(all_funcs) > 0 )
     self.assertTrue("Gaussian" in all_funcs)
示例#15
0
def _create_wrappers_for_all_fit_functions():
    for name in FunctionFactory.getFunctionNames():
        _create_wrapper_function(name)
示例#16
0
def _wrappers():
    wrappers = []
    for name in FunctionFactory.getFunctionNames():
        # Wrap all registered functions which are not in the black list
        if name not in _do_not_wrap:
            yield name, _create_wrapper_function(name)
示例#17
0

def _create_wrapper_function(name):
    """Create fake functions for the given name
       It should not be called directly
                   
       :param name: name of fake function
    """

    # ------------------------------------------------------------------------------------------------
    def wrapper_function(*args, **kwargs):
        name_to_constructor = {
            'CompositeFunction': CompositeFunctionWrapper,
            'ProductFunction': ProductFunctionWrapper,
            'Convolution': ConvolutionWrapper,
            'MultiDomainFunction': MultiDomainFunctionWrapper,
        }
        # constructor is FunctionWrapper if the name is not in the registry.
        constructor = name_to_constructor.get(name, FunctionWrapper)
        return constructor(name, *args, **kwargs)

    # ------------------------------------------------------------------------------------------------
    wrapper_function.__name__ = name
    # _replace_signature(fake_function, ("", ""))
    globals()[name] = wrapper_function


fnames = FunctionFactory.getFunctionNames()
for i, val in enumerate(fnames):
    _create_wrapper_function(val)