Re: Decorators not worth the effort

2012-09-19 Thread Jean-Michel Pichavant
- Original Message - > Jean-Michel Pichavant writes: > > > - Original Message - > >> Jean-Michel Pichavant wrote: > > [snip] > >> One minor note, the style of decorator you are using loses the > >> docstring > >> (at least) of the original function. I would add the > >> @functoo

Re: Decorators not worth the effort

2012-09-18 Thread 88888 Dihedral
Terry Reedy於 2012年9月15日星期六UTC+8上午4時40分32秒寫道: > 2nd try, hit send button by mistake before > > > > On 9/14/2012 5:28 AM, Jean-Michel Pichavant wrote: > > > > > Decorators are very popular so I kinda already know that the fault is > > > mine. Now to the reason why I have troubles writing them,

Re: Decorators not worth the effort

2012-09-18 Thread Dieter Maurer
Jean-Michel Pichavant writes: > - Original Message - >> Jean-Michel Pichavant wrote: > [snip] >> One minor note, the style of decorator you are using loses the >> docstring >> (at least) of the original function. I would add the >> @functools.wraps(func) >> decorator inside your decorator

Re: Decorators not worth the effort

2012-09-18 Thread 88888 Dihedral
Chris Angelico於 2012年9月18日星期二UTC+8下午9時25分04秒寫道: > On Tue, Sep 18, 2012 at 11:19 PM, Neil Cerutti wrote: > > > On 2012-09-14, Chris Angelico wrote: > > >> But then again, who actually ever needs fibonacci numbers? > > > > > > If it should happen that your question is not facetious: > > > > >

Re: Decorators not worth the effort

2012-09-18 Thread Chris Angelico
On Tue, Sep 18, 2012 at 11:19 PM, Neil Cerutti wrote: > On 2012-09-14, Chris Angelico wrote: >> But then again, who actually ever needs fibonacci numbers? > > If it should happen that your question is not facetious: > > http://en.wikipedia.org/wiki/Fibonacci_number#Applications It wasn't entirel

Re: Decorators not worth the effort

2012-09-18 Thread Neil Cerutti
On 2012-09-14, Chris Angelico wrote: > But then again, who actually ever needs fibonacci numbers? If it should happen that your question is not facetious: http://en.wikipedia.org/wiki/Fibonacci_number#Applications -- Neil Cerutti -- http://mail.python.org/mailman/listinfo/python-list

Re: Decorators not worth the effort

2012-09-18 Thread Thomas Rachel
Am 15.09.2012 16:18 schrieb 8 Dihedral: The concept of decorators is just a mapping from a function ... or class ... > to another function ... or any other object ... > with the same name in python. Thomas -- http://mail.python.org/mailman/listinfo/python-list

Re: Decorators not worth the effort

2012-09-17 Thread Jean-Michel Pichavant
- Original Message - > Jean-Michel Pichavant wrote: [snip] > One minor note, the style of decorator you are using loses the > docstring > (at least) of the original function. I would add the > @functools.wraps(func) > decorator inside your decorator. Is there a way to not loose the funct

Re: Decorators not worth the effort

2012-09-16 Thread alex23
On Sep 15, 6:30 am, Terry Reedy wrote: > > On 13Sep2012 18:58, alex23 wrote: > > | On Sep 14, 3:54 am, Jean-Michel Pichavant > > | wrote: > > | > I don't like decorators, I think they're not worth the mental effort. > > | > > | Because passing a function to a function is a huge cognitive burden?

Re: Decorators not worth the effort

2012-09-15 Thread 88888 Dihedral
David Hutto於 2012年9月15日星期六UTC+8下午6時04分28秒寫道: > On Sat, Sep 15, 2012 at 5:45 AM, 8 Dihedral > > wrote: > > > Steven D'Aprano於 2012年9月15日星期六UTC+8上午7時39分28秒寫道: > > >> On Fri, 14 Sep 2012 15:16:47 -0600, Ian Kelly wrote: > > >> > > >> > > >> > > >> > If only there were a conceptually simple

