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


Groups > comp.lang.python > #16805 > unrolled thread

I love the decorator in Python!!!

Started by88888 Dihedral <dihedral88888@googlemail.com>
First post2011-12-07 23:18 -0800
Last post2011-12-08 15:56 -0800
Articles 12 on this page of 32 — 10 participants

Back to article view | Back to comp.lang.python


Contents

  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

Page 2 of 2 — ← Prev page 1 [2]


#16905

FromRobert Kern <robert.kern@gmail.com>
Date2011-12-09 10:08 +0000
Message-ID<mailman.3467.1323425305.27778.python-list@python.org>
In reply to#16897
On 12/9/11 5:02 AM, alex23 wrote:
> On Dec 9, 2:38 am, Chris Angelico<ros...@gmail.com>  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.
>
> The 3rd party 'decorator' module takes care of issues like docstrings
> &  function signatures. I'd really like to see some of that
> functionality in the stdlib though.

Much of it is:

   http://docs.python.org/library/functools#functools.update_wrapper

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

[toc] | [prev] | [next] | [standalone]


#17007

Fromalex23 <wuwei23@gmail.com>
Date2011-12-11 19:36 -0800
Message-ID<82150b53-f1a5-432d-aae2-39bf6235be1b@d5g2000prf.googlegroups.com>
In reply to#16905
On Dec 9, 8:08 pm, Robert Kern <robert.k...@gmail.com> wrote:
> On 12/9/11 5:02 AM, alex23 wrote:
> > The 3rd party 'decorator' module takes care of issues like docstrings
> > &  function signatures. I'd really like to see some of that
> > functionality in the stdlib though.
>
> Much of it is:
>
>    http://docs.python.org/library/functools#functools.update_wrapper

Ah, cheers :) Is that a recent addition? The lack of note makes it
seem like it was there from the beginning?

[toc] | [prev] | [next] | [standalone]


#17011

From88888 Dihedral <dihedral88888@googlemail.com>
Date2011-12-11 20:51 -0800
Message-ID<13587584.2147.1323665518804.JavaMail.geo-discussion-forums@prnx7>
In reply to#17007
On Monday, December 12, 2011 11:36:07 AM UTC+8, alex23 wrote:
> On Dec 9, 8:08 pm, Robert Kern <rober...@gmail.com> wrote:
> > On 12/9/11 5:02 AM, alex23 wrote:
> > > The 3rd party 'decorator' module takes care of issues like docstrings
> > > &  function signatures. I'd really like to see some of that
> > > functionality in the stdlib though.
> >
> > Much of it is:
> >
> >    http://docs.python.org/library/functools#functools.update_wrapper
> 
> Ah, cheers :) Is that a recent addition? The lack of note makes it
> seem like it was there from the beginning?

To wrap a function properly is different from the 1-line lampda. 

This is really functional programming.

Every function can be decorated to change into a different one easily. 

There is  a method to replace every return action  of a python function 
into an  yield action without the source code.


 

 

[toc] | [prev] | [next] | [standalone]


#17015

Fromalex23 <wuwei23@gmail.com>
Date2011-12-11 21:47 -0800
Message-ID<4fa80db8-deec-4632-8402-f63f3fb91740@h37g2000pri.googlegroups.com>
In reply to#17011
On Dec 12, 2:51 pm, 88888 Dihedral <dihedral88...@googlemail.com>
wrote:
> To wrap a function properly is different from the 1-line lampda.
>
> This is really functional programming.
>
> Every function can be decorated to change into a different one easily.
>
> There is  a method to replace every return action  of a python function
> into an  yield action without the source code.

How does this have _anything_ to do with my exchange with Robert?

If you're _not_ a markov chainer, you're trying way too hard to show
off what you know, and very little of it seems relevant to the thread.

[toc] | [prev] | [next] | [standalone]


#17034

From88888 Dihedral <dihedral88888@googlemail.com>
Date2011-12-12 04:16 -0800
Message-ID<7918199.244.1323692193737.JavaMail.geo-discussion-forums@prlu4>
In reply to#17015
On Monday, December 12, 2011 1:47:52 PM UTC+8, alex23 wrote:
> On Dec 12, 2:51 pm, 88888 Dihedral <dihedr...@googlemail.com>
> wrote:
> > To wrap a function properly is different from the 1-line lampda.
> >
> > This is really functional programming.
> >
> > Every function can be decorated to change into a different one easily.
> >
> > There is  a method to replace every return action  of a python function
> > into an  yield action without the source code.
> 
> How does this have _anything_ to do with my exchange with Robert?
> 
> If you're _not_ a markov chainer, you're trying way too hard to show
> off what you know, and very little of it seems relevant to the thread.

I think in the CS  way in the 5th generation computer languages. 
But I also think in the hardware way in the 2,3,4th generations.

Why do you need to spawn a thread or a process  that can be decorated by 
wrapping  an yield for all kinds of  parameter checking of functions?

A lousy written function in any computer language indicates
a bad taste of art. 

     

[toc] | [prev] | [next] | [standalone]


#17024

