-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunner.py
More file actions
executable file
·56 lines (48 loc) · 1.68 KB
/
Copy pathrunner.py
File metadata and controls
executable file
·56 lines (48 loc) · 1.68 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
#!/usr/bin/python
import importlib
import euler
import argparse
import signal
import sys
import time
class TimeoutException(Exception):
pass
def timeout_handler(signum, frame):
raise TimeoutException()
def run_sol(n, timeout=None, disp_time=False):
print 'Euler #' + str(n) + ': ',
try:
sol = importlib.import_module('probs.e_' + str(n))
if timeout is not None:
signal.signal(signal.SIGALRM, timeout_handler)
signal.alarm(timeout)
start = time.time()
sol.main()
if disp_time:
print '\tTIME: ' + str(time.time() - start) + 's'
except ImportError:
print 'Not implemented.'
except TimeoutException:
print 'This is taking too long!'
except IOError, e:
print e # Required file is missing
def main():
parser = argparse.ArgumentParser(description='Runner for Project Euler solutions.')
parser.add_argument('-t', dest='timeout', type=int, help='Set the maximum \
time in seconds a problem is allowed to take.')
parser.add_argument('-v', action='store_true', dest='disp_time', \
help='Display time taken')
parser.add_argument('probs', metavar='P', nargs='*', help='Problem to solve')
args = parser.parse_args()
if len(sys.argv) == 1:
parser.print_help()
sys.exit(1)
for p in args.probs:
xs = p.split('-')
if len(xs) == 2:
for i in range(int(xs[0]), int(xs[1])+1, args.disp_time):
run_sol(i, args.timeout, args.disp_time)
else:
run_sol(int(xs[0]), args.timeout, args.disp_time)
if __name__ == '__main__':
main()