Skip to content

Commit f44dde2

Browse files
committed
style: last few lambdas etc
1 parent a0eeed1 commit f44dde2

4 files changed

Lines changed: 43 additions & 22 deletions

File tree

src/diffpy/srfit/fitbase/recipeorganizer.py

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import re
2626
from collections import OrderedDict
27+
from functools import partial
2728
from itertools import chain, groupby
2829

2930
import six
@@ -741,8 +742,7 @@ def clearConstraints(self, recurse=False):
741742
self.unconstrain(*self._constraints)
742743

743744
if recurse:
744-
_constraint_clearer = lambda m: hasattr(m, "clearConstraints")
745-
for m in filter(f, self._iterManaged()):
745+
for m in filter(_has_clear_constraints, self._iterManaged()):
746746
m.clearConstraints(recurse)
747747
return
748748

@@ -822,19 +822,16 @@ def clearRestraints(self, recurse=False):
822822
found there as well.
823823
"""
824824
self.unrestrain(*self._restraints)
825-
826825
if recurse:
827-
f = lambda m: hasattr(m, "clearRestraints")
828-
for m in filter(f, self._iterManaged()):
829-
m.clearRestraints(recurse)
826+
for msg in filter(_has_clear_restraints(), self._iterManaged()):
827+
msg.clearRestraints(recurse)
830828
return
831829

832830
def _getConstraints(self, recurse=True):
833831
"""Get the constrained Parameters for this and managed sub-objects."""
834832
constraints = {}
835833
if recurse:
836-
f = lambda m: hasattr(m, "_getConstraints")
837-
for m in filter(f, self._iterManaged()):
834+
for m in filter(_has_get_constraints(), self._iterManaged()):
838835
constraints.update(m._getConstraints(recurse))
839836

840837
constraints.update(self._constraints)
@@ -848,8 +845,7 @@ def _getRestraints(self, recurse=True):
848845
"""
849846
restraints = set(self._restraints)
850847
if recurse:
851-
f = lambda m: hasattr(m, "_getRestraints")
852-
for m in filter(f, self._iterManaged()):
848+
for m in filter(_has_get_restraints(), self._iterManaged()):
853849
restraints.update(m._getRestraints(recurse))
854850

855851
return restraints
@@ -968,15 +964,13 @@ def show(self, pattern="", textwidth=78):
968964
the screen width. Do not trim when negative or 0.
969965
"""
970966
regexp = re.compile(pattern)
971-
pmatch = lambda s: (
972-
len(s.split(None, 1)) < 2 or regexp.search(s.split(None, 1)[0])
973-
)
967+
_pmatch_with_re = partial(_pmatch, regexp=regexp)
974968
# Show sub objects and their parameters
975969
lines = []
976970
tlines = self._formatManaged()
977971
if tlines:
978972
lines.extend(["Parameters", _DASHEDLINE])
979-
linesok = filter(pmatch, tlines)
973+
linesok = filter(_pmatch_with_re, tlines)
980974
lastnotblank = False
981975
# squeeze repeated blank lines
982976
for lastnotblank, g in groupby(linesok, bool):
@@ -1002,7 +996,7 @@ def show(self, pattern="", textwidth=78):
1002996
if lines:
1003997
lines.append("")
1004998
lines.extend(["Restraints", _DASHEDLINE])
1005-
lines.extend(filter(pmatch, tlines))
999+
lines.extend(filter(_pmatch_with_re, tlines))
10061000

10071001
# Determine effective text width tw.
10081002
tw = textwidth if (textwidth is not None and textwidth > 0) else None
@@ -1059,5 +1053,22 @@ def equationFromString(
10591053
return eq
10601054

10611055

1062-
def _constraint_clearer(msg):
1056+
def _has_clear_constraints(msg):
10631057
return hasattr(msg, "clearConstraints")
1058+
1059+
1060+
def _has_clear_restraints(msg):
1061+
return hasattr(msg, "clearRestraints")
1062+
1063+
1064+
def _has_get_restraints(msg):
1065+
return hasattr(msg, "_getRestraints")
1066+
1067+
1068+
def _has_get_constraints(msg):
1069+
return hasattr(msg, "_getConstraints")
1070+
1071+
1072+
def _pmatch(inp_str, regexp):
1073+
parts = inp_str.split(None, 1)
1074+
return len(parts) < 2 or regexp.search(parts[0])

src/diffpy/srfit/pdf/characteristicfunctions.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,8 +216,6 @@ def lognormalSphericalCF(r, psize, psig):
216216
if psig <= 0:
217217
return sphericalCF(r, psize)
218218

219-
erfc = lambda x: 1.0 - erf(x)
220-
221219
sqrt2 = sqrt(2.0)
222220
s = sqrt(log(psig * psig / (1.0 * psize * psize) + 1))
223221
mu = log(psize) - s * s / 2
@@ -427,4 +425,8 @@ def __call__(self, r):
427425
return fr
428426

429427

428+
def erfc(x):
429+
return 1.0 - erf(x)
430+
431+
430432
# End of file

src/diffpy/srfit/sas/prcalculator.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323

2424
__all__ = ["PrCalculator", "CFCalculator"]
2525

26+
from functools import partial
27+
2628
import numpy
2729

2830
from diffpy.srfit.fitbase import Calculator
@@ -90,12 +92,15 @@ def __call__(self, r):
9092
self._invertor.y = iq
9193
self._invertor.err = diq
9294
c, c_cov = self._invertor.invert_optimize()
93-
l = lambda x: self._invertor.pr(c, x)
94-
pr = map(l, r)
95+
_inverted_w_c = partial(self._inverted, c=c)
96+
pr = map(_inverted_w_c, r)
9597

9698
pr = numpy.array(pr)
9799
return self.scale.value * pr
98100

101+
def _inverted(self, x, c):
102+
self._invertor.pr(c, x)
103+
99104

100105
# End class PrCalculator
101106

tests/test_contribution.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ def test_releaseOldEquations(self):
170170
def test_registerFunction(self):
171171
"""Ensure registered function works after second setEquation call."""
172172
fc = self.fitcontribution
173-
fsquare = lambda x: x**2
174-
fc.registerFunction(fsquare, name="fsquare")
173+
fc.registerFunction(_fsquare, name="fsquare")
175174
fc.setEquation("fsquare")
176175
fc.x.setValue(5)
177176
self.assertEqual(25, fc.evaluate())
@@ -184,6 +183,10 @@ def test_registerFunction(self):
184183
return
185184

186185

186+
def _fsquare(x):
187+
return x**2
188+
189+
187190
def testResidual(noObserversInGlobalBuilders):
188191
"""Test the residual, which requires all other methods."""
189192
gen = ProfileGenerator("test")

0 commit comments

Comments
 (0)