Last active
July 3, 2018 19:54
-
-
Save catichenor/a0db4f3b870064fe6f77cc5628876462 to your computer and use it in GitHub Desktop.
Take in a GitHub-Flavored-Markdown time tracking list and output total times
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import re | |
from collections import defaultdict | |
# Script takes in a GitHub-Flavored-Markdown table assuming the following format: | |
# | |
# Start | Stop | Task | |
# ----- | ---- | ---- | |
# 9:20 | 9:40 | Email/Admin | |
# 9:40 | 11:30 | Automation | |
# 11:30 | 12:00 | Meeting | |
# 12:15 | 2:30 | Automation | |
# 2:30 | 3:00 | Meeting | |
# 3:00 | 3:30 | Automation | |
# 3:30 | 4:00 | Meeting | |
# 4:00 | 5:45 | Automation | |
# 5:45 | 6:00 | Admin | |
# Script then outputs the same table, and appends the following table: | |
# | |
# Task | Total Time | |
# ---- | ---------- | |
# Admin | 0.25 | |
# Automation | 6.33 | |
# Meeting | 1.5 | |
# Email/Admin | 0.34 | |
# Total | 8.42 | |
table_lines = [] | |
for i in sys.argv[1:]: | |
table_lines.append(i) | |
## The line after this comment is a workaround for a macOS Automator issue where input is not given as separate arguments, | |
## but as newline-separated text | |
# table_lines = table_lines[0].splitlines() | |
def decimal_time(input_time): | |
time_hour, time_minute = input_time.split(":") | |
minute_int = round(float(time_minute) * 1.666666) | |
minute_decimal = minute_int / 100 | |
return float(time_hour) + minute_decimal | |
def calculate_time(start_time, end_time): | |
if end_time < start_time: | |
end_time = end_time + 12 | |
return end_time - start_time | |
error = False | |
output_lines = [] | |
for line in table_lines: | |
if re.match(r'^[0-9]', line): | |
validation_regex = r'(^[0-9]{1,2}:[0-9]{2}) \| ([0-9]{1,2}:[0-9]{2}) \| (.+)' | |
if not re.match(validation_regex, line): | |
error = True | |
break | |
start, end, category = re.search(validation_regex, line).groups() | |
hours_spent = calculate_time(decimal_time(start), decimal_time(end)) | |
output_lines.append((category, hours_spent)) | |
merged_times = defaultdict(float) | |
for k, v in output_lines: | |
merged_times[k] += v | |
if error: | |
print("\n".join(table_lines)) | |
else: | |
total_time = 0 | |
print("\n".join(table_lines)) | |
print("Task | Total Time") | |
print("---- | ----------") | |
for k, v in merged_times.iteritems(): | |
print(" | ".join((str(k), str(v)))) | |
total_time += v | |
print("Total | " + str(total_time)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment