Re: Decorator with parameters

2020-04-06 Thread Chris Angelico
On Mon, Apr 6, 2020 at 6:36 PM ast wrote: > > Hello > > I wrote a decorator to add a cache to functions. > I realized that cache dictionnary could be defined > as an object attribute or as a local variable in > method __call__. > Both seems to work properly. > Can you see any differences between t

Re: decorator needs access to variables where it is used.

2019-10-12 Thread Serhiy Storchaka
09.10.19 14:02, Chris Angelico пише: The decorator has full access to the function object, including a reference to that function's module globals. def trace(func): log = func.__globals__["log"] ... proceed as before As long as you can depend on "log" always being a module-level (glob

Re: decorator needs access to variables where it is used.

2019-10-10 Thread Antoon Pardon
On 9/10/19 13:02, Chris Angelico wrote: > On Wed, Oct 9, 2019 at 9:53 PM Antoon Pardon wrote: >> I have some logging utilities so that when I write library code, I just use >> the following. >> >> from logutil import Logger >> >> log = Logger(__name__) > Are you always absolutely consistent with

Re: decorator needs access to variables where it is used.

2019-10-09 Thread Peter Otten
Antoon Pardon wrote: > I have some logging utilities so that when I write library code, I just > use the following. > > from logutil import Logger > > log = Logger(__name__) If logutil is under your control you can make log a callable object with a tracing method: [logutil.py] class Logger:

Re: decorator needs access to variables where it is used.

2019-10-09 Thread Chris Angelico
On Wed, Oct 9, 2019 at 9:53 PM Antoon Pardon wrote: > > I have some logging utilities so that when I write library code, I just use > the following. > > from logutil import Logger > > log = Logger(__name__) Are you always absolutely consistent with this? Do you always have it as a module-level v

Re: Decorator

2017-02-09 Thread Chris Angelico
On Fri, Feb 10, 2017 at 10:18 AM, Terry Reedy wrote: > Perhaps "There are also cases in which evaluating 'fi(arg)' before rather > than after the def statement makes a difference." should be added. Perhaps not. The word "roughly" covers the odd edge cases, and Python has a general principle of ev

Re: Decorator

2017-02-09 Thread Terry Reedy
On 2/9/2017 2:03 AM, ast wrote: Hi In python courses I read, it is explained that @decor def f(): pass is equivalent to: def f(): pass f = decor(f) But that's not always true. See this code Which is why the official docs now say 'roughly equivalent' ''' For example, the following co

Re: Decorator

2017-02-09 Thread ast
"Steven D'Aprano" a écrit dans le message de news:589c2cdc$0$1584$c3e8da3$54964...@news.astraweb.com... On Thu, 09 Feb 2017 08:03:32 +0100, ast wrote: Congratulations, you've found a microscopic corner of the language where the two different ways of applying decorators are different. Are y

Re: Decorator

2017-02-09 Thread Steven D'Aprano
On Thu, 09 Feb 2017 08:03:32 +0100, ast wrote: > Hi > > In python courses I read, it is explained that > > @decor > def f(): > pass > > is equivalent to: > > def f(): > pass > > f = decor(f) > > But that's not always true. See this code [...] > any comment ? Congratulations, you've

Re: Decorator

2017-02-08 Thread Chris Angelico
On Thu, Feb 9, 2017 at 6:03 PM, ast wrote: > class Temperature: > def __init__(self): >self.value = 0 > > # @property >def celsius(self):return self.value > celsius = property(celsius) > # @celsius.setter def celsius(self, value):<-- > overwri

Re: Decorator

2014-03-20 Thread Peter Otten
Dave Angel wrote: > Peter Otten <__pete...@web.de> Wrote in message: > >> >> In your code change >> >> fib1 = isOddMy(fib) >> >> to >> >> fib = isOddMy(fib) >> >> and the without@ version will produce the same output as the with@ >> version. >> >> > > I expect that one more thing is need

Re: Decorator

2014-03-20 Thread Dave Angel
Peter Otten <__pete...@web.de> Wrote in message: > > In your code change > > fib1 = isOddMy(fib) > > to > > fib = isOddMy(fib) > > and the without@ version will produce the same output as the with@ version. > > I expect that one more thing is needed, since the above is inside a functio

