Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #24461

Re: Getting lazy with decorators

Path csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!ecngs!feeder2.ecngs.de!newsfeed.freenet.ag!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'source,': 0.04; 'yet.': 0.04; '"""': 0.07; 'assignment': 0.07; 'attribute': 0.07; 'method.': 0.07; '*args,': 0.09; 'created,': 0.09; 'exec': 0.09; 'lookup': 0.09; 'namespace': 0.09; 'override': 0.09; 'seemed': 0.09; 'subject:Getting': 0.09; 'works.': 0.09; 'def': 0.11; 'looked': 0.11; '"""\\': 0.16; '**kwargs)': 0.16; '**kwargs):': 0.16; '24,': 0.16; 'argument,': 0.16; 'cmd': 0.16; 'exist.': 0.16; 'fallback': 0.16; 'func': 0.16; 'instance:': 0.16; 'invokes': 0.16; 'mean,': 0.16; 'name)': 0.16; 'name):': 0.16; 'namespace.': 0.16; 'proceeds': 0.16; 'quits': 0.16; 'received:80.91': 0.16; 'received:80.91.229': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:gmane.org': 0.16; 'received:list': 0.16; 'received:t-dialin.net': 0.16; 'targets': 0.16; 'wrote:': 0.17; 'stefan': 0.18; 'working.': 0.18; 'solution.': 0.18; 'exists': 0.22; 'simpler': 0.22; 'equivalent': 0.23; 'import': 0.24; 'somewhere': 0.24; 'example': 0.24; 'skip:_ 20': 0.24; 'print': 0.25; 'header:User-Agent:1': 0.26; 'raise': 0.27; 'header:X -Complaints-To:1': 0.28; "doesn't": 0.28; 'skip:" 20': 0.28; 'instance': 0.29; 'josh': 0.29; 'starts': 0.29; 'function': 0.29; 'probably': 0.30; 'class': 0.30; 'looks': 0.31; 'skip:_ 10': 0.31; 'points': 0.32; 'could': 0.33; 'should': 0.35; 'returned': 0.35; 'to:addr:python-list': 0.35; 'goes': 0.35; 'there': 0.35; 'does': 0.36; 'but': 0.36; 'subject:with': 0.37; 'subject:: ': 0.37; 'level': 0.39; 'method': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.39; 'header:Received:5': 0.39; 'it.': 0.39; '(that': 0.63; 'more': 0.63; 'skip:n 10': 0.63; 'else.': 0.66; 'making': 0.67; 'obvious': 0.72; 'otten': 0.84; 'choice.': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: Getting lazy with decorators
Date Tue, 26 Jun 2012 08:57:39 +0200
Organization None
References <9d0c01f4-4430-4a45-8776-20d8dede9e14@googlegroups.com> <mailman.1442.1340525279.4697.python-list@python.org> <513d25ea-e977-4b84-b388-bfb23c171aae@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p5084be63.dip.t-dialin.net
User-Agent KNode/4.7.3
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.12
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.1510.1340693868.4697.python-list@python.org> (permalink)
Lines 106
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1340693868 news.xs4all.nl 6912 [2001:888:2000:d::a6]:47160
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:24461

Show key headers only | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Getting lazy with decorators Josh English <Joshua.R.English@gmail.com> - 2012-06-23 18:58 -0700
  Re: Getting lazy with decorators Peter Otten <__peter__@web.de> - 2012-06-24 10:07 +0200
    Re: Getting lazy with decorators Josh English <Joshua.R.English@gmail.com> - 2012-06-25 13:04 -0700
      Re: Getting lazy with decorators Peter Otten <__peter__@web.de> - 2012-06-26 08:57 +0200
        Re: Getting lazy with decorators Josh English <Joshua.R.English@gmail.com> - 2012-06-27 16:09 -0700
        Re: Getting lazy with decorators Josh English <Joshua.R.English@gmail.com> - 2012-06-27 16:09 -0700
      Re: Getting lazy with decorators Peter Otten <__peter__@web.de> - 2012-06-26 09:03 +0200
    Re: Getting lazy with decorators Josh English <Joshua.R.English@gmail.com> - 2012-06-25 13:04 -0700
  Re: Getting lazy with decorators "Stefan H. Holek" <stefan@epy.co.at> - 2012-06-24 12:44 +0200

csiph-web