Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!news.mixmin.net!eweka.nl!hq-usenetpeers.eweka.nl!xlned.com!feeder5.xlned.com!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: 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; 'yet.': 0.04; 'doug': 0.05; '"""': 0.07; 'attribute': 0.07; 'class,': 0.07; '*args,': 0.09; 'decorator': 0.09; 'exec': 0.09; 'namespace': 0.09; 'none)': 0.09; 'subject:Getting': 0.09; 'def': 0.11; 'appropriate': 0.15; '"""\\': 0.16; "(i'm": 0.16; '**kwargs)': 0.16; '**kwargs):': 0.16; 'cls': 0.16; 'cmd': 0.16; 'func': 0.16; 'mean,': 0.16; 'name)': 0.16; 'name):': 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; 'shortcut': 0.16; 'site).': 0.16; 'wrote:': 0.17; 'helper': 0.22; 'simpler': 0.22; 'trying': 0.22; 'import': 0.24; 'skip:_ 20': 0.24; 'print': 0.25; 'header:User- Agent:1': 0.26; 'wondering': 0.26; 'raise': 0.27; 'maybe': 0.27; 'error': 0.28; 'creating': 0.28; 'header:X-Complaints-To:1': 0.28; "doesn't": 0.28; 'skip:" 20': 0.28; 'instance': 0.29; 'josh': 0.29; 'code:': 0.29; 'figure': 0.29; 'function': 0.29; 'class': 0.30; "i'm": 0.30; 'skip:_ 10': 0.31; 'this?': 0.32; 'could': 0.33; 'skip:f 40': 0.33; "can't": 0.34; 'handle': 0.35; 'should': 0.35; 'to:addr:python-list': 0.35; 'goes': 0.35; 'there': 0.35; 'but': 0.36; 'subject:with': 0.37; 'possible': 0.37; 'subject:: ': 0.37; 'sure': 0.38; 'level': 0.39; 'method': 0.39; 'to:addr:python.org': 0.39; 'too': 0.39; 'step': 0.39; 'received:org': 0.39; 'easily': 0.39; 'header:Received:5': 0.39; 'to.': 0.61; 'developed': 0.62; 'skip:n 10': 0.63; 'here': 0.65; 'far.': 0.84; 'lazy': 0.91 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: Sun, 24 Jun 2012 10:07:45 +0200 Organization: None References: <9d0c01f4-4430-4a45-8776-20d8dede9e14@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p5084b349.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 112 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1340525279 news.xs4all.nl 6942 [2001:888:2000:d::a6]:57586 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:24382 Josh English wrote: > 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? 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()