1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 """Small shared routines:
16 numericStringSort -- sort list of strings according to numeric value
17 safeCPickleDumps -- same as cPickleDumps, but safe for NaN and Inf
18 """
19
20 __id__ = "$Id: utils.py 2980 2009-04-02 00:14:33Z juhas $"
21
23 """Sort list of strings inplace according to general numeric value.
24 Each string gets split to string and integer segments to create keys
25 for comparison. Signs, decimal points and exponents are ignored.
26
27 lst -- sorted list of strings
28
29 No return value to highlight inplace sorting.
30 """
31 import re
32 rx = re.compile(r'(\d+)')
33 keys = [ rx.split(s) for s in lst ]
34 for k in keys: k[1::2] = [ int(i) for i in k[1::2] ]
35 newlst = zip(keys, lst)
36 newlst.sort()
37 lst[:] = [kv[1] for kv in newlst]
38 return
39
41 """Get cPickle representation of an object possibly containing NaN or Inf.
42 By default it uses cPickle.HIGHEST_PROTOCOL, but falls back to ASCII
43 protocol 0 if there is SystemError frexp() exception.
44
45 obj -- object to be pickled
46
47 Return cPickle string.
48 """
49 import cPickle
50 ascii_protocol = 0
51 try:
52 s = cPickle.dumps(obj, cPickle.HIGHEST_PROTOCOL)
53 except SystemError:
54 s = cPickle.dumps(obj, ascii_protocol)
55 return s
56
57
58