-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmwatch.py
72 lines (59 loc) · 1.84 KB
/
mmwatch.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
#!/usr/bin/env python3
import sys
import time
import subprocess
import datetime
import re
digits_re = re.compile("([0-9eE.+]*)")
to = 2.0
CLS='\033[2J\033[;H'
digit_chars = set('0123456789.')
def isfloat(v):
try:
float(v)
except ValueError:
return False
return True
def total_seconds(td):
return (td.microseconds + (td.seconds + td.days * 24. * 3600) * 10**6) / 10**6
def main(cmd):
prevp = []
prevt = None
while True:
t0 = datetime.datetime.now()
out = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE).communicate()[0]
p = digits_re.split(out.decode('utf-8'))
if len(prevp) != len(p):
s = p
else:
s = []
i = 0
for i, (n, o) in enumerate(zip(p, prevp)):
if isfloat(n) and isfloat(o) and float(n) > float(o):
td = t0 - prevt
v = (float(n) - float(o)) / total_seconds(td)
if v > 1000000000:
v, suffix = v / 1000000000., 'g'
elif v > 1000000:
v, suffix = v / 1000000., 'm'
elif v > 1000:
v, suffix = v / 1000.,'k'
else:
suffix = ''
s.append('\x1b[7m')
s.append('%*s' % (len(n), '%.1f%s/s' % (v, suffix)))
s.append('\x1b[0m')
else:
s.append(n)
s += n[i:]
prefix = "%sEvery %.1fs: %s\t%s" % (CLS, to, ' '.join(cmd), t0)
sys.stdout.write(prefix + '\n\n' + ''.join(s).rstrip() + '\n')
sys.stdout.flush()
prevt = t0
prevp = p
time.sleep(to)
if __name__ == '__main__':
try:
main(sys.argv[1:])
except KeyboardInterrupt:
pass