Skip to content

Commit 133835b

Browse files
committed
Simplify redefinition of user envelopes or baselines.
Add the `replace` argument to makePDFBaseline and makePDFEnvelope. Also use more informative name for the factory built classes.
1 parent 7b28cc8 commit 133835b

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

Diff for: diffpy/srreal/pdfbaseline.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def _baseline_reduce_with_state(self):
6868

6969
# Python functions wrapper
7070

71-
def makePDFBaseline(name, fnc, **dbattrs):
71+
def makePDFBaseline(name, fnc, replace=False, **dbattrs):
7272
'''Helper function for registering Python function as a PDFBaseline.
7373
This is required for using Python function as PDFCalculator.baseline.
7474
@@ -79,6 +79,9 @@ def makePDFBaseline(name, fnc, **dbattrs):
7979
float parameters. The parameters need to be registered as
8080
double attributes in the functor class. The function fnc
8181
must be picklable and it must return a float.
82+
replace -- when set replace any PDFBaseline type already registered
83+
under the name. Otherwise raise RuntimeError when the
84+
name is taken.
8285
dbattrs -- optional float parameters of the wrapped function.
8386
These will be registered as double attributes in the
8487
functor class. The wrapped function must be callable as
@@ -102,7 +105,9 @@ def fshiftedline(x, aline, bline):
102105
# or pdfc.baseline = "shiftedline"
103106
'''
104107
from diffpy.srreal.wraputils import _wrapAsRegisteredUnaryFunction
105-
return _wrapAsRegisteredUnaryFunction(PDFBaseline, name, fnc, **dbattrs)
108+
rv = _wrapAsRegisteredUnaryFunction(PDFBaseline, name, fnc,
109+
replace=replace, **dbattrs)
110+
return rv
106111

107112
# Import delayed tweaks of the extension classes.
108113

Diff for: diffpy/srreal/pdfenvelope.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def _envelope_reduce_with_state(self):
8686

8787
# Python functions wrapper
8888

89-
def makePDFEnvelope(name, fnc, **dbattrs):
89+
def makePDFEnvelope(name, fnc, replace=False, **dbattrs):
9090
'''Helper function for registering Python function as a PDFEnvelope.
9191
This is required for using Python function as PDFCalculator envelope.
9292
@@ -97,6 +97,9 @@ def makePDFEnvelope(name, fnc, **dbattrs):
9797
float parameters. The parameters need to be registered as
9898
double attributes in the functor class. The function fnc
9999
must be picklable and it must return a float.
100+
replace -- when set replace any PDFEnvelope type already registered
101+
under the name. Otherwise raise RuntimeError when the
102+
name is taken.
100103
dbattrs -- optional float parameters of the wrapped function.
101104
These will be registered as double attributes in the
102105
functor class. The wrapped function must be callable as
@@ -121,7 +124,9 @@ def fexpdecay(x, expscale, exptail):
121124
# or pdfc.addEnvelope("expdecay")
122125
'''
123126
from diffpy.srreal.wraputils import _wrapAsRegisteredUnaryFunction
124-
return _wrapAsRegisteredUnaryFunction(PDFEnvelope, name, fnc, **dbattrs)
127+
rv = _wrapAsRegisteredUnaryFunction(PDFEnvelope, name, fnc,
128+
replace=replace, **dbattrs)
129+
return rv
125130

126131
# Import delayed tweaks of the extension classes.
127132

Diff for: diffpy/srreal/tests/testpdfbaseline.py

+10
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ def test_makePDFBaseline(self):
200200
parabola_baseline, a=1, b=2, c=3)
201201
self.assertRaises(RuntimeError, makePDFBaseline, 'parabolabaseline',
202202
parabola_baseline, a=1, b=2, c=3)
203+
# check replacement of an existing type.
204+
makePDFBaseline('linear', parabola_baseline, replace=True,
205+
a=1, b=2, c=4)
206+
pbl4 = PDFBaseline.createByType('linear')
207+
self.assertEqual(set(('a', 'b', 'c')), pbl4._namesOfDoubleAttributes())
208+
self.assertEqual(4, pbl4.c)
209+
# check baseline with no attributes
210+
pbl5 = makePDFBaseline('myzero', lambda x: 0.0)
211+
self.assertEqual(0, pbl5(33))
212+
self.assertEqual(set(), pbl5._namesOfDoubleAttributes())
203213
return
204214

205215
# End of class TestPDFBaseline

Diff for: diffpy/srreal/wraputils.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def setattrFromKeywordArguments(obj, **kwargs):
5858
return
5959

6060

61-
def _wrapAsRegisteredUnaryFunction(cls, regname, fnc, **dbattrs):
61+
def _wrapAsRegisteredUnaryFunction(cls, regname, fnc, replace=False, **dbattrs):
6262
'''Helper function for wrapping Python function as PDFBaseline or
6363
PDFEnvelope functor. Not intended for direct usage, this function
6464
is rather called from makePDFBaseline or makePDFEnvelope wrappers.
@@ -71,6 +71,9 @@ def _wrapAsRegisteredUnaryFunction(cls, regname, fnc, **dbattrs):
7171
float parameters. The parameters need to be registered as
7272
dbattrs in the functor class. The function fnc
7373
must be picklable and it must return a float.
74+
replace -- when set replace any functor already registered under
75+
the regname. Otherwise raise RuntimeError when regname
76+
is taken.
7477
dbattrs -- optional float parameters of the wrapped function.
7578
These will be registered as double attributes in the
7679
functor class. The wrapped function must be callable as
@@ -118,6 +121,9 @@ def __init__(self):
118121

119122
# End of class RegisteredUnaryFunction
120123

124+
if replace:
125+
RegisteredUnaryFunction._deregisterType(regname)
126+
RegisteredUnaryFunction.__name__ = 'User' + cls.__name__ + '_' + regname
121127
RegisteredUnaryFunction()._registerThisType()
122128
rv = RegisteredUnaryFunction.createByType(regname)
123129
assert type(rv) is RegisteredUnaryFunction

0 commit comments

Comments
 (0)