FromEthan Furman <ethan@stoneleaf.us>
Date2011-12-11 23:47 -0800
Message-ID<mailman.3535.1323679909.27778.python-list@python.org>
In reply to#17007
alex23 wrote:
> On Dec 9, 8:08 pm, Robert Kern <robert.k...@gmail.com> wrote:
>> On 12/9/11 5:02 AM, alex23 wrote:
>>> The 3rd party 'decorator' module takes care of issues like docstrings
>>> &  function signatures. I'd really like to see some of that
>>> functionality in the stdlib though.
>> Much of it is:
>>
>>    http://docs.python.org/library/functools#functools.update_wrapper
> 
> Ah, cheers :) Is that a recent addition? The lack of note makes it
> seem like it was there from the beginning?

Not sure how recent it is, but I do know it does *not* handle the signature.

~Ethan~

[toc] | [prev] | [next] | [standalone]


#17059

FromRobert Kern <robert.kern@gmail.com>
Date2011-12-12 16:27 +0000
Message-ID<mailman.3557.1323707274.27778.python-list@python.org>
In reply to#17007
On 12/12/11 3:36 AM, alex23 wrote:
> On Dec 9, 8:08 pm, Robert Kern<robert.k...@gmail.com>  wrote:
>> On 12/9/11 5:02 AM, alex23 wrote:
>>> The 3rd party 'decorator' module takes care of issues like docstrings
>>> &    function signatures. I'd really like to see some of that
>>> functionality in the stdlib though.
>>
>> Much of it is:
>>
>>     http://docs.python.org/library/functools#functools.update_wrapper
>
> Ah, cheers :) Is that a recent addition? The lack of note makes it
> seem like it was there from the beginning?

The module was added in Python 2.5 as noted at the top of the page.

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco

[toc] | [prev] | [next] | [standalone]


#17094

Fromalex23 <wuwei23@gmail.com>
Date2011-12-12 16:46 -0800
Message-ID<ece4a924-59a0-4cc5-b64d-c8cf79ea3a73@b14g2000prn.googlegroups.com>
In reply to#17059
On Dec 13, 2:27 am, Robert Kern <robert.k...@gmail.com> wrote:
> On 12/12/11 3:36 AM, alex23 wrote:
>
> > On Dec 9, 8:08 pm, Robert Kern<robert.k...@gmail.com>  wrote:
> >> On 12/9/11 5:02 AM, alex23 wrote:
> >>> The 3rd party 'decorator' module takes care of issues like docstrings
> >>> &    function signatures. I'd really like to see some of that
> >>> functionality in the stdlib though.
>
> >> Much of it is:
>
> >>    http://docs.python.org/library/functools#functools.update_wrapper
>
> > Ah, cheers :) Is that a recent addition? The lack of note makes it
> > seem like it was there from the beginning?
>
> The module was added in Python 2.5 as noted at the top of the page.

I had thought you meant it now included function signature handling,
as that was the context at the time. No biggie.

[toc] | [prev] | [next] | [standalone]


#16851

FromAndrew Berg <bahamutzero8825@gmail.com>
Date2011-12-08 10:54 -0600
Message-ID<mailman.3433.1323363307.27778.python-list@python.org>
In reply to#16812
On 12/8/2011 10:38 AM, 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 remember seeing this in a PEP that is planned to be implemented in 3.3.

-- 
CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0

[toc] | [prev] | [next] | [standalone]


#16852

FromAndrew Berg <bahamutzero8825@gmail.com>
Date2011-12-08 10:55 -0600
Message-ID<mailman.3434.1323363336.27778.python-list@python.org>
In reply to#16812
Decorators are great for adding common functionality to several
functions without duplicating code. For example, I have one for my IRC
bot that checks that the person sending the command is authorized to use
the command. It's only "if mask in owner list then execute function else
say access denied", but it simplifies the code and the intent of each
function that uses it becomes more obvious. I have another that puts
functions in a common try/except clause, which again simplifies the
functions and makes them easier to understand.

-- 
CPython 3.2.2 | Windows NT 6.1.7601.17640 | Thunderbird 7.0

[toc] | [prev] | [next] | [standalone]


#16885

FromEthan Furman <ethan@stoneleaf.us>
Date2011-12-08 15:34 -0800
Message-ID<mailman.3458.1323388282.27778.python-list@python.org>
In reply to#16812
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~

[toc] | [prev] | [next] | [standalone]


#16886

FromEthan Furman <ethan@stoneleaf.us>
Date2011-12-08 15:56 -0800
Message-ID<mailman.3459.1323391888.27778.python-list@python.org>
In reply to#16812
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.

And here it is with Michele's decorator module:

2.x code (probably the same in 3.x, but haven't tested)
----------------------------------------------------------------
from decorator import decorator

debugmode = True

def trace(func):
     if debugmode:
         @decorator
         def traced(func, *a, **ka):
             print(">", func.__name__, a, ka)
             result = func(*a, **ka)
             print("<", func.__name__)
             return result
         return traced(func)
     return func

@trace
def test(x):
     "a simple test"
     print("Test! "+x)
     return 5
----------------------------------------------------------------

~Ethan~

[toc] | [prev] | [standalone]


Page 2 of 2 — ← Prev page 1 [2]

Back to top | Article view | comp.lang.python


csiph-web