Day 27 - MISSED; Some python and regex written for Gyft
16 Mar 2017 100daysofwriting · late-post · writingARGH! I missed yesterday. It wasn’t even too tiring a day or anything, anyway, I am going to make the best of this. I am about 8 hours later than when the usual post should have been.
I have not been writing a lot of code, and I hadn’t submitted a pull request in almost 20 days! That’s unacceptable, Jimmy, UNACCEPTABLE.
I decided to remedy that situation today. Gyft is a set of python scripts which
lets you login into your IIT Kharagpur ERP account, get your timetable for the
semester from there. Before my patch, the only option was to put this timetable
into your Google Calendar. You generate a client_secret
file and give the
python script access to add recurring events to your calendar. It didn’t sound
right to me, and it was pretty cumbersome to use as well.
So, I wanted to enable exporting the calendar to ICS files. ICS files are amazing! Every major calendar application supports them, they are apt for static calendars like a Semester timetable. I had this idea in August 2016, I opened the issue in December 2016. Since, it has just been vegetating because I wasn’t working on it, and the whole Gyft repository has been rather inactive since.
The first part of the patch that I had implemented already was the relatively time-consuming part. It had a function which would build an event given the appropriate parameters like start time, end time, etc.
The second part of this patch was to merge this with the parsing code that is
there inside the add_events.py
script. It takes the data from the data.txt
file (which is taken from the ERP) and parses that into event information.
Here comes the Regular Expressions part. The old parsing was done with a complicated string parsing mechanism. Frankly, I couldn’t understand what was being done, and I took a step back and analysed the string that we do want to parse. It looks something like this:
10:0:AM-10:55:AM
I realised that this was the best place to use Regular Expressions! A couple of minutes in the Python shell led me to this regular expression:
TIMETABLE_DICT_RE ='([0-9]{1,2}):([0-9]{1,2}):([AP])M-([0-9]{1,2}):([0-9]{1,2}):([AP])M'
This Regex is clearly not optimal, there is no need to repeat the same Regular Expression twice. Having it only once, should be enough. I should implement that!
But barring that, it worked out pretty well. The new parsing code which is part
of the new generate_ics.py
script was
smaller
and also returned a datetime
object instead of trying to write the complete
iCalendar event as per the RFC (IETF RFC 5545).
Eventually, this pull request came out of my work today! I am awaiting reviews on the pull request, and it should get merged soon. I have already generated an ICS and added it to my Google Calendar! It looks good!
POST #27 is OVER