Getting lazy with decorators
Peter Otten
__peter__ at web.de
Tue Jun 26 02:57:39 EDT 2012
Josh English wrote:
> On Sunday, June 24, 2012 1:07:45 AM UTC-7, Peter Otten wrote:
>>
>> You cannot access a class instance because even the class itself doesn't
>> exist yet. You could get hold of the class namespace with
>> sys._getframe(),
>>
>> def add_help(f):
>> exec """\
>> def help_%s(self):
>> f = getattr(self, %r)
>> self.show_help(f)
>> """ % (f.__name__[3:], f.__name__) in sys._getframe(1).f_locals
>> return f
>>
>> but here's a simpler approach:
>>
>> import cmd
>>
>> def add_help(f):
>> def help(self):
>> self.show_help(f)
>> f.help = help
>> return f
>>
>>
>> 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()))
>>
>> def __getattr__(self, name):
>> if name.startswith("help_"):
>> helpfunc = getattr(self, "do_" + name[5:]).help
>> setattr(self.__class__, name, helpfunc)
>> return getattr(self, name)
>> raise AttributeError
>>
>> @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()
>> c.cmdloop()
>
>
> Okay. If I understand this, you are adding a help attribute to the class
> method. The help attribute is itself a function.
>
> There is nothing in the documentation (that I have found) that points to
> this solution.
That's because I "invented" it.
@deco
def func(...): ...
is equivalent to
def func(...): ...
func = deco(func)
so only one assignment is ever made in the enclosing namespace. If you have
more targets you have to put them somewhere else. Making the help_xxx()
function an attribute of do_xxx() seemed the obvious choice.
> Even after reading the do_help method in the cmd.Cmd
> source, I don't see this as working.
do_help(name)
looks up a "help_" + name method and invokes it if that lookup succeeds.
> Yet it works.
In the example above do_help() looks for a help_done attribute which doesn't
exist. As a fallback the __getattr__() method is invoked with the
"help_done" argument, finds that the name starts with "help_" and proceeds
with
>> helpfunc = getattr(self, "do_" + name[5:]).help
i. e. it looks for getattr(self, "do_help") which does exist and then stores
its help attribute (if that doesn't exist an AttributeError is implicitly
raised) in helpfunc. helpfunc is then added as "help_done" to the class
>> setattr(self.__class__, name, helpfunc)
and looked up again in the instance:
>> return getattr(self, name)
This time it exists and from the class attribute help_done an instance
method is created, returned from __getattr__() and invoked by do_help().
PS: Stefan is probably right that you should just override do_help()
More information about the Python-list
mailing list