Trivial Thoughts
Thoughts and discussion on programming projects using the Python language.


Python Sites of Note
Software Development



Recent Posts
 7/1/03
 6/26/03
 6/25/03
 6/18/03
 6/15/03
 6/2/03
 5/28/03


Subscribe to "Trivial Thoughts" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.
 

 

Wednesday, June 18, 2003
 

Quest for Massage, Part 6

My little robot is starting to grow up.  Here is the latest version, which now knows how to extract the appointment schedule from the web page, and find the first available non-taken appointment slot.  I've also done some refactoring, turning the main code into its own class, which is the way I prefer to write code.

I like Python's list comprehensions.  It's still true that I often have to write the code more verbosely, then turn it into a comprehension, but I'm getting better at it.  This piece of code, from the ApptSched class, is the key to extracting the appointment schedule data from the ElementTree tree of element objects:

rows = secondTBodyElem.getchildren()
skipCols = (0, 4, 8)
self.dates = [ dateElem[0].text for index, dateElem in enumerate(rows[0])
if index not in skipCols ]
self.times = [ row[0].text for row in rows[1:] ]
self.slots = [ [ col.get("class") for colIndex, col in enumerate(row.getchildren()) 
    if colIndex not in skipCols ] for row in rows[1:] ]        

self.slots is bound to a two-dimensional array (a list of lists), containing the data from each appointment slot.  If the data for a slot is "avail", then it's an available slot.  The two-dimensional array is generated by using a list comprehension inside another list comprehension.  Some of the columns in the table that the data is extracted from are labels for the rows, so we use skipCols to skip over them.  The enumerate function (not shown in the code) is my implementation of Python 2.3's new function for iteration over a sequence while at the same time numbering (enumerating) each element of the sequence.  It's easy to write your own function using a generator, for use with versions of Python prior to 2.3.

So far, I've successfully parsed out the appointment schedule info from the web page, and I can find the first available slot.  However, this won't be sufficient, because I need to be able to restrict my slot search to those slots where I'm available.  That is, when searching for an appointment slot, I need to be able to exclude certain ranges of slots (like when I'm in a meeting, or at lunch).  This requires being able to do date comparisons.  For that, if I were using Python2.3, I'd use its new 'batteries included' datetime class.  However, since I'm using 2.2 now, it's time to investigate mxDateTime.

 


1:17:51 PM  comment []    


Click here to visit the Radio UserLand website. © Copyright 2003 Michael Kent.
Last update: 7/2/2003; 1:55:42 PM.
This theme is based on the SoundWaves (blue) Manila theme.
June 2003
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30          
May   Jul

Previous/Next