-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refs #8588. move algorithm_decorator into own module.
- Loading branch information
1 parent
82332bf
commit c1e1a2f
Showing
3 changed files
with
63 additions
and
51 deletions.
There are no files selected for viewing
52 changes: 1 addition & 51 deletions
52
...Framework/PythonInterface/test/python/plugins/algorithms/ReflectometryReductionOneTest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
set ( PY_FILES | ||
__init__.py | ||
algorithm_decorator.py | ||
) | ||
|
||
# Copy python files to output directory | ||
|
61 changes: 61 additions & 0 deletions
61
Code/Mantid/Framework/PythonInterface/test/testhelpers/algorithm_decorator.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import inspect | ||
import re | ||
|
||
def make_decorator(algorithm_to_decorate): | ||
""" | ||
Dynamically create a builder pattern style decorator around a Mantid algorithm. | ||
This allows you to separate out setting algorithm parameters from the actual method execution. Parameters may be reset multiple times. | ||
Usage: | ||
rebin = make_decorator(Rebin) | ||
rebin.set_Params([0, 0.1, 1]) | ||
.... | ||
rebin.execute() | ||
Arguments: | ||
algorithm_to_decorate: The mantid.simpleapi algorithm to decorate. | ||
""" | ||
|
||
class Decorator(object): | ||
|
||
def __init__(self, alg_subject): | ||
self.__alg_subject = alg_subject | ||
self.__parameters__ = dict() | ||
|
||
def execute(self, additional=None, verbose=False): | ||
if verbose: | ||
print "Algorithm Parameters:" | ||
print self.__parameters__ | ||
out = self.__alg_subject(**self.__parameters__) | ||
return out | ||
|
||
def set_additional(self, additional): | ||
self.__parameters__.update(**additional) | ||
|
||
def add_getter_setter(type, name): | ||
|
||
def setter(self, x): | ||
self.__parameters__[name] = x | ||
|
||
def getter(self): | ||
return self.__parameters__[name] | ||
|
||
setattr(type, "set_" + name, setter) | ||
setattr(type, "get_" + name, getter) | ||
|
||
|
||
argspec = inspect.getargspec(algorithm_to_decorate) | ||
for parameter in argspec.varargs.split(','): | ||
m = re.search('(^\w+)', parameter) # Take the parameter key part from the defaults given as 'key=value' | ||
if m: | ||
parameter = m.group(0).strip() | ||
m = re.search('\w+$', parameter) # strip off any leading numerical values produced by argspec | ||
if m: | ||
parameter = m.group(0).strip() | ||
add_getter_setter(Decorator, m.group(0).strip()) | ||
|
||
return Decorator(algorithm_to_decorate) |