Skip to content

Instantly share code, notes, and snippets.

@notslang
Last active December 13, 2021 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notslang/3146122397b0f510c8c2e88d2e43b7a2 to your computer and use it in GitHub Desktop.
Save notslang/3146122397b0f510c8c2e88d2e43b7a2 to your computer and use it in GitHub Desktop.
from datetime import datetime, date
from json import dumps
import fileinput
import re
import sys
# detect useless "page x of n" lines
page_regex = re.compile(r'^Page,[0-9]+,of,[0-9]+')
def parse_input():
# skip the first line
page_header = sys.stdin.readline()
# lines are hard-wrapped. gather lines that belong together to join them
# later
lines_to_join = []
for line in fileinput.input():
# skip lines that only give a page number
if page_regex.match(line):
continue
# the first line is repeated several times, skip that too
if line == page_header:
continue
if len(lines_to_join) == 2:
yield ','.join(lines_to_join)
lines_to_join = []
lines_to_join.append(line.rstrip())
for line in parse_input():
if line == 'Site Name,Technician,Desciption,When Started,Duration (in minutes),Service Date,When Ended':
# skip headers
continue
values = line.split(',')
if len(values) == 7:
site_name, technician, description, when_started, duration, service_date, when_ended = values
else:
print('bad line: "' + line + '"', file=sys.stderr)
continue
# parse the dates
when_ended = datetime.strptime(when_ended, '%m/%d/%Y %I:%M:%S %p')
when_started = datetime.strptime(when_started, '%m/%d/%Y %I:%M:%S %p')
print(dumps({
'service_date': datetime.strptime(service_date, '%m/%d/%Y').strftime("%Y-%m-%d"),
'when_started': when_started.isoformat(),
'when_ended': when_ended.isoformat(),
'site_name': site_name,
'technician': technician,
'description': description,
#'duration': int(duration),
'duration': abs((when_ended - when_started).total_seconds()) / 60,
}))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment