Skip to content

Commit 365273a

Browse files
committed
Move numeric sort to the util module.
Also convert the list-sorting function to a more general key-generating function.
1 parent 70a6bdb commit 365273a

2 files changed

Lines changed: 33 additions & 22 deletions

File tree

diffpy/srfit/fitbase/fitresults.py

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
from diffpy.srfit.util.inpututils import inputToString
3030
from diffpy.srfit.util import _DASHEDLINE
31+
from diffpy.srfit.util import sortKeyForNumericString as numstr
3132

3233

3334
class FitResults(object):
@@ -347,7 +348,7 @@ def formatResults(self, header = "", footer = "", update = False):
347348
## Per-FitContribution results
348349
if len(self.conresults) > 1:
349350
keys = self.conresults.keys()
350-
numericStringSort(keys)
351+
keys.sort(key=numstr)
351352

352353
lines.append("")
353354
l = "Contributions"
@@ -428,7 +429,7 @@ def formatResults(self, header = "", footer = "", update = False):
428429
keys.append(name)
429430
vals[name] = (val, unc)
430431

431-
numericStringSort(keys)
432+
keys.sort(key=numstr)
432433
w = str(w+1)
433434
formatstr = "%-"+w+"s %- 15f +/- %-15f"
434435
for name in keys:
@@ -669,23 +670,3 @@ def initializeRecipe(recipe, results):
669670
var.value = float(value)
670671

671672
return
672-
673-
674-
def numericStringSort(lst):
675-
"""Sort list of strings inplace according to general numeric value.
676-
677-
Each string gets split to string and integer segments to create keys
678-
for comparison. Signs, decimal points and exponents are ignored.
679-
680-
lst -- sorted list of strings
681-
682-
No return value to highlight inplace sorting.
683-
684-
"""
685-
rx = re.compile(r'(\d+)')
686-
keys = [ rx.split(s) for s in lst ]
687-
for k in keys: k[1::2] = [ int(i) for i in k[1::2] ]
688-
newlst = zip(keys, lst)
689-
newlst.sort()
690-
lst[:] = [kv[1] for kv in newlst]
691-
return

diffpy/srfit/util/__init__.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,34 @@
2020

2121
_DASHEDLINE = 78 * '-'
2222

23+
24+
def sortKeyForNumericString(s):
25+
"""\
26+
Compute key for sorting strings according to their integer numeric value.
27+
28+
Each string gets split to string and integer segments to create keys
29+
for comparison. Signs, decimal points and exponents are ignored.
30+
This function is intended as the ``key`` argument for the ``sorted``
31+
or ``list.sort`` function.
32+
33+
Parameters
34+
----------
35+
s : str
36+
String which may have numeric components, e.g., "a12b".
37+
38+
Returns
39+
-------
40+
tuple
41+
Tuple of non-numeric segments intermixed with integer values.
42+
"""
43+
if sortKeyForNumericString._rx is None:
44+
import re
45+
sortKeyForNumericString._rx = re.compile(r'(\d+)')
46+
rx = sortKeyForNumericString._rx
47+
rv = tuple((int(w) if i % 2 else w)
48+
for i, w in enumerate(rx.split(s)))
49+
return rv
50+
51+
sortKeyForNumericString._rx = None
52+
2353
# End of file

0 commit comments

Comments
 (0)