-
Notifications
You must be signed in to change notification settings - Fork 1
/
machine_learning.py
118 lines (94 loc) · 2.66 KB
/
machine_learning.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
'''machine learning utility functions and classes'''
import datetime
import os
import sys
import typing
import pdb
import pprint
import unittest
import shared_configuration
class Logger(object):
# ref: stack overflow: how do i duplicat sys stdout to a log file in python
def __init__(self, logfile_path=None):
self.terminal = sys.stdout
clean_path = logfile_path.replace(':', '-') if os.name == 'posix' else logfile_path
self.log = open(clean_path, 'w')
def write(self, message):
self.terminal.write(message)
self.log.write(message)
def flush(self):
self.terminal.flush()
pass
def main(
argv: typing.List[str],
program: str,
unittest,
do_work,
out_log='out_log',
):
config = shared_configuration.make(
program=program,
argv=argv[1:],
)
print('started %s with configuration' % program)
print(str(config))
# echo print statement to the log file
sys.stdout = Logger(config.get(out_log))
print(str(config))
if config.get('debug', False):
# enter pdb if run-time error
# (useful during development)
import debug
if False:
debug.info
unittest(config)
do_work(config)
def make_call_if(condition: bool, f):
if condition:
def ff(*args, **kwargs):
f(*args, **kwargs)
else:
def ff(*args, **kwargs):
pass
return ff
def make_verbose_pp(verbose: bool):
return make_call_if(
condition=verbose,
f=pprint.pprint,
)
def make_verbose_print(verbose: bool):
if verbose:
def verbose_print(*args):
print(*args)
else:
def verbose_print(*args):
pass
return verbose_print
def make_set_trace(flag: bool):
if flag:
def set_trace():
# NOTE: see ~/anaconda3/lib/python3.6/pdb.py function set_trace() to understand this call
pdb.Pdb().set_trace(sys._getframe().f_back)
else:
def set_trace():
pass
return set_trace
class Test(unittest.TestCase):
def test_make_verbose_print(self):
vp1 = make_verbose_print(False)
vp1('a', 123)
vp2 = make_verbose_print(False)
vp2('a', 123) # should print
def test_make_set_trace(self):
set_trace1 = make_set_trace(False)
set_trace2 = make_set_trace(False)
if False:
set_trace1()
set_trace2()
if False:
# usage example
sys.stdout = Logger('path/to/log/file')
pdb
# now print statements write on both stdout and the log file
if __name__ == '__main__':
unittest.main()