Commits

Remi Meier  committed cdb55f3

print times even if KeyboardInterrupt

  • Participants
  • Parent commits 34b85e9

Comments (0)

Files changed (1)

 
     times = []
     k = 1
-    while True:
-        time.sleep(0.2)
-        if not args.q:
-            print "Run {}/{}:".format(k, args.k)
-
-        test_time = time.time()
-        if args.p:
-            test.run(*args.more)
-        else:
-            with nostdout():
+    try:
+        while True:
+            time.sleep(0.2)
+            if not args.q:
+                print "Run {}/{}:".format(k, args.k)
+
+            test_time = time.time()
+            if args.p:
                 test.run(*args.more)
-        times.append(time.time() - test_time)
-
+            else:
+                with nostdout():
+                    test.run(*args.more)
+            times.append(time.time() - test_time)
+
+            if not args.q:
+                print "took {} s".format(times[-1])
+
+            if k >= args.k:
+                if within_error(args, times):
+                    break
+                elif not args.q:
+                    print "error was not within", args.error
+
+                if k > 2 * args.k:
+                    if not args.q:
+                        print "max number of iterations reached", \
+                            "error still too great, finish anyway"
+                    break
+            k += 1
+    finally:
         if not args.q:
-            print "took {} s".format(times[-1])
-            
-        if k >= args.k:
-            if within_error(args, times):
-                break
-            elif not args.q:
-                print "error was not within", args.error
-            
-            if k > 2 * args.k:
-                if not args.q:
-                    print "max number of iterations reached", \
-                        "error still too great, finish anyway"
-                break
-        k += 1
-
-    if not args.q:
-        print "times:", times
-
-    times = sorted(times)[:args.k]
-    result = {'best':min(times),
-              'error':get_error(times),
-              'std_dev(k)':std_dev(times)}
-    print json.dumps(result)
+            print "times:", times
+
+        if times:
+            times = sorted(times)[:args.k]
+            result = {'best':min(times),
+                      'error':get_error(times),
+                      'std_dev(k)':std_dev(times)}
+            print json.dumps(result)