馃悕馃悕馃悕
1"""
2Jupyter-specific stuff, mostly
3"""
4import time
5import uuid
6import os.path
7from datetime import date
8from pathlib import Path
9from IPython.core.magic import register_cell_magic
10
11print("notebook reloaded")
12
13class Context(dict):
14 __getattr__ = dict.__getitem__
15 __setattr__ = dict.__setitem__
16 __delattr__ = dict.__delitem__
17
18 def __init__(self, other=None):
19 if other is None:
20 return
21 for key in other:
22 self[key] = other[key]
23
24 def update_by(self, method):
25 self.update(method(self))
26
27def index_where(predicate, _list):
28 return next(filter(lambda _tuple: predicate(_tuple[1]), enumerate(_list)))[0]
29
30def is_comment(line):
31 return next(filter(lambda c: c != ' ', line)) == '#'
32
33@register_cell_magic
34def settings(args, cell):
35 original_lines = [line for line in cell.split("\n")]
36 i = index_where(lambda line: line.startswith("#!#"), original_lines)
37
38 settings_id = uuid.uuid4()
39 id_line = f"settings_id = \"{settings_id}\""
40
41 if os.path.isfile("settings.py"):
42 with open("settings.py", "r") as reader:
43 private_lines = [line for line in reader]
44 lines = [id_line] + private_lines + original_lines[i+1:]
45 else:
46 lines = [id_line] + original_lines[:i] + original_lines[i+1:]
47
48 is_meaningful = lambda line: line != "" and line != "\n" and not is_comment(line)
49
50 log_content = "\n".join(filter(is_meaningful, lines))
51
52 daily_directory = date.today().strftime("%d.%b.%Y")
53 daily_directory = f"out/{daily_directory}"
54 settings_directory = f"{daily_directory}/{settings_id}"
55
56 Path(settings_directory).mkdir(exist_ok=True, parents=True)
57
58 with open(f"{daily_directory}/{settings_id}/settings.py", "w") as file:
59 file.write(log_content)
60
61 run_content = log_content + f"\ndaily_directory = \"{daily_directory}\"\nsettings_directory = \"{settings_directory}\""
62
63 get_ipython().run_cell(run_content)
64
65def file_snapshot(filename):
66 try:
67 with open(filename, "r") as reader:
68 snapshot = reader.read()
69 return f"# Private settings:\n\n{snapshot}\n\n"
70 except:
71 return "# No private settings.\n\n"
72
73def _gc():
74 gc.collect()
75 torch.cuda.empty_cache()
76
77class Timer(object):
78 def __init__(self, name):
79 self.name = name
80
81 def __enter__(self):
82 self.t = time.perf_counter()
83
84 def __exit__(self, *args):
85 print(f"{self.name}: {time.perf_counter() - self.t}")