Skip to content

Instantly share code, notes, and snippets.

@temporaer
Created November 2, 2016 17:29
Show Gist options
  • Save temporaer/8ce50239083642ab0305408e0fb28f9f to your computer and use it in GitHub Desktop.
Save temporaer/8ce50239083642ab0305408e0fb28f9f to your computer and use it in GitHub Desktop.
Time your progress on a monotonous task, print your ETA
import sys
import click
import shelve
import datetime
from prompt_toolkit.validation import Validator, ValidationError
from prompt_toolkit import prompt
class NumberValidator(Validator):
def validate(self, document):
text = document.text
if text and not text.isdigit():
raise ValidationError(message='enter current line number')
def print_stats(records, length):
alpha, omega = records[0], records[-1]
time_per_line = ((omega['ts']-alpha['ts']) / (omega['line'] - alpha['line']))
print("Time per line:", time_per_line)
print("ETA: ", datetime.datetime.now() + time_per_line * (length - records[-1]['line']))
@click.command()
@click.argument('sheet')
@click.argument('length', type=int)
def main(sheet, length):
shv = shelve.open("slave-time.shv")
records = []
if sheet in shv:
records = shv[sheet]
if len(records) > 1:
print_stats(records, length)
print("Last record:", records[-1])
while True:
line = int(prompt('Line: ', validator=NumberValidator()))
records.append(dict(line=line, ts=datetime.datetime.now()))
shv[sheet] = records
if len(records) > 1:
print_stats(records, length)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment