I'm creating a cmd.Cmd class, and I have developed a helper method to easily 
handle help_xxx methods.

I'm trying to figure out if there is an even lazier way I could do this with 
decorators.

Here is the code:
*********************
import cmd


def add_help(func):
    if not hasattr(func, 'im_class'):
        return func #probably should raise an error
    cls = func.im_class
    setattr(cls, func.im_func.__name__.replace("do","help"), None)

    return func


class BaseCmd(cmd.Cmd):
    def __init__(self, *args, **kwargs):
        cmd.Cmd.__init__(self, *args, **kwargs)

    def show_help(self, func):
        print "\n".join((line.strip() for line in func.__doc__.splitlines()))

    @add_help
    def do_done(self, line):
        """done
        Quits this and goes to higher level or quits the application.
        I mean, what else do you expect?
        """
        return True

if __name__=='__main__':
    c = BaseCmd()

    print c.help_done


********************* 

This generates "AttributeError: BaseCmd instance has no attribute 'help_done'"

The show_help method is the shortcut I want to use (I'm pretty sure it's from 
Doug Hellman's site). I'm wondering if it's possible to use a decorator such as 
add_help to automatically create the appropriate help_xxx function.

In the decorator, I can get the function and the name of the class, but I can't 
find the instance of  the class that the method is attached to. Maybe this is 
just one step of lazy too far.


Am I right in thinking that I can't do this? There is no way to access the 
class instance from the method?
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to