-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
86 lines (67 loc) · 1.7 KB
/
utility.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
"""Define utility functions to manage interaction with user (input/output)."""
def interp(T, T1, T2, v1, v2):
coef = (v2 - v1) / (T2 - T1)
return coef * (T - T1) + v1
def separate():
print('\n' + '=' * 29 + '\n')
def pr(msg, maxlen=29):
"""Fit msg in the screen"""
if type(msg) != str:
msg = str(msg)
if len(msg) <= maxlen:
print(msg)
return
i = msg.find(" ")
lasti = -1
while i != -1:
if i >= maxlen:
if lasti == -1:
break
print(msg[:lasti])
pr(msg[lasti + 1:], maxlen)
break
#
lasti = i
i = msg.find(" ", i + 1)
# Couldn't split
if lasti == -1:
print(msg)
elif i == -1:
print(msg[:lasti])
print(msg[lasti + 1:])
def get(varname):
try:
inp = float(input(varname + ' = '))
return inp
except KeyboardInterrupt:
return
except Exception as e:
print(e)
return get(varname)
def get_params(lst):
params = {}
for p in lst:
params[p] = get(p)
pr(params)
return params
sci = lambda x: "{:.4e}".format(x)
def out(desc, val):
if val > 9999 or val < 0.000999999:
val = sci(val)
else:
val = "{:.5}".format(val)
print(desc + ' = ' + val)
def choose_opt(descr, optmap):
"""Only lowcase letter in optmap !"""
intmap = {i + 1: opt for i, opt in enumerate(optmap)}
# opt_txt = ', '.join([str(k)+': '+e for k,e in intmap.items()])
pr('Choose ' + descr + ': ')
pr(intmap)
inp = input('> ')
try:
inp = int(inp)
inp = intmap[inp]
except:
inp = inp.lower()
separate()
return optmap[inp]()