forked from ekfriis/farmout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
expandRunRange.py
executable file
·76 lines (59 loc) · 1.75 KB
/
expandRunRange.py
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
70
71
72
73
74
75
76
#!/bin/env python
'''
Expand an input list of run ranges.
Example:
>> expandRunRange.py 1,2,3-7,9
>> 1,2,3,4,5,6,7,9
Run unit test with: python -m doctest expandRunRange.py
Author: Evan K. Friis, UW Madison
'''
import sys
import re
def main():
# Get all command line arguments
arguments = sys.argv[1:]
try:
output_runs = expandRunRange(arguments)
except (TypeError, ValueError), e:
sys.stderr.write("%s: %s\n" % (sys.argv[0], e.message))
return 1
sys.stdout.write(','.join('%i' % x for x in output_runs) + '\n')
range_matcher = re.compile('(?P<start>\d+)-(?P<end>\d+)')
def expandRunRange(runrange):
'''
Concatenate a range of arguments
>>> main(['1,2,3,4,5-7'])
1,2,3,4,5,6,7
There can also be spaces.
>>> main(['1,2,3', '4,5-7'])
1,2,3,4,5,6,7
'''
# Turn into a single comma separated list
joined_arguments = ','.join(runrange)
output_runs = []
# Look at each cleaned argument
for arg in joined_arguments.split(','):
# Skip blank arguments
if not arg:
continue
# Check if it's a range
match = range_matcher.match(arg)
if match:
start = int(match.group('start'))
end = int(match.group('end'))
if end < start:
raise TypeError("end of range %s is less than the start" % arg)
output_runs.extend(range(start, end+1))
else:
try:
output_runs.append(int(arg))
except ValueError, e:
e.message = "can't parse argument %r" % arg
raise
return output_runs
if __name__ == "__main__":
try:
ret = main()
except KeyboardInterrupt:
ret = None
sys.exit(ret)