Re: Decorator

2014-03-20 Thread Peter Otten
muru kessan wrote: > Is there a difference between accessing decorators via '@' symbol and > hard coding that ? esp when the function passed to the decorator is a > recursive one? The difference is not the decorator but the recursive function call. Consider Case 1: @deco def f(): ...

Re: Decorator help

2013-07-04 Thread Joshua Landau
On 4 July 2013 06:39, Peter Otten <__pete...@web.de> wrote: > Joshua Landau wrote: > >> On 3 July 2013 23:19, Joshua Landau wrote: >>> If you don't want to do that, you'd need to use introspection of a >>> remarkably hacky sort. If you want that, well, it'll take a mo. >> >> After some effort I'm

RE: Decorator help

2013-07-04 Thread Joseph L. Casale
>Well, technically it's > >func.func_closure[0].cell_contents.__name__ > >but of course you cannot know that for the general case. Hah, I admit I lacked perseverance in looking at this in PyCharms debugger as I missed that. Much appreciated! jlc -- http://mail.python.org/mailman/listinfo/python

Re: Decorator help

2013-07-03 Thread Peter Otten
Joshua Landau wrote: > On 3 July 2013 23:19, Joshua Landau wrote: >> If you don't want to do that, you'd need to use introspection of a >> remarkably hacky sort. If you want that, well, it'll take a mo. > > After some effort I'm pretty confident that the hacky way is impossible. Well, technical

RE: Decorator help

2013-07-03 Thread Joseph L. Casale
>> If you don't want to do that, you'd need to use introspection of a >> remarkably hacky sort. If you want that, well, it'll take a mo. > > After some effort I'm pretty confident that the hacky way is impossible. Hah, I fired it in PyCharm's debugger and spent a wack time myself, thanks for the c

Re: Decorator help

2013-07-03 Thread Joshua Landau
On 3 July 2013 23:19, Joshua Landau wrote: > If you don't want to do that, you'd need to use introspection of a > remarkably hacky sort. If you want that, well, it'll take a mo. After some effort I'm pretty confident that the hacky way is impossible. -- http://mail.python.org/mailman/listinfo/py

Re: Decorator help

2013-07-03 Thread Joshua Landau
On 3 July 2013 23:09, Joseph L. Casale wrote: > I have a set of methods which take args that I decorate twice, > > def wrapped(func): > def wrap(*args, **kwargs): > try: > val = func(*args, **kwargs) > # some work > except BaseException as error: >

Re: decorator to fetch arguments from global objects

