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


Python Sites of Note
Software Development



Recent Posts
 8/21/03
 7/21/03
 7/17/03
 7/10/03
 7/7/03
 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.
 

 

Tuesday, July 01, 2003
 

Quest for Massage, Part 7

In Part 6, I developed the project to the point where the program could successfully find the first available appointment slot in the website's appointment schedule.  But I needed the ability to exclude available slots during time periods when I'm not available.  This means being able to do date and time comparisons.  Python 2.3 will have date and time classes as part of its 'batteries included', but I'm still using Python 2.2.  So, M.-A. Lemburg's mxDateTime to the rescue.

The new version of the program changes the ApptSched.getNextAvail generator method so that, instead of returning the date and time of the next available appointment slot as a string, it now returns a tuple of indexes.  The first index is an index into the list of dates shown on the appointment schedule, while the second is an index into the list of times.

We pass this tuple of indexes to a new method, slotToDateTime.  This method uses the indexes to get the date and time strings, then parses out the date and time portions and uses this information to instantiate a DateTime object, which the method returns.

The MassageAppt class has been reorganized.  It now has an addExcludeRange method, which takes a tuple composed of a pair of DateTime objects.  These two objects specify the start and end of the date/time range to be excluded from desired appointment slots.  The tuple is added to a list of exclude ranges.  The findSlot method repeatedly calls ApptSched.getNextSlot, testing to see if the slot found falls within any of the date/time ranges to be excluded, until either a non-excluded slot is found, or no more slots are available.

There's another important area of reorganization in the ApptSched class.  ElementTree 1.2a adds limited support for XPath, which is a method for specifying a particular part of an XML document via a URL-like address.  In the previous version of the project, finding the ElementTree element that contained the appointment schedule table required this (more verbose than necessary) code:

treeRoot = treeObj.getroot()
bodyElem = treeRoot[1]
outerTableElem = bodyElem[0]
outerTBodyElem = outerTableElem[0]
secondTrElem = outerTBodyElem[1]
firstTdElem = secondTrElem[0]
secondInnerTableElem = firstTdElem[3]
secondTBodyElem = secondInnerTableElem[0]

Using ElementTree's XPath, that code is reduced to:

elem = treeObj.findall(".//tbody")[-1]

The string ".//tbody" is an XPath address, which the findall method now understands.  It tells findall to search for all elements with the tag 'tbody' throughout the tree of XML elements, and return them as a list.  Our problem-domain-specific knowledge of our web page tells us that we want the very last 'tbody' element in the list.  XPath is good!

Our project has now reached the point where it can find the first available appointment slot that is not in conflict with our own schedule.  But we need a better way for the program to know what that schedule is.  Stay tuned for Part 8, where we explore how to synchronize our massage appointment scheduler with our personal schedule.


3:22:59 PM  comment []    


Click here to visit the Radio UserLand website. © Copyright 2003 Michael Kent.
Last update: 8/21/2003; 3:59:33 PM.
This theme is based on the SoundWaves (blue) Manila theme.
July 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 31    
Jun   Aug

Previous/Next