Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed5.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; 'else:': 0.03; 'example:': 0.03; 'exception': 0.03; 'argument': 0.04; 'one?': 0.05; 'startup': 0.05; 'badly': 0.07; 'decorator': 0.07; 'important,': 0.07; 'parameter': 0.07; 'raises': 0.07; 'sphinx': 0.07; 'users,': 0.07; 'python': 0.09; 'appropriate.': 0.09; 'booth': 0.09; 'does,': 0.09; 'timeout': 0.09; 'timeout)': 0.09; 'way:': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'subject:not': 0.11; 'adjustment': 0.16; 'argument.': 0.16; 'cli': 0.16; 'describing': 0.16; 'doc,': 0.16; 'email addr:functools.wraps(func)': 0.16; 'suggested,': 0.16; 'timeout):': 0.16; 'wrote:': 0.17; 'skip:` 20': 0.17; 'specify': 0.17; '(or': 0.18; 'appropriate': 0.20; 'code.': 0.20; 'effort.': 0.22; 'exceptions': 0.22; 'posted': 0.22; 'runs': 0.22; 'work,': 0.22; "i'd": 0.22; 'cc:2**0': 0.23; 'work.': 0.23; 'cc:no real name:2**0': 0.24; 'idea': 0.24; 'second': 0.24; 'device': 0.24; 'command': 0.24; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'wrote': 0.26; 'values': 0.26; 'replace': 0.27; 'interface': 0.27; 'run': 0.28; 'checking.': 0.29; 'consistency': 0.29; 'email name:': 0.29; 'publicly': 0.29; 'steven': 0.29; 'url:mailman': 0.29; 'keyword': 0.30; 'function': 0.30; 'feedback': 0.30; 'code': 0.31; 'url:python': 0.32; '-----': 0.32; 'url:listinfo': 0.32; 'adjust': 0.33; 'function.': 0.33; 'platform,': 0.33; 'raising': 0.33; 'tech': 0.33; 'another': 0.33; 'agree': 0.34; 'whatever': 0.35; 'returning': 0.35; 'sometimes': 0.35; 'something': 0.35; 'explain': 0.36; 'but': 0.36; 'url:org': 0.36; "didn't": 0.36; 'method': 0.36; 'test': 0.36; 'itself': 0.37; 'one,': 0.37; 'subject:: ': 0.38; 'some': 0.38; 'instead': 0.39; 'end': 0.40; 'url:mail': 0.40; 'your': 0.60; 'easy': 0.60; 'remove': 0.61; 'save': 0.61; 'received:194': 0.61; 'spending': 0.61; 'different': 0.63; 'ever': 0.63; 'times': 0.63; 'url:blogspot': 0.64; 'within': 0.64; 'our': 0.65; 'else.': 0.65; 'on...': 0.65; 'hours': 0.66; 'life': 0.66; 'consequences': 0.71; 'day': 0.73; 'goal': 0.74; 'heavy': 0.83; 'decorate': 0.84; 'misuse': 0.84; 'colleagues.': 0.91; 'dozen': 0.91; 'advertise': 0.96; 'colleagues': 0.97 X-IronPort-AV: E=Sophos;i="4.80,423,1344204000"; d="scan'208";a="725835" X-Virus-Scanned: amavisd-new at zimbra.sequans.com Date: Fri, 14 Sep 2012 15:22:26 +0200 (CEST) From: Jean-Michel Pichavant To: duncan booth In-Reply-To: Subject: Re: Decorators not worth the effort MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit X-Mailer: Zimbra 7.2.0_GA_2669 (ZimbraWebClient - GC7 (Linux)/7.2.0_GA_2669) Cc: python-list@python.org X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 95 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1347628931 news.xs4all.nl 6880 [2001:888:2000:d::a6]:33594 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:29166 ----- Original Message ----- > Jean-Michel Pichavant wrote: > > > I wrote the following one, used to decorate any function that > > access > > an equipment, it raises an exception when the timeout expires. The > > timeout is adapted to the platform, ASIC of FPGA so people don't > > need > > to specify everytime one timeout per platform. > > > > In the end it would replace > > > > def boot(self, timeout=15): > > if FPGA: > > self.sendCmd("bootMe", timeout=timeout*3) > > else: > > self.sendCmd("bootMe", timeout=timeout) > > > > with > > > > @timeout(15) > > def boot(self, timeout=None): > > self.sendCmd("bootMe", timeout) > > > > I wrote a nice documentation with sphinx to explain this, how to > > use > > it, how it can improve code. After spending hours on the decorator > > + > > doc, feedback from my colleagues : What the F... !! > > > > I'd agree with your colleagues. How are you going to ensure that all > relevant functions are decorated and yet no decorated function ever > calls another decorated one? > > From the code you posted it would seem appropriate that the > adjustment > of the timeout parameter happen in the `sendCmd()` method itself and > nowhere else. Alternatively use named values for different categories > of > timeouts and adjust them on startup so instead of a default of > `timeout= > 15` you would have a default `timeout=MEDIUM_TIMEOUT` or whatever > name > is appropriate. > > -- > Duncan Booth http://kupuguy.blogspot.com > -- > http://mail.python.org/mailman/listinfo/python-list All functions set different timeout values, I cannot use a DEFAULT_VALUE. All functions are design in the same way: def doSomeAction(self, timeout): preprocess() self.sendCmd('relatedAction', timeout) # send the command to the device CLI interface postprocess() Ultimately, the goal is to have something like @timeout(2) def doAction1 @timeout(4) def doAction2 @timeout(12) def doAction3 and so on... (1 second is important, because there's a platform I remove from my example, didn't want to advertise publicly tech used by the company, that runs 1000 times slower) Additionally, the multiple check I run within the decorator is for consistency check and argument checking. I though it was a good idea because our python engine is used by a dozen of engineers to control equipment, and any misuse of this new decorator would lead to badly configured timeouts with heavy consequences on the test sessions. Sometimes a RTFM is not enough, when you need to make this work, you slip on your Batman costume like Steven suggested, and you save the day (or so I though :) ) by raising nice exceptions about missing keyword argument. But let's forget about my poor example, I end up describing my life which is pretty pointless. Here's Steven example: # Untested! def timeout(t=15): # Decorator factory. Return a decorator to actually do the work. if FPGA: t *= 3 def decorator(func): @functools.wraps(func) def inner(self, timeout): self.sendCmd("bootMe", timeout=t) return inner return decorator I can assure you, that for some python users, it's is not easy to understand what it does, this function returning a function which returns another (wrapped) function. It requires some effort. JM