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


Groups > comp.lang.python > #16885

Re: I love the decorator in Python!!!

Path csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!news-transit.tcx.org.uk!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed5.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ethan@stoneleaf.us>
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; 'subject:Python': 0.05; 'exec': 0.07; 'roll': 0.07; 'decorator': 0.09; 'from:addr:ethan': 0.09; 'from:addr:stoneleaf.us': 0.09; 'from:name:ethan furman': 0.09; 'func': 0.09; 'idle,': 0.09; 'message-id:@stoneleaf.us': 0.09; 'module)': 0.09; 'received:gator410.hostgator.com': 0.09; '~ethan~': 0.09; 'def': 0.13; 'argument': 0.15; 'downside': 0.16; "function's": 0.16; 'received:72.11': 0.16; 'received:72.11.125': 0.16; 'received:72.11.125.166': 0.16; 'tracebacks': 0.16; 'this:': 0.16; 'wrote:': 0.18; '2.x': 0.18; '(which': 0.19; "aren't": 0.21; 'header:In-Reply-To:1': 0.22; 'skip:{ 20': 0.23; 'somehow': 0.23; 'code': 0.25; 'skip:_ 20': 0.26; 'testing': 0.26; 'function': 0.27; 'do.': 0.28; 'looks': 0.29; 'clean.': 0.30; 'chris': 0.30; 'skip:- 70': 0.31; 'quite': 0.32; 'actual': 0.32; 'header:User- Agent:1': 0.33; 'rather': 0.33; 'instead': 0.33; 'to:addr:python- list': 0.34; 'see,': 0.34; 'list.': 0.35; 'something': 0.35; 'own,': 0.37; 'but': 0.37; 'useful': 0.38; 'help': 0.39; 'to:addr:python.org': 0.40; 'your': 0.61; 'received:websitewelcome.com': 0.64; 'piece': 0.66; 'received:184': 0.67; 'subject:!!!': 0.76; 'trim': 0.84
Date Thu, 08 Dec 2011 15:34:13 -0800
From Ethan Furman <ethan@stoneleaf.us>
User-Agent Thunderbird 1.5.0.10 (Windows/20070221)
MIME-Version 1.0
To python-list@python.org
Subject Re: I love the decorator in Python!!!
References <29996186.628.1323328726122.JavaMail.geo-discussion-forums@prfb7> <jbpu9j$1af$1@r03.glglgl.gl> <jbq6li$p9o$1@dough.gmane.org> <CAPTjJmqEjBWkyP9MDooqTUUv2=D9HV21WZZJv_6cWjJPterebw@mail.gmail.com> <jbqobb$s5d$1@dough.gmane.org> <CAPTjJmoejSiNWfJcnEt0PTfYNr=9Yrn+rcW+J7qBM5+WhmUKOw@mail.gmail.com>
In-Reply-To <CAPTjJmoejSiNWfJcnEt0PTfYNr=9Yrn+rcW+J7qBM5+WhmUKOw@mail.gmail.com>
Content-Type text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding 7bit
X-AntiAbuse This header was added to track abuse, please include it with any abuse report
X-AntiAbuse Primary Hostname - gator410.hostgator.com
X-AntiAbuse Original Domain - python.org
X-AntiAbuse Originator/Caller UID/GID - [47 12] / [47 12]
X-AntiAbuse Sender Address Domain - stoneleaf.us
X-BWhitelist no
X-Source
X-Source-Args
X-Source-Dir
X-Source-Sender mail.admailinc.com ([192.168.10.136]) [72.11.125.166]:4003
X-Source-Auth ethan+stoneleaf.us
X-Email-Count 1
X-Source-Cap dG9idWs7dG9idWs7Z2F0b3I0MTAuaG9zdGdhdG9yLmNvbQ==
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.3458.1323388282.27778.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1323388282 news.xs4all.nl 6841 [2001:888:2000:d::a6]:47993
X-Complaints-To abuse@xs4all.nl
Xref x330-a1.tempe.blueboxinc.net comp.lang.python:16885

Show key headers only | View raw


Chris Angelico wrote:
> One piece of sophistication that I would rather like to see, but don't
> know how to do. Instead of *args,**kwargs, is it possible to somehow
> copy in the function's actual signature? I was testing this out in
> IDLE, and the fly help for the function no longer gave useful info
> about its argument list.

If you want to roll your own, it looks something like this:

