root / projects / wwp_cameras / natsort.py

Revision 557, 0.7 kB (checked in by tim, 10 months ago)

Add initial graphing code, some pyx demos, and the natural sort needed to make things work right

Line 
1def try_int(s):
2    "Convert to integer if possible."
3    try: return int(s)
4    except: return s
5
6def natsort_key(s):
7    "Used internally to get a tuple by which s is sorted."
8    import re
9    return map(try_int, re.findall(r'(\d+|\D+)', s))
10
11def natcmp(a, b):
12    "Natural string comparison, case sensitive."
13    return cmp(natsort_key(a), natsort_key(b))
14
15def natcasecmp(a, b):
16    "Natural string comparison, ignores case."
17    return natcmp(a.lower(), b.lower())
18
19def natsort(seq, cmp=natcmp):
20    "In-place natural string sort."
21    seq.sort(cmp)
22   
23def natsorted(seq, cmp=natcmp):
24    "Returns a copy of seq, sorted by natural string sort."
25    import copy
26    temp = copy.copy(seq)
27    natsort(temp, cmp)
28    return temp
Note: See TracBrowser for help on using the browser.