2013-06-19 Thread Terry Reedy
On 6/19/2013 4:03 AM, Wolfgang Maier wrote: Wolfgang Maier biologie.uni-freiburg.de> writes: andrea crotti gmail.com> writes: 2013/6/18 Terry Reedy udel.edu> Decorators are only worthwhile if used repeatedly. What you specified can easily be written, for instance, as def save_doc(db=No

Re: decorator to fetch arguments from global objects

2013-06-19 Thread Wolfgang Maier
Wolfgang Maier biologie.uni-freiburg.de> writes: > > andrea crotti gmail.com> writes: > > > 2013/6/18 Terry Reedy udel.edu> > > > > Decorators are only worthwhile if used repeatedly. What you specified can > easily be written, for instance, as > > def save_doc(db=None): > >   if db is None:

Re: decorator to fetch arguments from global objects

2013-06-19 Thread Wolfgang Maier
andrea crotti gmail.com> writes: > > 2013/6/18 Terry Reedy udel.edu> > On 6/18/2013 5:47 AM, andrea crotti wrote: > Using a CouchDB server we have a different database object potentially > for every request. > We already set that db in the request object to make it easy to pass it > around for

Re: decorator to fetch arguments from global objects

2013-06-18 Thread Steven D'Aprano
On Tue, 18 Jun 2013 10:47:57 +0100, andrea crotti wrote: > Using a CouchDB server we have a different database object potentially > for every request. > > We already set that db in the request object to make it easy to pass it > around form our django app, however it would be nice if I could set

Re: decorator to fetch arguments from global objects

2013-06-18 Thread andrea crotti
2013/6/18 Terry Reedy > On 6/18/2013 5:47 AM, andrea crotti wrote: > >> Using a CouchDB server we have a different database object potentially >> for every request. >> >> We already set that db in the request object to make it easy to pass it >> around form our django app, however it would be nic

Re: decorator to fetch arguments from global objects

2013-06-18 Thread Terry Reedy
On 6/18/2013 5:47 AM, andrea crotti wrote: Using a CouchDB server we have a different database object potentially for every request. We already set that db in the request object to make it easy to pass it around form our django app, however it would be nice if I could set it once in the API and

Re: decorator to fetch arguments from global objects

2013-06-18 Thread Wolfgang Maier
andrea crotti gmail.com> writes: > > 2013/6/18 Wolfgang Maier biologie.uni-freiburg.de> > > > andrea crotti gmail.com> writes: > > > > > > Using a CouchDB server we have a different database object potentially for > every request. > > > > We already set that db in the request object to make

Re: decorator to fetch arguments from global objects

2013-06-18 Thread andrea crotti
2013/6/18 Wolfgang Maier > andrea crotti gmail.com> writes: > > > > > > > Using a CouchDB server we have a different database object potentially > for > every request. > > > > We already set that db in the request object to make it easy to pass it > around form our django app, however it would b

Re: decorator to fetch arguments from global objects

2013-06-18 Thread Wolfgang Maier
andrea crotti gmail.com> writes: > > > Using a CouchDB server we have a different database object potentially for every request. > > We already set that db in the request object to make it easy to pass it around form our django app, however it would be nice if I could set it once in the API an

Re: decorator to fetch arguments from global objects

2013-06-18 Thread Fábio Santos
On Tue, Jun 18, 2013 at 10:47 AM, andrea crotti wrote: > def with_optional_db(func): > """Decorator that sets the database to the global current one if > not passed in or if passed in and None > """ > @wraps(func) > def _with_optional_db(*args, **kwargs): > func_args = func.func_code.co_varnames >

Re: Decorator help

2013-03-30 Thread 88888 Dihedral
Jason Swails於 2013年3月28日星期四UTC+8上午4時33分08秒寫道: > On Wed, Mar 27, 2013 at 3:49 PM, Joseph L. Casale > wrote: > > I have a class which sets up some class vars, then several methods that are > passed in data > > and do work referencing the class vars. > > > > > > I want to decorate these meth

RE: Decorator help

2013-03-30 Thread Joseph L. Casale
> When you say "class vars", do you mean variables which hold classes? You guessed correctly, and thanks for pointing out the ambiguity in my references. > The one doesn't follow from the other. Writing decorators as classes is  > fairly unusual. Normally, they will be regular functions. I

Re: Decorator help

2013-03-27 Thread Steven D'Aprano
On Wed, 27 Mar 2013 22:38:11 -0400, Jason Swails wrote: >> The second case is the easiest. Suppose you have a class like this, >> with many methods which have code in common. Here's a toy example: >> >> >> def MyClass(object): >> x = "class attribute" >> >> def __init__(self, y): >>

Re: Decorator help

2013-03-27 Thread Jason Swails
On Wed, Mar 27, 2013 at 7:29 PM, Steven D'Aprano < steve+comp.lang.pyt...@pearwood.info> wrote: > > The one doesn't follow from the other. Writing decorators as classes is > fairly unusual. Normally, they will be regular functions. If your > decorator needs to store so much state that it needs to

Re: Decorator help

2013-03-27 Thread Steven D'Aprano
On Wed, 27 Mar 2013 19:49:54 +, Joseph L. Casale wrote: > I have a class which sets up some class vars, then several methods that > are passed in data and do work referencing the class vars. When you say "class vars", do you mean variables which hold classes? Like "string vars" are variable

RE: Decorator help

2013-03-27 Thread Joseph L. Casale
> So decorators will never take instance variables as arguments (nor should >they, since no instance > can possibly exist when they execute). Right, I never thought of it that way, my only use of them has been trivial, in non class scenarios so far. > Bear in mind, a decorator should take a

Re: Decorator help

2013-03-27 Thread Jason Swails
On Wed, Mar 27, 2013 at 3:49 PM, Joseph L. Casale wrote: > I have a class which sets up some class vars, then several methods that > are passed in data > and do work referencing the class vars. > > > I want to decorate these methods, the decorator needs access to the class > vars, so I thought >

Re: Decorator help

2013-03-27 Thread Arnaud Delobelle
On 27 March 2013 19:49, Joseph L. Casale wrote: > I have a class which sets up some class vars, then several methods that are > passed in data > and do work referencing the class vars. > > > I want to decorate these methods, the decorator needs access to the class > vars, so I thought > about ma

Re: Decorator Pattern with Iterator

2012-06-11 Thread Ian Kelly
On Mon, Jun 11, 2012 at 1:51 AM, Tom Harris wrote: > Greetings, > > I have a class that implements the iterator protocol, and tokenises a string > into a series of tokens. As well as the token, it keeps track of some > information such as line number, source file, etc. So each processor needs to

Re: decorator problem

2012-01-09 Thread 88888 Dihedral
Chris Rebert於 2012年1月10日星期二UTC+8下午1時15分53秒寫道: > On Mon, Jan 9, 2012 at 8:14 PM, contro opinion wrote: > > test1.py > > > > def deco(func): > > print 'i am in deco' > > > > @deco > > def test(): > >      print 'i am in test' > > > > > > when you run it ,you get : > > i am in deco > > > > > > >

Re: decorator and wrap

2012-01-09 Thread Chris Rebert
Cheers, Chris -- http://rebertia.com On Mon, Jan 9, 2012 at 8:34 PM, contro opinion wrote: > > def deco(func): > def wrap(): > print 'i am in deco' > return func() > return wrap > > @deco > def test(): > print  "i am in test" > when you run it ,there is no result , >

Re: decorator problem1:

2012-01-09 Thread Chris Rebert
On Mon, Jan 9, 2012 at 7:59 PM, contro opinion wrote: > test1.py > > def deco(func): >    print 'i am in deco' >    return func > > @deco > > def test(): >   print 'i am in test' > > when you run it ,you get : > > > > test2.py > > def tsfunc(func): >     def wrappedFunc(): >     print '[%s] %s

Re: decorator problem

2012-01-09 Thread Chris Rebert
On Mon, Jan 9, 2012 at 8:14 PM, contro opinion wrote: > test1.py > > def deco(func): > print 'i am in deco' > > @deco > def test(): >      print 'i am in test' > > > when you run it ,you get : > i am in deco > > > > test2.py > > def tsfunc(func): > def wrappedFunc(): >   print 'i

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

Re: decorator issue with modules dbus & gobject

2011-08-22 Thread Makiavelik
On 21 août, 18:31, Emile van Sebille wrote: > On 8/18/2011 5:02 AM Makiavelik said... > > > Hi, > > Here is a sample code that reproduces the issue : > > Not really 'sample' enough to allow others to investigate... > > ImportError: No module namedgobject > ImportError: No module nameddbus > Import

Re: decorator issue with modules dbus & gobject

2011-08-21 Thread Emile van Sebille
On 8/18/2011 5:02 AM Makiavelik said... Hi, Here is a sample code that reproduces the issue : Not really 'sample' enough to allow others to investigate... ImportError: No module named gobject ImportError: No module named dbus ImportError: No module named dbus.mainloop.glib Try to eliminate th

Re: Decorator behavior

2011-07-22 Thread Dave Angel
On 01/-10/-28163 02:59 PM, mhearne808[insert-at-sign-here]gmail[insert-dot-here]com wrote: I am just trying to wrap my head around decorators in Python, and I'm confused about some behavior I'm seeing. Run the code below (slightly adapted from a Bruce Eckel article), and I get the following outp

Re: Decorator behavior

2011-07-22 Thread Ian Kelly
On Fri, Jul 22, 2011 at 2:38 PM, mhearne808[insert-at-sign-here]gmail[insert-dot-here]com wrote: > I am just trying to wrap my head around decorators in Python, and I'm > confused about some behavior I'm seeing.  Run the code below (slightly > adapted from a Bruce Eckel article), and I get the fol

Re: Decorator Syntax

2011-03-22 Thread Rafe Kettler
On Mar 21, 8:59 pm, Mike Patterson wrote: > In my Python class the other day, the professor was going over > decorators and he briefly mentioned that there had been this huge > debate about the syntax and using the @ sign to signify decorators. > > I read about the alternative forms proposed here

Re: Decorator Syntax

2011-03-22 Thread Laurent Claessens
And I'm willing to bet that there are plenty of scripts out there that use "dec" as a name for Decimal objects. You won. I owe you a beer ;) Laurent -- http://mail.python.org/mailman/listinfo/python-list

Re: Decorator Syntax

2011-03-21 Thread Ian Kelly
On Mon, Mar 21, 2011 at 7:31 PM, Benjamin Kaplan wrote: > On Mon, Mar 21, 2011 at 8:59 PM, Mike Patterson > wrote: >> In my Python class the other day, the professor was going over >> decorators and he briefly mentioned that there had been this huge >> debate about the syntax and using the @ sign

Re: Decorator Syntax

2011-03-21 Thread Benjamin Kaplan
On Mon, Mar 21, 2011 at 8:59 PM, Mike Patterson wrote: > In my Python class the other day, the professor was going over > decorators and he briefly mentioned that there had been this huge > debate about the syntax and using the @ sign to signify decorators. > > I read about the alternative forms p

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

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

Re: Decorator to inject function into __call__ of a class

2010-03-13 Thread Steven D'Aprano
On Sat, 13 Mar 2010 11:02:44 -0800, Jon Clements wrote: > If I can re-explain slightly, say I have a class 'compute': > > class Compute(object): > def __init__(self, something): > self.something = something > # misc other methods here. > > then... > > class ComputeAdd(Comput

Re: Decorator to inject function into __call__ of a class

2010-03-13 Thread Jon Clements
On 13 Mar, 17:42, Jack Diederich wrote: > On Sat, Mar 13, 2010 at 12:10 PM, Jon Clements wrote: > > On 13 Mar, 16:42, Jack Diederich wrote: > >> On Sat, Mar 13, 2010 at 11:19 AM, Jon Clements > >> wrote: > >> > This is semi-experimental and I'd appreciate opinions of whether it's > >> > the co

Re: Decorator to inject function into __call__ of a class

2010-03-13 Thread Stephen Hansen
On Sat, Mar 13, 2010 at 8:19 AM, Jon Clements wrote: > The name 'some_function' is completely redundant -- don't need it, > don't actually care about the function afterwards, as long as it > becomes a __call__ of a 'B' *instance*. > Special methods are looked up on the class, not the instance, s

Re: Decorator to inject function into __call__ of a class

2010-03-13 Thread Jack Diederich
On Sat, Mar 13, 2010 at 12:10 PM, Jon Clements wrote: > On 13 Mar, 16:42, Jack Diederich wrote: >> On Sat, Mar 13, 2010 at 11:19 AM, Jon Clements wrote: >> > This is semi-experimental and I'd appreciate opinions of whether it's >> > the correct design approach or not. It seems like a good idea,

Re: Decorator to inject function into __call__ of a class

2010-03-13 Thread Jon Clements
On 13 Mar, 16:42, Jack Diederich wrote: > On Sat, Mar 13, 2010 at 11:19 AM, Jon Clements wrote: > > This is semi-experimental and I'd appreciate opinions of whether it's > > the correct design approach or not. It seems like a good idea, but it > > doesn't mean it is. > > > I have a class 'A', thi

Re: Decorator to inject function into __call__ of a class

2010-03-13 Thread Patrick Maupin
On Mar 13, 10:38 am, Jon Clements wrote: > On 13 Mar, 16:26, Patrick Maupin wrote: > > > > > On Mar 13, 10:19 am, Jon Clements wrote: > > > > What I'd like to achieve is something similar to: > > > > @inject(B): > > >  def some_function(a, b): > > >      pass # something useful > > > So, just ty

Re: Decorator to inject function into __call__ of a class

2010-03-13 Thread Jack Diederich
On Sat, Mar 13, 2010 at 11:19 AM, Jon Clements wrote: > This is semi-experimental and I'd appreciate opinions of whether it's > the correct design approach or not. It seems like a good idea, but it > doesn't mean it is. > > I have a class 'A', this provides standard support functions and > excepti

Re: Decorator to inject function into __call__ of a class

2010-03-13 Thread Jon Clements
On 13 Mar, 16:26, Patrick Maupin wrote: > On Mar 13, 10:19 am, Jon Clements wrote: > > > What I'd like to achieve is something similar to: > > > @inject(B): > >  def some_function(a, b): > >      pass # something useful > > So, just typing at the keyboard here, you mean something like: > > class

Re: Decorator to inject function into __call__ of a class

2010-03-13 Thread Patrick Maupin
On Mar 13, 10:19 am, Jon Clements wrote: > What I'd like to achieve is something similar to: > > @inject(B): >  def some_function(a, b): >      pass # something useful So, just typing at the keyboard here, you mean something like: class InjectClass(object): def __init__(self, func, *args, *

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

Re: decorator module in stdlib?

2009-04-06 Thread Michele Simionato
On Apr 7, 7:57 am, Daniel Fetchinson wrote: > > have always seen the decorator module > > as a temporary hack waiting for a proper solution > > at the language level. I wanted the possibility to modify the > > signature of a function. Everybody more or less agreed > > that this was a good idea on

Re: decorator module in stdlib?

2009-04-06 Thread Daniel Fetchinson
>> Similar functionality is already provided by >> functools.update_wrapper() and functools.wraps(). >> Seehttp://docs.python.org/library/functools.html >> You might consider proposing the modification of these functions instead. > > Unfortunately functools.update_wrapper() and functools.wraps() >

Re: decorator module in stdlib?

2009-04-06 Thread Michele Simionato
On Apr 7, 2:50 am, Chris Rebert wrote: > Similar functionality is already provided by > functools.update_wrapper() and functools.wraps(). > Seehttp://docs.python.org/library/functools.html > You might consider proposing the modification of these functions instead. Unfortunately functools.update_w

Re: decorator module in stdlib?

2009-04-06 Thread Chris Rebert
On Mon, Apr 6, 2009 at 5:41 PM, Daniel Fetchinson wrote: > The decorator module [1] written by Michele Simionato is a very useful > tool for maintaining function signatures while applying a decorator. > Many different projects implement their own versions of the same > functionality, for example t

Re: Decorator for validation - inefficient?

2008-11-03 Thread Bryan
Steven D'Aprano wrote: > On Sun, 02 Nov 2008 09:33:41 -0800, Bryan wrote: > > > I'm coming from a .Net background, and yes, one of the reasons I did not > > consider raising exceptions was to avoid the overhead of an exception > > handler clause, which in .Net land is expensive. > > Actually catchi

Re: Decorator for validation - inefficient?

2008-11-02 Thread Steven D'Aprano
On Sun, 02 Nov 2008 09:33:41 -0800, Bryan wrote: > I'm coming from a .Net background, and yes, one of the reasons I did not > consider raising exceptions was to avoid the overhead of an exception > handler clause, which in .Net land is expensive. Actually catching an exception in Python is expens

Re: Decorator for validation - inefficient?

2008-11-02 Thread Arnaud Delobelle
Bryan <[EMAIL PROTECTED]> writes: > However, hoping to make client code cleaner and to avoid setter > functions doing expensive db lookup validations, I do not validate > during the setter, but instead defer it until the client explicitly > asks for the validity of the business object. So the ess

Re: Decorator for validation - inefficient?

2008-11-02 Thread Bryan
On Nov 1, 6:57 pm, Steven D'Aprano <[EMAIL PROTECTED] cybersource.com.au> wrote: > On Sat, 01 Nov 2008 17:12:33 -0700, Bryan wrote: > > The list of validation error descriptions is returned instead of raising > > exceptions so clients can show the errors to the user for fixing. > > Raising exceptio

Re: Decorator for validation - inefficient?

2008-11-01 Thread Steven D'Aprano
On Sat, 01 Nov 2008 17:12:33 -0700, Bryan wrote: > The list of validation error descriptions is returned instead of raising > exceptions so clients can show the errors to the user for fixing. > Raising exceptions seems like an uneeded burden for the client, as there > is nothing exceptional about

Re: Decorator for validation - inefficient?

2008-11-01 Thread Bryan
Steven D'Aprano wrote: > On Fri, 31 Oct 2008 11:26:19 -0700, Bryan wrote: > > > I want my business objects to be able to do this: > [snip code] > > The code you show is quite long and complex and frankly I don't have the > time to study it in detail at the moment, but I can make a couple of > qui

Re: Decorator for validation - inefficient?

2008-10-31 Thread Steven D'Aprano
On Fri, 31 Oct 2008 11:26:19 -0700, Bryan wrote: > I want my business objects to be able to do this: [snip code] The code you show is quite long and complex and frankly I don't have the time to study it in detail at the moment, but I can make a couple of quick comments. I see by your subject li

Re: Decorator for validation - inefficient?

2008-10-31 Thread Arnaud Delobelle
On Oct 31, 6:26 pm, Bryan <[EMAIL PROTECTED]> wrote: > I want my business objects to be able to do this: > class Person(base): > >     def __init__(self): >         self.name = None > >     @base.validator >     def validate_name(self): >         if not self.name: return ['Name cannot be empty'] >

Re: decorator and API

2008-09-18 Thread Gerard flanagan
Lee Harr wrote: I have a class with certain methods from which I want to select one at random, with weighting. The way I have done it is this import random def weight(value): def set_weight(method): method.weight = value return method return set_weight class A(o

Re: decorator and API

2008-09-18 Thread Peter Otten
Steven D'Aprano wrote: I agree with you that the simple explicit approach is better. Now, to answer the question the OP didn't ask: > def choose_with_weighting(actions, weights=None): >     if weights is None: >         weights = [1]*len(actions)  # equal weights >     # Taken virtually unchanged

Re: decorator and API

2008-09-17 Thread George Sakkis
On Sep 17, 5:56 pm, Lee Harr <[EMAIL PROTECTED]> wrote: > I have a class with certain methods from which I want to select > one at random, with weighting. > > The way I have done it is this > > import random > > def weight(value): > def set_weight(method): > method.weight = value

Re: decorator and API

2008-09-17 Thread Steven D'Aprano
On Thu, 18 Sep 2008 02:26:29 +0430, Lee Harr wrote: > I have a class with certain methods from which I want to select one at > random, with weighting. > > The way I have done it is this [snip] You are coupling the weights, the actions, and the object which chooses an action all in the on

Re: decorator and API

2008-09-17 Thread Aaron "Castironpi" Brady
On Sep 17, 6:09 pm, "Aaron \"Castironpi\" Brady" <[EMAIL PROTECTED]> wrote: > On Sep 17, 4:56 pm, Lee Harr <[EMAIL PROTECTED]> wrote: > > > > > I have a class with certain methods from which I want to select > > one at random, with weighting. (snip) > > > The problem I have now is that if I subclas

Re: decorator and API

2008-09-17 Thread Aaron "Castironpi" Brady
On Sep 17, 4:56 pm, Lee Harr <[EMAIL PROTECTED]> wrote: > I have a class with certain methods from which I want to select > one at random, with weighting. > > The way I have done it is this > > import random > > def weight(value): >     def set_weight(method): >         method.weight = value >

Re: decorator to prevent adding attributes to class?

2008-07-12 Thread rbossy
>> class Foo(Freezeable): >> def __init__(self): >> self.bar = 42 >> self.freeze() # ok, we set all variables, no more from here >> >> >> x = Foo() >> print x.bar >> x.bar = -42 >> print x.bar >> x.baz = "OMG! A typo!" >> > >Pretty nice, but unfortunately the subclass has to remember to call freeze

Re: decorator to prevent adding attributes to class?

2008-07-11 Thread Michele Simionato
On Jul 11, 9:24 pm, Neal Becker <[EMAIL PROTECTED]> wrote: > Robert Bossy wrote: > > class Foo(Freezeable): > > def __init__(self): > > self.bar = 42 > > self.freeze() # ok, we set all variables, no more from here > > > x = Foo() > > print x.bar > > x.bar = -42 > > print x.bar > > x.baz = "OMG! A t

Re: decorator to prevent adding attributes to class?

2008-07-11 Thread Neal Becker
Robert Bossy wrote: > class Foo(Freezeable): > def __init__(self): > self.bar = 42 > self.freeze() # ok, we set all variables, no more from here > > > x = Foo() > print x.bar > x.bar = -42 > print x.bar > x.baz = "OMG! A typo!" > Pretty nice, but unfortunately the subclass has to remember to c

Re: decorator to prevent adding attributes to class?

2008-07-11 Thread Michele Simionato
On Jul 11, 6:38 pm, Robert Bossy > I don't get it. Why use a metaclass? Wouldn't the following be the same, > but easier to grasp: > > class Frozen(object): >     def __setattr__(self, name, value): >        if not hasattr(self, name): >           raise AttributeError, "cannot add attributes to %s"

Re: decorator to prevent adding attributes to class?

2008-07-11 Thread Robert Bossy
Michele Simionato wrote: This article could give you same idea (it is doing the opposite, warning you if an attribute is overridden): http://stacktrace.it/articoli/2008/06/i-pericoli-della-programmazione-con-i-mixin1/ There is also a recipe that does exactly what you want by means of a metaclass

Re: decorator to prevent adding attributes to class?

2008-07-11 Thread Michele Simionato
On Jul 11, 5:29 pm, Neal Becker <[EMAIL PROTECTED]> wrote: > After spending the morning debugging where I had misspelled the name of an > attribute (thus adding a new attr instead of updating an existing one), I > would like a way to decorate a class so that attributes cannot be (easily) > added. >

Re: Decorator metaclass

2008-05-24 Thread Gabriel Genellina
En Fri, 23 May 2008 16:25:19 -0300, Thomas Karolski <[EMAIL PROTECTED]> escribió: > Turns out the first msg I sent did not reach the list, so I'll just post > what I've achieved by now: [snip a couple of long metaclasses] > Now the reason why I'm using decorators, is because I want to be ably t

Re: Decorator metaclass

2008-05-23 Thread Thomas Karolski
Turns out the first msg I sent did not reach the list, so I'll just post what I've achieved by now: -- class DecoratorDummy(object): pass class InheritedDecoratorType(type): def __new__(cls, name, bases, dct): # return if its a clas

Re: Decorator metaclass

2008-05-23 Thread Carl Banks
On May 23, 11:42 am, Thomas Karolski <[EMAIL PROTECTED]> wrote: > > You will note that Decorator does not define __init__. In fact, > > object.__init__ will be called, which does nothing. If you think that > > all classes with DecoratorType as their metaclass will be a direct > > subclass of Dec

Re: Decorator metaclass

2008-05-23 Thread Thomas Karolski
Thanks for pointing out all those mistakes. I think I'm already starting to grasp all of the python magic going on in there. Parenthetical: I don't normally recommend this style, since it obscures the fact that you're using a custom metaclass to the user. That is something the user probably wou

Re: Decorator metaclass

2008-05-23 Thread Carl Banks
On May 22, 10:28 pm, [EMAIL PROTECTED] wrote: > Hi, > I would like to create a Decorator metaclass, which automatically > turns a class which inherits from the "Decorator" type into a > decorator. > A decorator in this case, is simply a class which has all of its > decorator implementation inside a

Re: Decorator metaclass

2008-05-22 Thread Maric Michaud
Le Friday 23 May 2008 04:28:22 [EMAIL PROTECTED], vous avez écrit : > Hi, > I would like to create a Decorator metaclass, which automatically > turns a class which inherits from the "Decorator" type into a > decorator. > A decorator in this case, is simply a class which has all of its > decorator

  1   2   3   >