Re: Decorators not worth the effort

2012-09-15 Thread Dwight Hutto
On Sat, Sep 15, 2012 at 5:45 AM, 8 Dihedral wrote: > Steven D'Aprano於 2012年9月15日星期六UTC+8上午7時39分28秒寫道: >> On Fri, 14 Sep 2012 15:16:47 -0600, Ian Kelly wrote: >> >> >> >> > If only there were a conceptually simpler way to do this. Actually, >> >> > there is. I give you: muman than humanetadec

Re: Decorators not worth the effort

2012-09-15 Thread 88888 Dihedral
Steven D'Aprano於 2012年9月15日星期六UTC+8上午7時39分28秒寫道: > On Fri, 14 Sep 2012 15:16:47 -0600, Ian Kelly wrote: > > > > > If only there were a conceptually simpler way to do this. Actually, > > > there is. I give you: metadecorators! > > [code snipped but shown below] > > > Which I think is certain

Re: Decorators not worth the effort

2012-09-15 Thread Thomas Rachel
[Sorry, my Firefox destroyed the indent... Am 14.09.2012 22:29 schrieb Terry Reedy: In other words def make_wrapper(func, param): def wrapper(*args, **kwds): for i in range(param): func(*args, **kwds) return wrapper def f(x): print(x) f = make_wrapper(f, 2) f('simple') # is simpler, at least

Re: Decorators not worth the effort

2012-09-15 Thread Dieter Maurer
Dwight Hutto wrote at 2012-9-14 23:42 -0400: > ... >Reduce redundancy, is argumentative. > >To me, a decorator, is no more than a logging function. Correct me if >I'm wrong. Well, it depends on how you are using decorators and how complex your decorators are. If what you are using as decorating fu

Re: Decorators not worth the effort

2012-09-14 Thread Dwight Hutto
On Fri, Sep 14, 2012 at 2:40 AM, Dieter Maurer wrote: >> On Sep 14, 3:54 am, Jean-Michel Pichavant >> wrote: >>> I don't like decorators, I think they're not worth the mental effort. > > Fine. > > I like them because they can vastly improve reusability and drastically > reduce redundancies (which

Re: Decorators not worth the effort

2012-09-14 Thread Steve Howell
On Sep 14, 6:05 am, Tim Chase wrote: > On 09/14/12 07:01, Steven D'Aprano wrote:> [snip timeout class] > > > Holy over-engineering Batman!!! > > > No wonder you don't think much of decorators, > > [snip] > > > Most of my decorator functions are under a dozen lines. And that's the > > complicated o

Re: Decorators not worth the effort

2012-09-14 Thread Steven D'Aprano
On Fri, 14 Sep 2012 15:16:47 -0600, Ian Kelly wrote: > If only there were a conceptually simpler way to do this. Actually, > there is. I give you: metadecorators! [code snipped but shown below] > Which I think is certainly easier to understand than the nested > functions approach. Maybe for you

RE: Decorators not worth the effort

2012-09-14 Thread Prasad, Ramit
Jean-Michel Pichavant wrote: [snip] > Ultimately, the goal is to have something like > > @timeout(2) > def doAction1 > > @timeout(4) > def doAction2 [snip] > Here's Steven example: > > # Untested! > def timeout(t=15): > # Decorator factory. Return a decorator to actually do the work. >

Re: Decorators not worth the effort

2012-09-14 Thread Joshua Landau
On 14 September 2012 18:30, Chris Angelico wrote: > On Sat, Sep 15, 2012 at 2:15 AM, andrea crotti > wrote: > > The poor algorithm is much more close to the mathematical definition > > than the smarter iterative one.. And in your second version you > > include some ugly caching logic inside it,

Re: Decorators not worth the effort

2012-09-14 Thread Ian Kelly
On Fri, Sep 14, 2012 at 2:29 PM, Terry Reedy wrote: > For a simple, unparameterized wrapper, the difficulty is entirely in the > wrapper maker. It must define the final wrapper as a nested function and > return it*. It is irrelevant whether the wrapper maker is used with pre-def > decorator syntax

Re: Decorators not worth the effort

2012-09-14 Thread Terry Reedy
On 9/14/2012 4:29 PM, Terry Reedy wrote: On 9/13/2012 10:12 PM, Cameron Simpson wrote: On 13Sep2012 18:58, alex23 wrote: | On Sep 14, 3:54 am, Jean-Michel Pichavant | wrote: | > I don't like decorators, I think they're not worth the mental effort. | | Because passing a function to a function i

Re: Decorators not worth the effort

2012-09-14 Thread Terry Reedy
2nd try, hit send button by mistake before On 9/14/2012 5:28 AM, Jean-Michel Pichavant wrote: Decorators are very popular so I kinda already know that the fault is mine. Now to the reason why I have troubles writing them, I don't know. Every time I did use decorators, I spent way too much time

Re: Decorators not worth the effort

2012-09-14 Thread Terry Reedy
On 9/14/2012 5:28 AM, Jean-Michel Pichavant wrote: Decorators are very popular so I kinda already know that the fault is mine. Now to the reason why I have troubles writing them, I don't know. Every time I did use decorators, I spent way too much time writing it (and debugging it). -- Terry

Re: Decorators not worth the effort

2012-09-14 Thread Terry Reedy
On 9/13/2012 10:12 PM, Cameron Simpson wrote: On 13Sep2012 18:58, alex23 wrote: | On Sep 14, 3:54 am, Jean-Michel Pichavant | wrote: | > I don't like decorators, I think they're not worth the mental effort. | | Because passing a function to a function is a huge cognitive burden? For parameter

Re: Decorators not worth the effort

2012-09-14 Thread Chris Angelico
On Sat, Sep 15, 2012 at 2:15 AM, andrea crotti wrote: > The poor algorithm is much more close to the mathematical definition > than the smarter iterative one.. And in your second version you > include some ugly caching logic inside it, so why not using a > decorator then? I learned Fibonacci as

Re: Decorators not worth the effort

2012-09-14 Thread 88888 Dihedral
Chris Angelico於 2012年9月14日星期五UTC+8下午10時41分06秒寫道: > On Sat, Sep 15, 2012 at 12:12 AM, andrea crotti > > wrote: > > > def fib(n): > > > if n <= 1: > > > return 1 > > > return fib(n-1) + fib(n-2) > > > > > > @memoize > > > def fib_memoized(n): > > > if n <= 1: > > >

Re: Decorators not worth the effort

2012-09-14 Thread andrea crotti
2012/9/14 Chris Angelico : > > Trouble is, you're starting with a pretty poor algorithm. It's easy to > improve on what's poor. Memoization can still help, but I would start > with a better algorithm, such as: > > def fib(n): > if n<=1: return 1 > a,b=1,1 > for i in range(1,

Re: Decorators not worth the effort

2012-09-14 Thread Jean-Michel Pichavant
- Original Message - > On Fri, 14 Sep 2012 15:22:26 +0200, Jean-Michel Pichavant wrote: > > > Here's Steven example: > > > > # Untested! > > def timeout(t=15): > > # Decorator factory. Return a decorator to actually do the > > work. if > > FPGA: > > t *= 3 > > def

Re: Decorators not worth the effort

2012-09-14 Thread Steven D'Aprano
On Fri, 14 Sep 2012 15:22:26 +0200, Jean-Michel Pichavant wrote: > Here's Steven example: > > # Untested! > def timeout(t=15): > # Decorator factory. Return a decorator to actually do the work. if > FPGA: > t *= 3 > def decorator(func): > @functools.wraps(func) >

Re: Decorators not worth the effort

2012-09-14 Thread Chris Angelico
On Sat, Sep 15, 2012 at 12:12 AM, andrea crotti wrote: > def fib(n): > if n <= 1: > return 1 > return fib(n-1) + fib(n-2) > > @memoize > def fib_memoized(n): > if n <= 1: > return 1 > return fib_memoized(n-1) + fib_memoized(n-2) > > > The second fibonacci looks exac

Re: Decorators not worth the effort

2012-09-14 Thread andrea crotti
I think one very nice and simple example of how decorators can be used is this: def memoize(f, cache={}, *args, **kwargs): def _memoize(*args, **kwargs): key = (args, str(kwargs)) if not key in cache: cache[key] = f(*args, **kwargs) return cache[key] r

Re: Decorators not worth the effort

2012-09-14 Thread Jean-Michel Pichavant
- Original Message - > Jean-Michel Pichavant wrote: > > > I wrote the following one, used to decorate any function that > > access > > an equipment, it raises an exception when the timeout expires. The > > timeout is adapted to the platform, ASIC of FPGA so people don't > > need > > to

Re: Decorators not worth the effort

2012-09-14 Thread Tim Chase
On 09/14/12 07:01, Steven D'Aprano wrote: > [snip timeout class] > > Holy over-engineering Batman!!! > > No wonder you don't think much of decorators, [snip] > Most of my decorator functions are under a dozen lines. And that's the > complicated ones! As are mine, and a sizable chunk of those

Re: Decorators not worth the effort

2012-09-14 Thread Ulrich Eckhardt
Am 14.09.2012 11:28, schrieb Jean-Michel Pichavant: Decorators are very popular so I kinda already know that the fault is mine. Now to the reason why I have troubles writing them, I don't know. Every time I did use decorators, I spent way too much time writing it (and debugging it). I wrote the

Re: Decorators not worth the effort

2012-09-14 Thread Steven D'Aprano
On Fri, 14 Sep 2012 11:28:22 +0200, Jean-Michel Pichavant wrote: > PS : Here's the decorator, just to give you an idea about how it looks. > Small piece of code, but took me more than 2 hours to write it. I > removed some sensible parts so I don't expect it to run. [snip timeout class] Holy over

Re: Decorators not worth the effort

2012-09-14 Thread Duncan Booth
Jean-Michel Pichavant wrote: > I wrote the following one, used to decorate any function that access > an equipment, it raises an exception when the timeout expires. The > timeout is adapted to the platform, ASIC of FPGA so people don't need > to specify everytime one timeout per platform. > > I

Re: Decorators not worth the effort

2012-09-14 Thread Jean-Michel Pichavant
- Original Message - > On Sep 14, 3:54 am, Jean-Michel Pichavant > wrote: > > I don't like decorators, I think they're not worth the mental > > effort. > > Because passing a function to a function is a huge cognitive burden? > -- > http://mail.python.org/mailman/listinfo/python-list > I

Re: Decorators not worth the effort

2012-09-13 Thread Dieter Maurer
> On Sep 14, 3:54 am, Jean-Michel Pichavant > wrote: >> I don't like decorators, I think they're not worth the mental effort. Fine. I like them because they can vastly improve reusability and drastically reduce redundancies (which I hate). Improved reusability and reduced redundancies can make a

Re: Decorators not worth the effort

2012-09-13 Thread alex23
On Sep 14, 12:12 pm, Cameron Simpson wrote: > On 13Sep2012 18:58, alex23 wrote: > | On Sep 14, 3:54 am, Jean-Michel Pichavant | wrote: > | > I don't like decorators, I think they're not worth the mental effort. > | > | Because passing a function to a function is a huge cognitive burden? > > It is

Re: Decorators not worth the effort

2012-09-13 Thread Cameron Simpson
On 13Sep2012 18:58, alex23 wrote: | On Sep 14, 3:54 am, Jean-Michel Pichavant | wrote: | > I don't like decorators, I think they're not worth the mental effort. | | Because passing a function to a function is a huge cognitive burden? It is for me when I'm _writing_ the decorator:-) But if I get