Skip to content

Instantly share code, notes, and snippets.

@janwillemm
Last active February 28, 2017 12:20
Show Gist options
  • Save janwillemm/285fa894732b33609a6cd691b885c1f8 to your computer and use it in GitHub Desktop.
Save janwillemm/285fa894732b33609a6cd691b885c1f8 to your computer and use it in GitHub Desktop.
Counts all the hours from a horrific format
from datetime import datetime
import re
# Splitting the lines
lines = []
format_string = "%d.%m.%Y";
hours = {}
def parseLines(start_date, end_date, lines):
curDate = None
curDateLines = []
for line in lines:
# Checking for indentation
tabs = re.match(r'[ \t\n\r\f\v]', line)
if tabs:
#not a date
curDateLines.append(line)
else:
if curDate is not None and curDate <= end_date and curDate >= start_date :
parseLinesForDate(curDateLines)
curDateLines = []
curDate = datetime.strptime(line.strip().replace(":", ""), format_string);
if curDate is not None and curDate <= end_date and curDate >= start_date:
parseLinesForDate(curDateLines)
def parseLinesForDate(lines):
for line in lines:
parseLine(line)
def parseLine(line):
substring = line.split(":");
if len(substring) > 1:
# It has an : so it is hours (can be description too, todo)
hoursName = substring[0].strip().lower()
additionHours = float(substring[1])
curHours = hours.get(hoursName, 0)
hours[hoursName] = curHours + additionHours
else:
#Its a discription
pass
files = input("All files with commas inbetween: ")
if ',' in files:
files = files.split(",")
else:
files = [files]
for file in files:
fp = open(file)
for i, line in enumerate(fp.readlines()):
lines.append(line)
start_date = input("Starting date(dd.mm.yyyy: ")
end_date = input("Ending date(dd.mm.yyyy: ")
start_date = datetime.strptime(start_date, format_string)
end_date = datetime.strptime(end_date, format_string)
parseLines(start_date, end_date, lines)
print(hours)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment