Re: Decorator question: prefer class, but only function works

2011-11-14 Thread Steven D'Aprano
On Mon, 14 Nov 2011 17:00:38 -0800, Russell E. Owen wrote: > Oops, I stripped so much out of my example that I stripped the ugly bit. > This is closer to the original and demonstrated the issue: > > def timeMethod(func): > name = func.__name__ + "Duration" > def wrapper(self, *args, **key

Re: Decorator question: prefer class, but only function works

2011-11-14 Thread Russell E. Owen
In article , Ian Kelly wrote: > On Thu, Nov 10, 2011 at 2:52 PM, Russell E. Owen wrote: > > I am trying to write a decorator that times an instance method and > > writes the results to a class member variable. For example: > > > > def timeMethod(func): > >    def wrapper(self, *args, **keyArgs

Re: Decorator question: prefer class, but only function works

2011-11-10 Thread Ian Kelly
On Thu, Nov 10, 2011 at 2:52 PM, Russell E. Owen wrote: > I am trying to write a decorator that times an instance method and > writes the results to a class member variable. For example: > > def timeMethod(func): >    def wrapper(self, *args, **keyArgs): >        t1 = time.time() >        res = fu

Decorator question: prefer class, but only function works

2011-11-10 Thread Russell E. Owen
I am trying to write a decorator that times an instance method and writes the results to a class member variable. For example: def timeMethod(func): def wrapper(self, *args, **keyArgs): t1 = time.time() res = func(self, *args, **keyArgs) duration = time.time() - t1

Re: Decorator question

2011-01-27 Thread Mark Summerfield
On Jan 27, 2:42 am, "Thomas L. Shinnick" wrote: > At 08:17 PM 1/26/2011, Chris wrote: > > >I have a class (A, for instance) that possesses a boolean (A.b, for > >instance) that is liable to change over an instance's lifetime. > > >Many of the methods of this class (A.foo, for instance) should not

Re: Decorator question

2011-01-26 Thread Thomas L. Shinnick
At 08:17 PM 1/26/2011, Chris wrote: I have a class (A, for instance) that possesses a boolean (A.b, for instance) that is liable to change over an instance's lifetime. Many of the methods of this class (A.foo, for instance) should not execute as long as this boolean is false, but should instead

Re: Decorator question

2011-01-26 Thread MRAB
On 27/01/2011 02:17, Chris wrote: I have a class (A, for instance) that possesses a boolean (A.b, for instance) that is liable to change over an instance's lifetime. Many of the methods of this class (A.foo, for instance) should not execute as long as this boolean is false, but should instead ra

Re: Decorator question

2011-01-26 Thread Chris
On Jan 26, 6:17 pm, Chris wrote: > I have a class (A, for instance) that possesses a boolean (A.b, for > instance) that is liable to change over an instance's lifetime. > > Many of the methods of this class (A.foo, for instance) should not > execute as long as this boolean is false, but should ins

Decorator question

2011-01-26 Thread Chris
I have a class (A, for instance) that possesses a boolean (A.b, for instance) that is liable to change over an instance's lifetime. Many of the methods of this class (A.foo, for instance) should not execute as long as this boolean is false, but should instead raise an exception. Can I use a decor

Re: Decorator question

2010-10-17 Thread Lucasm
On 16 Okt, 16:49, Peter Otten <__pete...@web.de> wrote: > Lucasm wrote: > > I have a decorator problem and hope someone is able to help me out/ > > assist me. Thanks in advance. > > > Suppose: > > ### Begin some_library_module ### > > > def some_decorator(some_method): > >     def inner(an_arg, *ar

Re: Decorator question

2010-10-16 Thread Peter Otten
Lucasm wrote: > I have a decorator problem and hope someone is able to help me out/ > assist me. Thanks in advance. > > Suppose: > ### Begin some_library_module ### > > def some_decorator(some_method): > def inner(an_arg, *args, **kwargs): > return some_method(an_arg, *args, **kwargs

Decorator question

2010-10-16 Thread Lucasm
Hello, I have a decorator problem and hope someone is able to help me out/ assist me. Thanks in advance. Suppose: ### Begin some_library_module ### def some_decorator(some_method): def inner(an_arg, *args, **kwargs): return some_method(an_arg, *args, **kwargs) return inner ### E

Re: Decorator question (how to test if decorated function is in a class)

2009-06-20 Thread Martin P. Hellwig
Bruno Desthuilliers wrote: Short answer: this makes no sense. Absolutely right, took me a while to figure that out though :-) Lesson learned (again): If it really seems impossible to do something in Python, it is likely the proposed solution is flawed. -- MPH http://blog.dcuktec.com 'If con

Re: Decorator question (how to test if decorated function is in a class)

2009-06-19 Thread Bruno Desthuilliers
Martin P. Hellwig a écrit : Hi all, I have been trying out to wrap my mind around the advantages of decorators and thought I found a use in one of my experiments. (see code after my sig). Although it works, I think it should be able to do it better. My particular problem is that I want to re

Decorator question (how to test if decorated function is in a class)

2009-06-19 Thread Martin P. Hellwig
Hi all, I have been trying out to wrap my mind around the advantages of decorators and thought I found a use in one of my experiments. (see code after my sig). Although it works, I think it should be able to do it better. My particular problem is that I want to remove an argument (say always

Re: Decorator question

2007-05-23 Thread Michele Simionato
If you are using Python 2.5, you can use functools.update_wrapper to simplify your life. For instance from functools import update_wrapper # requires Python 2.5 def trace(func): def wrapper(*args,**kw): print 'calling %s with args %s' % (func.__name__,args) return func(*args,*

Re: Decorator question

2007-05-23 Thread Giles Brown
On 23 May, 14:46, "Steven W. Orr" <[EMAIL PROTECTED]> wrote: > I just discovered decorators. Very cool. My question is that I can't > figure out how to make a decorator not be restricted to a function so it > would also work on a method. > > Here's my code: > > def g(expr): > def rpt(func): >

Re: Decorator question

2007-05-23 Thread Peter Otten
Steven W. Orr wrote: > I just discovered decorators. Very cool. My question is that I can't > figure out how to make a decorator not be restricted to a function so it > would also work on a method. > > Here's my code: > @g(20) > def f(s): > print 's="%s"'%s > f('Hello') Here you are callin

Decorator question

2007-05-23 Thread Steven W. Orr
I just discovered decorators. Very cool. My question is that I can't figure out how to make a decorator not be restricted to a function so it would also work on a method. Here's my code: def g(expr): def rpt(func): def wrapper(t): for ii in range(expr):

Re: decorator question

2006-01-09 Thread Michele Simionato
Schüle Daniel wrote: > Can someone give me some pointers to the metaprogramming in Python? > links etc Check the Python Wiki. For the decorators in particular I wrote a module that you may find useful. See http://www.phyast.pitt.edu/~micheles/python/decorator.zip http://www.phyast.pitt.edu/~miche

Re: decorator question

2006-01-09 Thread Duncan Booth
Bengt Richter wrote: >>is it possible to pass parameters to a decorator function? >> > Yes, but then the function must return the same kind of thing > a bare decorator-function name would have, which is a function > able to take a single argument of a function and return a function. > > So your de

Re: decorator question

2006-01-08 Thread Schüle Daniel
thx to all now I understand how it works and why it should be done in this way so it's possible to write more than only one declarator >>> def foo(f): ... l = [1] ... def method(*a,**kw): ... f(l, *a, **kw) ... return method ... >>> def bar(f): ... l = [2] ... de

Re: decorator question

2006-01-08 Thread Bengt Richter
On Sun, 08 Jan 2006 23:26:28 +0100, =?ISO-8859-1?Q?Sch=FCle_Daniel?= <[EMAIL PROTECTED]> wrote: [...] > >the code above works fine >but I am wondering wheather it's possible to >write something like this > > >>> def timelogger(f, logfile=sys.stdout): >... def wrapper(*a,**kw): >...

Re: decorator question

2006-01-08 Thread Ralf Schmitt
Schüle Daniel schrieb: > hello NG, > > consider this code > > >>> def timelogger(f): > ... def wrapper(*a,**kw): > ... print "started at %s" % time.ctime() > ... t0 = time.time() > ... f(*a, **kw) > ... t1 = time.time() > ... print

Re: decorator question

2006-01-08 Thread Frank Niessink
Schüle Daniel wrote: > > (1) fails to compile > is it possible to pass parameters to a decorator function? Yes, I think this does what you want: import time, sys def timelogger(logfile=sys.stdout): def actual_timelogger(function): def wrapper(*a,**kw): logfile.write("

decorator question

2006-01-08 Thread Schüle Daniel
hello NG, consider this code >>> def timelogger(f): ... def wrapper(*a,**kw): ... print "started at %s" % time.ctime() ... t0 = time.time() ... f(*a, **kw) ... t1 = time.time() ... print "ended at %s" % time.ctime() ...