Skip to content

Instantly share code, notes, and snippets.

@alex-hofsteede
Last active September 6, 2022 10:57
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save alex-hofsteede/56801c165dd2bd0ceca7 to your computer and use it in GitHub Desktop.
Save alex-hofsteede/56801c165dd2bd0ceca7 to your computer and use it in GitHub Desktop.
show numeric deltas for repeated runs of a command
#!/usr/bin/python
import re
import sys
import time
import subprocess
SLEEP_FOR = 2.0
RED = 196
NUMBER_REGEX = re.compile(r'-?\d+')
def colorize(text, color):
return '\033[38;5;%sm%s\033[0;00m' % (color, text)
if __name__ == '__main__':
if len(sys.argv) < 2:
print "Usage: ./delta COMMAND"
sys.exit(1)
command = " ".join(sys.argv[1:]) + "; exit 0;"
result = []
prev_result = []
now = time.time()
while True:
result = subprocess.check_output(command, shell=True)
delta_t = time.time() - now
now = time.time()
if result:
result = result.split('\n')
subbed_lines = []
for (index, line) in enumerate(result):
if index < len(prev_result):
prev_matches = list(NUMBER_REGEX.findall(prev_result[index]))
def delta_from_prev_match(match):
delta_v = int(match.group(0)) - int(prev_matches.pop(0)) if len(prev_matches) > 0 else 0
return colorize("%0.1f/sec" % (delta_v / delta_t), RED) if delta_v != 0 else match.group(0)
subbed_lines.append(NUMBER_REGEX.sub(delta_from_prev_match, line))
else:
subbed_lines.append(line)
print '\n'.join(subbed_lines)
prev_result = result
time.sleep(SLEEP_FOR)
@joeatwork
Copy link

For extra credit, you could move the re.compile out of the loop and (uselessly?) improve performance somewhat

@alex-hofsteede
Copy link
Author

done, thanks @joeatwork

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment