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
|
|