-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathutils.py
More file actions
69 lines (54 loc) · 1.9 KB
/
utils.py
File metadata and controls
69 lines (54 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
##############################################################################
#
# PDFgui by DANSE Diffraction group
# Simon J. L. Billinge
# (c) 2007 trustees of the Michigan State University.
# All rights reserved.
#
# File coded by: Pavol Juhas
#
# See AUTHORS.txt for a list of people who contributed.
# See LICENSE.txt for license information.
#
##############################################################################
"""Small shared routines:
numericStringSort -- sort list of strings according to numeric value
safeCPickleDumps -- same as pickle.dumps, but safe for NaN and Inf
"""
def numericStringSort(lst):
"""Sort list of strings inplace according to general numeric value.
Each string gets split to string and integer segments to create keys
for comparison. Signs, decimal points and exponents are ignored.
lst -- sorted list of strings
No return value to highlight inplace sorting.
"""
import re
rx = re.compile(r"(\d+)")
keys = [rx.split(s) for s in lst]
for k in keys:
k[1::2] = [int(i) for i in k[1::2]]
newlst = sorted(zip(keys, lst))
lst[:] = [kv[1] for kv in newlst]
return
def safeCPickleDumps(obj):
"""Get pickle representation of an object possibly containing NaN or
Inf. By default it uses pickle.HIGHEST_PROTOCOL, but falls back to
ASCII protocol 0 if there is SystemError frexp() exception.
obj -- object to be pickled
Return pickle string.
"""
import pickle
ascii_protocol = 0
try:
s = pickle.dumps(obj, pickle.HIGHEST_PROTOCOL)
except SystemError:
s = pickle.dumps(obj, ascii_protocol)
return s
def asunicode(s):
"""Convert string or bytes object to a text type."""
rv = s
if not isinstance(s, str):
rv = s.decode("utf-8")
return rv
# End of file