Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!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; 'args': 0.05; 'subject:Python': 0.06; 'formed': 0.07; 'method,': 0.07; 'subject:two': 0.07; 'python': 0.08; 'be:': 0.09; 'decorator': 0.09; 'executes': 0.09; 'none):': 0.09; 'run.': 0.09; 'subject:both': 0.09; 'essentially': 0.10; 'output': 0.10; 'def': 0.15; 'method.': 0.15; '**kwargs)': 0.16; '**kwargs):': 0.16; '*args):': 0.16; 'decorators.': 0.16; 'instance)': 0.16; 'pointers': 0.16; 'special,': 0.16; 'subject: \n ': 0.16; 'subject:function': 0.16; 'twice.': 0.16; 'this:': 0.16; 'wrote:': 0.16; 'received:74.125.82.174': 0.19; 'received:mail- wy0-f174.google.com': 0.19; 'appropriate': 0.20; 'trying': 0.21; 'header:In-Reply-To:1': 0.22; 'once.': 0.23; 'received:192.168.1.100': 0.23; 'cache': 0.24; 'code': 0.25; 'function': 0.27; 'all,': 0.28; 'print': 0.29; 'definition': 0.30; 'decorators': 0.30; 'email name:': 0.30; 'thanks': 0.30; 'error': 0.32; 'to:addr:python-list': 0.33; 'that,': 0.33; 'calling': 0.33; "i've": 0.34; 'header:User-Agent:1': 0.34; 'message- id:@gmail.com': 0.34; 'function.': 0.34; 'from:charset:iso-8859-1': 0.35; 'subject:How': 0.35; 'post': 0.36; 'doing': 0.36; 'checks': 0.37; 'executing': 0.37; 'using': 0.37; 'but': 0.37; 'something': 0.37; 'received:74.125.82': 0.38; 'question,': 0.38; 'received:google.com': 0.38; 'subject:: ': 0.39; 'received:192': 0.39; 'user': 0.39; 'to:addr:python.org': 0.39; 'received:74.125': 0.39; 'called': 0.40; "it's": 0.40; 'your': 0.61; 'chain': 0.66; 'response.': 0.67; 'login': 0.69; 'cleared': 0.84; 'subject:call': 0.91; 'subject:want': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=L5MB0skHxd90L1XoKWxUi9Vy1HWiJnrYiTohCoeHbDc=; b=Ywisc6swQJ55thwOxtfb7CoiayA0o6j/7a4EK1mb+eFnkAUmQNJwwfd6L8TMKHNWV2 jg/FpAB+PBcF3A5WoaU1RveowNiz+Jt0VBA/oYA7aieVhiBWFRyBmLzyInvYl6WOiDSI 29wXCwUeuMYITjvKHRz+FLs9+vRGA34Z3LdWw= Date: Sat, 06 Aug 2011 09:49:30 +0200 From: =?ISO-8859-1?Q?Rafael_Dur=E1n_Casta=F1eda?= User-Agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.18) Gecko/20110617 Thunderbird/3.1.11 MIME-Version: 1.0 To: python-list@python.org Subject: Re: How do I implement two decorators in Python both of which would eventually want to call the calling function References: <8b087624-4e05-4d2e-bf4f-78383202b7e1@28g2000pry.googlegroups.com> In-Reply-To: <8b087624-4e05-4d2e-bf4f-78383202b7e1@28g2000pry.googlegroups.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit 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: 68 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1312616975 news.xs4all.nl 23913 [2001:888:2000:d::a6]:55301 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:10960 You don't need doing something special, each decorator returns a new function that and only the result function formed by all decorators will be run. Consider this: def clear_cache(func): def decorator(*args, **kwargs): print "cache cleared" return func(*args, **kwargs) return decorator def login(func): def decorator(*args, **kwargs): print "login" return func(*args, **kwargs) return decorator @login @clear_cache def post(msg= "POST", *args): print msg return args result = post("POST", "something") print result The output will be: login cache cleared POST ('something',) On 06/08/11 07:49, Devraj wrote: > Hi all, > > I am trying to simply my Web application handlers, by using Python > decorators. > > Essentially I want to use decorators to abstract code that checks for > authenticated sessions and the other that checks to see if the cache > provider (Memcache in this instance) has a suitable response. > > Consider this method definition with the decorators: > > @auth.login_required > @cache.clear > def post(self, facility_type_id = None): > > auth.login_required checks to see if the user is logged in, otherwise > returns an appropriate error message, or executes the original > function. > > cache.clear would check to to see if the cache has a particular key > and drop that, before it executes the calling method. > > Both auth.login_required and cache.clear would want to eventually > execute the calling method (post). > > > From what I've read both, doing what I am doing now would execute the > calling method (post) twice. > > My question, how do I chain decorators that end up executing the > calling method, but ensure that it's only called once. > > Appreciate any pointers and thanks for your time.