Created
February 15, 2014 16:43
-
-
Save lukasmartinelli/9021795 to your computer and use it in GitHub Desktop.
Calculate the total time based on the summary and time of iCalendar Events
This file contains 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
from icalendar import Calendar | |
from datetime import timedelta | |
from itertools import groupby | |
from operator import itemgetter | |
def calculate_time(event): | |
start = event['DTSTART'].dt | |
end = event['DTEND'].dt | |
return end - start | |
def lecturize(event): | |
summary = str(event['SUMMARY']) | |
for lecture in lectures: | |
if lecture in summary: | |
return lecture | |
def time_per_lecture(events): | |
sorted_events = sorted(events, key=itemgetter(0)) | |
for key, group in groupby(sorted_events, itemgetter(0)): | |
yield (key, sum(map(itemgetter(1), group), timedelta())) | |
lectures = ['An1I', 'Math1I', 'Bsys1', 'CN1', 'EnglHTw', 'Prog1', 'ICTh'] | |
file = open('examtime_export.ics', 'rb') | |
cal = Calendar.from_ical(file.read()) | |
events = [(lecturize(e), calculate_time(e)) for e in cal.walk('vevent')] | |
used_time = dict(time_per_lecture(events)) | |
total_time = sum(used_time.values(), timedelta()) | |
for lecture, time in used_time.items(): | |
print('{}\t{}h'.format(lecture, time.total_seconds() / 3600)) | |
print('=============') | |
print('TOTAL\t{}h'.format(total_time.total_seconds() / 3600)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Lukas,,
def calculate_time(event): start = event['DTSTART'].dt end = event['DTEND'].dt return start
In the code snippet above, event['DTEND'].dt throws a key error. Do you know if the module has changed since you wrote the code?