Skip to content

Teachers Coding: exploring CalDav files

August 25, 2019

It’s the start of the school year, and for me on of the big mundane tasks is getting my calendar set up for the school year. Our school’s home-brew SAS has this great feature that will generate .ics file containing all of your classes that you can import into Google calendar or any other calendaring program. But it has one problem—all of your class data is in the single file. Since we have a new rotating schedule, I was hoping I might be able to put each class into it’s own calendar in order to let me better see how a particular class is mapping out through the upcoming weeks.

When I thought about it for a bit, I realized this was a great opportunity to put my coding skills to use. I knew an .ics file was basically a text file that listed all of the events to be imported into the calendar. I opened up the calendar file in a text editor (Atom, in this case) and took a look:

Screen Shot 2019 08 25 at 11 23 39 PM

If you look closely at this file you see the structure is pretty simple. The first 7 lines are the header that sets up the calendar, and then every event begins with a BEGIN: VEVENT and ends with a END:VEVENT 7 lines later.

That got me thinking that I could write a python program that would find the first event, and then parse it in 7-line chunks, writing each chunk into a file based on which class it was associated with (the SUMMARY line).

I knew the python is great for this thing, but I’d forgotten the finer points of reading and writing files, so I Googled it. I also didn’t know about how I would parse the text file in 7 line chunks, but googling “parse text file n lines at a time python” sent me to this very helpful post that introduced me to itertools and the islice function which does exactly what I needed.

Here’s the program I came up with (link to code on Github Gist):

NewImage

If you’re interested, here’s a 5-minute screencast where I explain how this code works

All told, it took me about 45 minutes to write this code, which is probably close to the amount of time it would have taken me to just cut and paste the original file into 4 separate calendar files. I’m also sure there’s some regex expert out there who could have done this in 5 seconds, and if you’re reading this, I’d love to hear about it in the comments.

If you were at my school, or happened to have a similar calDav file containing events with the same name that you wanted to separate into individual files, you should be able to modify the search strings in line 27, 31, 35 and 39 to whatever you’re looking for, change the names of the output files in the first 4 lines and you’d be good to go.

Now, I’ve got a much better idea of how CalDav files work, and can think of a lot of ways to modify this program:

  • If I knew all the days I was going to give a quiz in a class, I could put together a file that would list all of the quiz days to share with my students.
  • I could modify this program to calculate the total number of class periods or total minutes I have in a class.
  • I could export my Google calendar as an .ics file, and then use a program like this to analyze all sorts of questions like if I had logged my workouts, how often do I workout in the past year.
Advertisements
No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: