Thursday, September 13, 2012

The Cane Creek Market


I'm traveling home from Djangocon in Washington DC. Since I refuse to surrender my constitutional rights in order to be allowed to ride in an airplane, I'm stuck on the ground. However, I treated this as an opportunity, I trucked my Harley to Tennessee and the availed my self of some seriously nice motorcycling in the eastern states both to and from DC. I'm now on the return truck part of the trip with a couple Harleys in tow.

My riding buddy, Mike, had a mission. His neighbor, Rosie, on hearing that he was to be traveling in the South, requested that he get her some Sorghum Syrup. Not knowing where to find it, we asked our hosts Bill and Peggy in Hohenwald for a clue. They immediate suggested Cane Creek Market a small rural store on an obscure road in Tennesse. 
 
It was only a few miles away from Hohenwald, so we decided to stop by as we started our long journey back to the West Coast. It really seems to be in the middle of nowhere. I hadn't planned on going in, but Mike was taking a long time, so I decided to see what was up.

The only word that I can come up with to describe the place is “enchanting”. The store is a showcase of locally produced goods and all the components to make them. I saw no national brands. The shelves were stocked with bags, tubs and jars all sporting labels likely printed right there in the store.

While I'm not a stranger to Costco, I generally prefer to shop at my local organic coop. I doubt that even my politically correct Oregon coop market has as many locally sourced items on its shelves.

I encountered a bearded man taking inventory and commented on my delight in visiting his store. I introduced myself and I into a conversation with Vance Coblentz, the current owner:


“This place was established in 1990, approx. 22 yrs. ago. Originally it had
been a farmers market at another location. The business was then moved to
the current place by the previous owner. It was run by a Mennonite man, same
as me...”

“I had originally moved in from Ohio to take a job at the local lumber yard, which I did. I've always thought I'd like to do something like this, operating a store, but it never seemed to work out until 2 years after we moved here. I have a small family consisting of: my wife, 9 mo. old son, and a 15 mo. old foster daughter that we will be adopting on Sept. 24 of this year. So, am hoping this will be a greatthing to bring up my children on.”

About the mix of his clientele, Vance said, “I would say about 35% of my customers are local. The rest would either be tourists, out-of-state friends and relatives of the locals, and people from surrounding cities like Memphis and Nashville. The "locals", as they are known, would consist of a mixture of Amish-Mennonite, Mennonite, and a few others in between.”

Between the two of us, Mike and I bought about eighty bucks worth of snacks, pickled or dried vegetables, and, yes, sorghum syrup.

So this is my advice, if you're traveling I40 between Nashville and Memphis, take the extra forty minutes: get off the freeway at exit 143, head south on TN13 for about 15 miles, turn left onto TN438 and you'll find the Cane Creek Market on the right in about two miles. It is well worth the trip.

Wednesday, September 05, 2012

Useless or Useful?

While at Daniel Lindsley's lecture at Djangocon entitled "API Design Tips", he made a comment about what he considered to be a useless Python base class where all the methods simply raised NotImplemented exceptions. While not a literal quote, it looked something like this:

    class MessageHandlerBase(object):

        def send_message(self, *args, **kwargs):
            raise NotImplemented

        def broadcast_message(self, *args, **kwargs):
            raise NotImplemented

On the surface, I do admit that this class looks pretty useless. It looks like the code a C++ programmer would produce to simulate a virtual base class. A VBC in C++ forces authors of subclasses to create implementations for each of the pure functions in the base class.

A class that follows that perceived intent and implements all the pure methods gains nothing from the base class. Because of Python's "duck typing" abilities, the derived class didn't need the base class at all. It would have just as functional deriving from object as it would from MessageHandlerBase.

I contend that the base class isn't trying to simulate VBCs at all. It is defining optional methods, not required methods. By defining them in the base class as just raising a NotImplemented exception, it is setting up to allow a controlled fallback behavior if a derived class chooses to not implement a method.

    class A(object):
        """talk to someone"""

        def send_message(self, message):
            person = self.find_someone()
            person.talk_to_me(message_broken)

        def broadcast_message(self, message):
            self.shout(message)
             

    class B(object):
        """send message via radio broadcast""" 
   
        def broadcast_message(self, message):
            self.radio.broadcast(message)


    class C(MessageHandlerBase):
        """send message by waving flags"""

         def broadcast_message(self, message):
            self.flags.wave_message(message)


    def notify_of_emergency(a_message_handler):
        """Notify a specific person of an emergency condition.  If that is
        not possible, broadcast the emergency message"""
        message = "the sky is falling!"
        try:
            # try to send the message
            a_message_handler.send_message(message)
        except NotImplemented:
            # fallback to broadcasting the message
            a_message_handler.broadcast_message(message)

Classes A and B can be used with the function notify_of_emergency, but both will fail. Class A raises an AttributeError because of an admittedly intentional problem in the send_message method. Class B raises an AttributeError, too, but for a different reason: it doesn't know how to send a direct message, it can only broadcast. By deriving from MessageHandlerBase, class C is able to implement only the broadcast method. With an instance of class C, the notify_of_emergency function can gracefully degrade.

I think a key point here can be seen in the names of raised exceptions. Classes A and B both raise an error: AttributeError. The term "Error" doesn't appear in the name of the exception NotImplemented, because it is not an error. This exception is used for control flow, not an error condition. I didn't want to use the AttributeError exception for control flow. I don't want to hide legitimate errors by catching them and using them in control flow.

While there are certainly other ways, perhaps even superior, to implement a similar fallback behavior, I believe this example shows that a base class composed entirely of NotImplemented methods can be useful. Even if nothing else, the base class, if commented well to explain the intent, serves as documentation of the methods to be employed by users of the class.