turbx 1.0.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
turbx/utils.py ADDED
@@ -0,0 +1,84 @@
1
+ import numpy as np
2
+
3
+ '''
4
+ ========================================================================
5
+ Utilities
6
+ ========================================================================
7
+ '''
8
+
9
+ # ======================================================================
10
+
11
+ def even_print(label, output, **kwargs):
12
+ '''
13
+ print/return a fixed width message
14
+ '''
15
+ terminal_width = kwargs.get('terminal_width',72)
16
+ s = kwargs.get('s',False) ## return string
17
+
18
+ ndots = (terminal_width-2) - len(label) - len(output)
19
+ text = label+' '+ndots*'.'+' '+output
20
+ if s:
21
+ return text
22
+ else:
23
+ #sys.stdout.write(text)
24
+ print(text)
25
+ return
26
+
27
+ def format_time_string(tsec):
28
+ '''
29
+ format seconds as dd:hh:mm:ss
30
+ '''
31
+ m, s = divmod(abs(int(round(tsec))),60)
32
+ h, m = divmod(m,60)
33
+ d, h = divmod(h,24)
34
+ d = int(round(d))
35
+ h = int(round(h))
36
+ m = int(round(m))
37
+ s = int(round(s))
38
+ if (d==0) and (h==0) and (m==0):
39
+ s_out = f'{s:d}s'
40
+ elif (d==0) and (h==0):
41
+ s_out = f'{m:d}m:{s:02d}s'
42
+ elif (d==0):
43
+ s_out = f'{h:d}h:{m:02d}m:{s:02d}s'
44
+ else:
45
+ s_out = f'{d:d}d:{h:02d}h:{m:02d}m:{s:02d}s'
46
+ if (tsec >= 0):
47
+ return s_out
48
+ else:
49
+ return f'-{s_out}'
50
+
51
+ def format_nbytes(size):
52
+ '''
53
+ format a number of bytes to [B],[KB],[MB],[GB],[TB]
54
+ '''
55
+ if not isinstance(size,(int,float)):
56
+ raise ValueError('arg should be of type int or float')
57
+ if (size<1024):
58
+ size_fmt, size_unit = size, '[B]'
59
+ elif (size>1024) and (size<=1024**2):
60
+ size_fmt, size_unit = size/1024, '[KB]'
61
+ elif (size>1024**2) and (size<=1024**3):
62
+ size_fmt, size_unit = size/1024**2, '[MB]'
63
+ elif (size>1024**3) and (size<=1024**4):
64
+ size_fmt, size_unit = size/1024**3, '[GB]'
65
+ else:
66
+ size_fmt, size_unit = size/1024**4, '[TB]'
67
+ return size_fmt, size_unit
68
+
69
+ def step(a,b,x,order=3):
70
+ '''
71
+ Polynomial step function
72
+ https://en.wikipedia.org/wiki/Smoothstep
73
+ '''
74
+ x = (x-a)/(b-a)
75
+ if (order==1):
76
+ y = -2*x**3 + 3*x**2
77
+ elif (order==2):
78
+ y = 6*x**5 - 15*x**4 + 10*x**3
79
+ elif (order==3):
80
+ y = -20*x**7 + 70*x**6 - 84*x**5 + 35*x**4
81
+ else:
82
+ raise NotImplementedError
83
+ y = np.clip(y,0.,1.)
84
+ return y