-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathterminal_logger.py
More file actions
executable file
·59 lines (45 loc) · 1.71 KB
/
terminal_logger.py
File metadata and controls
executable file
·59 lines (45 loc) · 1.71 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
from blessings import Terminal
import progressbar
import sys
class TermLogger(object):
def __init__(self, n_epochs, train_size, test_size):
self.n_epochs = n_epochs
self.train_size = train_size
self.test_size = test_size
self.t = Terminal()
s = 10
e = 1 # epoch bar position
tr = 3 # train bar position
ts = 6 # test bar position
h = self.t.height
for i in range(10):
print('')
self.epoch_bar = progressbar.ProgressBar(max_value=n_epochs, fd=Writer(self.t, (0, h-s+e)))
self.train_writer = Writer(self.t, (0, h-s+tr))
self.train_bar_writer = Writer(self.t, (0, h-s+tr+1))
self.test_writer = Writer(self.t, (0, h-s+ts))
self.test_bar_writer = Writer(self.t, (0, h-s+ts+1))
self.reset_train_bar()
self.reset_test_bar()
def reset_train_bar(self):
self.train_bar = progressbar.ProgressBar(max_value=self.train_size, fd=self.train_bar_writer)
def reset_test_bar(self):
self.test_bar = progressbar.ProgressBar(max_value=self.test_size, fd=self.test_bar_writer)
class Writer(object):
"""Create an object with a write method that writes to a
specific place on the screen, defined at instantiation.
This is the glue between blessings and progressbar.
"""
def __init__(self, t, location):
"""
Input: location - tuple of ints (x, y), the position
of the bar in the terminal
"""
self.location = location
self.t = t
def write(self, string):
with self.t.location(*self.location):
sys.stdout.write("\033[K")
print(string)
def flush(self):
return