2.x code
----------------------------------------------------------------------
def trace(func, debugmode=debugmode):
     if debugmode:
         name = func.__name__
         argspec = inspect.getargspec(func)
         signature = inspect.formatargspec(
             formatvalue=lambda val: "", *argspec)[1:-1] # trim parens
         new_func = (
                     'def _wrapper_(%(signature)s):\n'
                     '    print(">"+func.__name__)\n'
                     '    result = func(%(signature)s)\n'
                     '    print("<"+func.__name__)\n'
                     '    return result\n'
                     % {'signature':signature}
                     )
         evaldict = {'func':func}
         exec new_func in evaldict
         wrapped = evaldict['_wrapper_']
         wrapped.__name__ = name
         wrapped.__doc__ = func.__doc__
         wrapped.__module__ = func.__module__
         wrapped.__dict__ = func.__dict__
         wrapped.func_defaults = func.func_defaults
         return wrapped
     return func
----------------------------------------------------------------------

The downside (which you get even with Michele's decorator module) is 
that tracebacks aren't quite as clean.

~Ethan~

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


Thread

I love the decorator in Python!!! 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-07 23:18 -0800
  Re: I love the decorator in Python!!! Chris Rebert <clp2@rebertia.com> - 2011-12-08 00:04 -0800
  Re: I love the decorator in Python!!! Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-12-08 09:59 +0100
    Re: I love the decorator in Python!!! Chris Angelico <rosuav@gmail.com> - 2011-12-08 20:37 +1100
    Re: I love the decorator in Python!!! K.-Michael Aye <kmichael.aye@gmail.com> - 2011-12-08 12:22 +0100
      Re: I love the decorator in Python!!! Francesco Bochicchio <bieffe62@gmail.com> - 2011-12-09 04:00 -0800
    Re: I love the decorator in Python!!! Chris Angelico <rosuav@gmail.com> - 2011-12-08 22:43 +1100
      Re: I love the decorator in Python!!! Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-12-09 10:57 +0100
      Re: I love the decorator in Python!!! 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-09 10:28 -0800
        Re: I love the decorator in Python!!! 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-09 16:21 -0800
        Re: I love the decorator in Python!!! 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-09 16:21 -0800
          Re: I love the decorator in Python!!! 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-10 06:04 -0800
            Re: I love the decorator in Python!!! 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-10 06:09 -0800
              Re: I love the decorator in Python!!! alex23 <wuwei23@gmail.com> - 2011-12-11 19:35 -0800
            Re: I love the decorator in Python!!! 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-10 06:09 -0800
          Re: I love the decorator in Python!!! 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-10 06:04 -0800
      Re: I love the decorator in Python!!! 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-09 10:28 -0800
    Re: I love the decorator in Python!!! K.-Michael Aye <kmichael.aye@gmail.com> - 2011-12-08 17:24 +0100
    Re: I love the decorator in Python!!! Chris Angelico <rosuav@gmail.com> - 2011-12-09 03:38 +1100
      Re: I love the decorator in Python!!! alex23 <wuwei23@gmail.com> - 2011-12-08 21:02 -0800
        Re: I love the decorator in Python!!! Robert Kern <robert.kern@gmail.com> - 2011-12-09 10:08 +0000
          Re: I love the decorator in Python!!! alex23 <wuwei23@gmail.com> - 2011-12-11 19:36 -0800
            Re: I love the decorator in Python!!! 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-11 20:51 -0800
              Re: I love the decorator in Python!!! alex23 <wuwei23@gmail.com> - 2011-12-11 21:47 -0800
                Re: I love the decorator in Python!!! 88888 Dihedral <dihedral88888@googlemail.com> - 2011-12-12 04:16 -0800
            Re: I love the decorator in Python!!! Ethan Furman <ethan@stoneleaf.us> - 2011-12-11 23:47 -0800
            Re: I love the decorator in Python!!! Robert Kern <robert.kern@gmail.com> - 2011-12-12 16:27 +0000
              Re: I love the decorator in Python!!! alex23 <wuwei23@gmail.com> - 2011-12-12 16:46 -0800
    Re: I love the decorator in Python!!! Andrew Berg <bahamutzero8825@gmail.com> - 2011-12-08 10:54 -0600
    Re: I love the decorator in Python!!! Andrew Berg <bahamutzero8825@gmail.com> - 2011-12-08 10:55 -0600
    Re: I love the decorator in Python!!! Ethan Furman <ethan@stoneleaf.us> - 2011-12-08 15:34 -0800
    Re: I love the decorator in Python!!! Ethan Furman <ethan@stoneleaf.us> - 2011-12-08 15:56 -0800

csiph-web