-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_with_timings.py
More file actions
64 lines (53 loc) · 2.09 KB
/
run_with_timings.py
File metadata and controls
64 lines (53 loc) · 2.09 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
# run_with_timings.py
#
#!/usr/bin/env python
# coding: utf-8
dict_def = input("For first time use, or to clean up, YES to initialize the timing output dictionary, NO to restore dictionary from previous runs: ")
# check input above
if dict_def.upper() == "YES":
time_dict = {}
elif dict_def.upper() == "Y":
time_dict = {}
elif dict_def.upper() == "NO":
# don't initialize the time dictionary, but recall any stored values
get_ipython().run_line_magic('store', '-r')
elif dict_def.upper() == "N":
# don't initialize the time dictionary, but recall any stored values
get_ipython().run_line_magic('store', '-r')
import time
import subprocess
import sys
start_time = time.time()
file_to_run = input("Full path to file (without ipynb extension - e.g., myjupyternotebook): ")
file_type = input("Is this a jupyter notebook?: ")
# check file-type entered above
if file_type.upper() == "YES":
file_to_run = file_to_run + ".ipynb"
elif file_type.upper() == "Y":
file_to_run = file_to_run + ".ipynb"
else:
print("Not a Jupyter Notebook.")
# Use subprocess to run the external Python file instead of %run
try:
subprocess.run([sys.executable, file_to_run], check=True)
except Exception as e:
print(f"Error running file: {e}")
end_time = time.time()
elapsed_time = end_time - start_time
elapsed_time_inminutes = elapsed_time/60
print(f"Total run-time: \033[1m{elapsed_time:.4f} seconds\033[0m or \033[1m{elapsed_time_inminutes:.4f} minutes\033[0m")
print(" ")
import datetime
now = datetime.datetime.now()
timenow = now.strftime("%Y-%m-%d_%H:%M:%S")
now = timenow
# bold variables before making them a dictionary key
ctime = f"\033[1m{now}\033[0m"
file_to_run = f"\033[1m{file_to_run}\033[0m"
keyname = "File Name: " + file_to_run + ", " + "Date and Time of run: " + ctime
time_dict.update({keyname:elapsed_time})
get_ipython().run_line_magic('store', 'time_dict')
for key, value in time_dict.items():
value_in_min = int(value/60)
value_seconds = value % 60
print(f"{key}\tTime to run notebook:\t\033[1m{value_in_min:.0f}\033[0m minutes and \033[1m{value_seconds:06.3f}\033[0m seconds")