Skip to content

Instantly share code, notes, and snippets.

Last active Feb 12, 2019
What would you like to do?
show numeric deltas for repeated runs of a command
import re
import sys
import time
import subprocess
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"
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( - 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
subbed_lines.append(NUMBER_REGEX.sub(delta_from_prev_match, line))
print '\n'.join(subbed_lines)
prev_result = result

This comment has been minimized.

Copy link

@joeatwork joeatwork commented Mar 26, 2015

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


This comment has been minimized.

Copy link
Owner Author

@alex-hofsteede alex-hofsteede commented Mar 26, 2015

done, thanks @joeatwork

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