Re: Trouble with defaults and timeout decorator

2023-06-24 Thread Piergiorgio Sartor via Python-list
On 24/06/2023 18.18, Jason Friedman wrote: I'm writing a database connectivity module to be used by other modules and leveraging the jaydebeapi module. From what I can tell jaydebeapi contains no built-in timeout capability, so then I turned to https://pypi.org/project/timeout-decorator

Re: Trouble with defaults and timeout decorator

2023-06-24 Thread MRAB via Python-list
meout-decorator/. My goal is to have a default timeout of, say, 10 seconds, which can be overridden by the caller. import jaydebeapi from timeout_decorator import timeout class Database: database_connection = None database_name, user_name, password, host, port = stuff timeout =

Trouble with defaults and timeout decorator

2023-06-24 Thread Jason Friedman via Python-list
I'm writing a database connectivity module to be used by other modules and leveraging the jaydebeapi module. >From what I can tell jaydebeapi contains no built-in timeout capability, so then I turned to https://pypi.org/project/timeout-decorator/. My goal is to have a default timeout of,

Re: Implementation of an lru_cache() decorator that ignores the first argument

2022-09-30 Thread Heinrich Kruger
On Thursday, September 29th, 2022 at 07:18, Robert Latest via Python-list wrote: > Hi Chris and dh, > > thanks for your --as usually-- thoughtful and interesting answers. Indeed, > when > doing these web applications I find that there are several layers of useful, > maybe less useful, and unk

Re: Implementation of an lru_cache() decorator that ignores the first argument

2022-09-29 Thread Robert Latest via Python-list
Hi Chris and dh, thanks for your --as usually-- thoughtful and interesting answers. Indeed, when doing these web applications I find that there are several layers of useful, maybe less useful, and unknown caching. Many of my requests rely on a notoriously unreliable read-only database outside of m

Re: Implementation of an lru_cache() decorator that ignores the first argument

2022-09-28 Thread dn
On 29/09/2022 07.22, Robert Latest via Python-list wrote: ... > This is what I came up with. I'm quite happy with it so far. Question: Am I > being too clever? is it too complicated? Am I overlooking something that will > come back and bite me later? Thanks for any comments! Thank you for the ch

Re: Implementation of an lru_cache() decorator that ignores the first argument

2022-09-28 Thread Chris Angelico
that will > come back and bite me later? Thanks for any comments! > > def lru_ignore_first(timeout=0, **lru_args): > ... I think this code is fairly specific to what you're doing, which means the decorator won't be as reusable (first hint of that is the entire "timeout&qu

Implementation of an lru_cache() decorator that ignores the first argument

2022-09-28 Thread Robert Latest via Python-list
forever (unless it's kicked out of the cache) and not reflect the (infrequent) changes on the database. So what I need is some decorator that can be used like this: @lru_ignore_first(timeout=10) def query_db(db, args): # do the "expensive" query return resul

Re: name for new Enum decorator

2021-05-29 Thread hw
On 5/28/21 5:23 AM, Ethan Furman wrote: Greetings! The Flag type in the enum module has had some improvements, but I find it necessary to move one of those improvements into a decorator instead, and I'm having a hard time thinking up a name. What is the behavior?  Well, a name in a

Re: name for new Enum decorator

2021-05-28 Thread MRAB
On 2021-05-28 04:23, Ethan Furman wrote: Greetings! The Flag type in the enum module has had some improvements, but I find it necessary to move one of those improvements into a decorator instead, and I'm having a hard time thinking up a name. [snip] So, like the enum.unique decorator

Re: name for new Enum decorator

2021-05-27 Thread 2QdxY4RzWzUUiLuE
On 2021-05-27 at 20:23:29 -0700, Regarding "name for new Enum decorator," Ethan Furman wrote: > Greetings! > > The Flag type in the enum module has had some improvements, but I find it > necessary to move one of those improvements into a decorator instead, and > I'

name for new Enum decorator

2021-05-27 Thread Ethan Furman
Greetings! The Flag type in the enum module has had some improvements, but I find it necessary to move one of those improvements into a decorator instead, and I'm having a hard time thinking up a name. What is the behavior? Well, a name in a flag type can be either canonical (it repre

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

Decorator with parameters

2020-04-06 Thread ast
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 the two variants ? from collection import OrderedDict

Re: Better use a class decorator or a metaclass?(was: super not behaving as I expected)

2020-04-04 Thread Souvik Dutta
> slots = C.__slots__ > > except AttributeError: > > assert C is object > > else: > > all_slots.update(slots) > > return all_slots > > > > I have been thinking about

Better use a class decorator or a metaclass?(was: super not behaving as I expected)

2020-04-04 Thread Antoon Pardon
try: > slots = C.__slots__ > except AttributeError: > assert C is object > else: > all_slots.update(slots) > return all_slots > I have been thinking about this. AFAIU the slots are static data. So

Decorator as a class and Descriptor __get__ working? - cookbook, 9.9, pg 349

2019-12-08 Thread Veek M
instance, instance, value) NOT whatever he's done.. ? How is he passing Spam to 'grok'? It should be (self=decorator-instance, Spam- instance-s and value) being passed to __get__ (for those who don't have the book, he's switched to 'grok' instead of 'b

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" al

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

2019-10-10 Thread Antoon Pardon
tion-local "log" or anything.) I have been up to now, but I will take this under consideration when thinking about how to proceed. >> And from then on I just use log, to do the logging of that module. >> >> But now I would like to write a decorator trace, so that a decorated >&g

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

2019-10-09 Thread Peter Otten
__name__, result)) return result return trace [useit.py] from logutil import Logger log = Logger(__name__) @log.traced def some_func(...): ... But yes, Chris' suggestion is more straight-forward ;) > And from then on I just use log, to do the logging of that module.

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

2019-10-09 Thread Chris Angelico
it as a module-level variable and always called "log", and you never use the name "log" for anything else? (Namespaced usage like "math.log" is fine, but you never have a function-local "log" or anything.) > And from then on I just use log, to do the loggi

decorator needs access to variables where it is used.

2019-10-09 Thread Antoon Pardon
I have some logging utilities so that when I write library code, I just use the following. from logutil import Logger log = Logger(__name__) And from then on I just use log, to do the logging of that module. But now I would like to write a decorator trace, so that a decorated function would

Re: How do I give a decorator acces to the class of a decorated function

2019-09-05 Thread Chris Angelico
On Fri, Sep 6, 2019 at 4:33 AM Serhiy Storchaka wrote: > > 04.09.19 17:21, Antoon Pardon пише: > > What I am trying to do is the following. > > > > class MyClass (...) : > > @register > > def MyFunction(...) > > ... > > > &

Re: How do I give a decorator acces to the class of a decorated function

2019-09-05 Thread Serhiy Storchaka
04.09.19 17:21, Antoon Pardon пише: What I am trying to do is the following. class MyClass (...) : @register def MyFunction(...) ... What I would want is for the register decorator to somehow create/mutate class variable(s) of MyClass. Is that possible or do I have to

Re: How do I give a decorator acces to the class of a decorated function

2019-09-05 Thread Peter Otten
Antoon Pardon wrote: > On 5/09/19 15:30, Peter Otten wrote: >> Can you provide some context? > > Sure I am researching the possibility of writing an easy to use > lexing/parsing tool. The idea is to write your lexer/parser as > follows: > > class Calculator(metaclass = ...): > def __init__(

Re: How do I give a decorator acces to the class of a decorated function

2019-09-05 Thread Antoon Pardon
On 5/09/19 15:30, Peter Otten wrote: >> 2) Is it possible to make MyClass automatically a subclass of an other >> class >>through the metaclass? >> > While you can modify `bases` before passing it on to `type` this starts to > get a bit messy. Maybe you need a real metaclass which unlike the r

Re: How do I give a decorator acces to the class of a decorated function

2019-09-05 Thread Peter Otten
Antoon Pardon wrote: > On 4/09/19 17:46, Peter Otten wrote: >> Antoon Pardon wrote: >> >>> What I am trying to do is the following. >>> >>> class MyClass (...) : >>> @register >>> def MyFunction(...) >>> ... >

Re: How do I give a decorator acces to the class of a decorated function

2019-09-05 Thread Antoon Pardon
On 4/09/19 17:46, Peter Otten wrote: > Antoon Pardon wrote: > >> What I am trying to do is the following. >> >> class MyClass (...) : >> @register >> def MyFunction(...) >> ... >> >> What I would want is for the register deco

Re: How do I give a decorator acces to the class of a decorated function

2019-09-05 Thread Antoon Pardon
On 4/09/19 17:46, Peter Otten wrote: > Antoon Pardon wrote: > >> What I am trying to do is the following. >> >> class MyClass (...) : >> @register >> def MyFunction(...) >> ... >> >> What I would want is for the register deco

Re: How do I give a decorator acces to the class of a decorated function

2019-09-04 Thread dieter
Antoon Pardon writes: > What I am trying to do is the following. > > class MyClass (...) : > @register > def MyFunction(...) > ... > > What I would want is for the register decorator to somehow create/mutate > class variable(s) of MyClass. > >

Re: How do I give a decorator acces to the class of a decorated function

2019-09-04 Thread Peter Otten
Antoon Pardon wrote: > What I am trying to do is the following. > > class MyClass (...) : > @register > def MyFunction(...) > ... > > What I would want is for the register decorator to somehow create/mutate > class variable(s) of MyClass. > >

Re: How do I give a decorator acces to the class of a decorated function

2019-09-04 Thread Chris Angelico
On Thu, Sep 5, 2019 at 12:23 AM Antoon Pardon wrote: > > What I am trying to do is the following. > > class MyClass (...) : > @register > def MyFunction(...) > ... > > What I would want is for the register decorator to somehow create/mutate > class

Re: How do I give a decorator acces to the class of a decorated function

2019-09-04 Thread Rhodri James
On 04/09/2019 15:21, Antoon Pardon wrote: What I am trying to do is the following. class MyClass (...) : @register def MyFunction(...) ... What I would want is for the register decorator to somehow create/mutate class variable(s) of MyClass. Is that possible or do I have to

How do I give a decorator acces to the class of a decorated function

2019-09-04 Thread Antoon Pardon
What I am trying to do is the following. class MyClass (...) : @register def MyFunction(...) ... What I would want is for the register decorator to somehow create/mutate class variable(s) of MyClass. Is that possible or do I have to rethink my approach? -- Antoon Pardon

Re: Question about the @staticmethod decorator

2019-03-18 Thread Terry Reedy
particular function should be a module scope function or a static method. The @staticmethod decorator merely facilitates a particular organisation of the code allowing us to place what could otherwise be free functions within classes. I didn’t get quiet well this block of text. My first question

Re: Question about the @staticmethod decorator

2019-03-17 Thread Cameron Simpson
— so you may want to consider carefully whether a particular function should be a module scope function or a static method. The @staticmethod decorator merely facilitates a particular organisation of the code allowing us to place what could otherwise be free functions within classes. I didn’t get

Re: Question about the @staticmethod decorator

2019-03-17 Thread Paul Moore
y want to consider carefully whether a particular > function should be a module scope function or a static method. The > @staticmethod decorator merely facilitates a particular organisation of the > code allowing us to place what could otherwise be free functions within > classes. &g

Question about the @staticmethod decorator

2019-03-17 Thread Arup Rakshit
scope function or a static method. The @staticmethod decorator merely facilitates a particular organisation of the code allowing us to place what could otherwise be free functions within classes. I didn’t get quiet well this block of text. My first question is how would I make a module level

Re: transform a "normal" decorator in one for async functions

2018-09-19 Thread vito . detullio
Il giorno martedì 18 settembre 2018 17:36:16 UTC+2, vito.d...@gmail.com ha scritto: > > > is there a way to "convert" a "normal" decorator in one that can handle > > > async functions? > > In general? No. > Ouch. > > I'm new to the

Re: transform a "normal" decorator in one for async functions

2018-09-18 Thread vito . detullio
Il giorno martedì 18 settembre 2018 17:15:22 UTC+2, Thomas Jollans ha scritto: > > but I cannot rewrite this library. > > Perhaps not, but surely you can write your own decorator that does > whatever this library's decorator does for async functions? Presumably > you

Re: transform a "normal" decorator in one for async functions

2018-09-18 Thread Thomas Jollans
On 2018-09-18 16:20, vito.detul...@gmail.com wrote: > but I cannot rewrite this library. Perhaps not, but surely you can write your own decorator that does whatever this library's decorator does for async functions? Presumably you have the code... > is there a way to "co

transform a "normal" decorator in one for async functions

2018-09-18 Thread vito . detullio
Hi. Let's say I have this library that expose a decorator; it's meant to be used with normal functions. but I have to "apply" the decorator to an async function. >From what I understand I cannot "really" wait for this decorated async >function, as it'

Generic OAuth2 authorization inside view-based decorator/middleware?

2018-07-13 Thread Etienne Robillard
, secret=self.settings.SECRET_KEY, login_path=self.settings.OAUTH2_LOGIN_URL) return response(env, start_response) Decorator: from functools import wraps from notmm.controllers.oauth import GoogleController from notmm.utils.django_settings import LazySettings _settings = Laz

Re: property decorator?

2017-12-21 Thread Irv Kalb
Thanks to Rob, Cameron, Ian, Chris and Kirill for the detailed explanations. Very helpful, Irv > On Dec 20, 2017, at 3:56 PM, Irv Kalb wrote: > > I am trying to work through the concept of the @property decorator with > respect to object oriented programming. > >

Re: property decorator?

2017-12-21 Thread Cholo Lennon
On 20/12/17 21:39, Stefan Ram wrote: Irv Kalb writes: about property decorators. The code @property def salary(self): pass @salary.setter def salary(self, newSalary): pass is an abbreviation for def salary_get(self): pass salary = property( salary_get ) def salary_set(self, newSalary

Fwd: property decorator?

2017-12-21 Thread Kirill Balunov
2017-12-21 2:56 GMT+03:00 Irv Kalb : > My questions about this are really historical. From my reading, it looks > like using an @property decorator is a reference to an older approach using > a built in "property" function. But here goes: > > 1) Why were these decorator

Re: property decorator?

2017-12-20 Thread Cameron Simpson
On 20Dec2017 15:56, Irv Kalb wrote: I am trying to work through the concept of the @property decorator with respect to object oriented programming. I believe that I understand how the @property and @.setter work - and that they are used to turn what looks like direct access to instance

Re: property decorator?

2017-12-20 Thread Chris Angelico
On Thu, Dec 21, 2017 at 2:38 PM, Ian Kelly wrote: > On Wed, Dec 20, 2017 at 5:56 PM, Irv Kalb wrote: >> 2) Alternatively, if the getter was going to use the @property decorator, >> then it would seem complimentary to have the decorator name for the >> associated setter

Re: property decorator?

2017-12-20 Thread Ian Kelly
On Wed, Dec 20, 2017 at 5:56 PM, Irv Kalb wrote: > My questions about this are really historical. From my reading, it looks > like using an @property decorator is a reference to an older approach using a > built in "property" function. But here goes: > > 1) Why w

Re: property decorator?

2017-12-20 Thread Rob Gaddi
On 12/20/2017 03:56 PM, Irv Kalb wrote: I am trying to work through the concept of the @property decorator with respect to object oriented programming. I believe that I understand how the @property and @.setter work - and that they are used to turn what looks like direct access to instance

property decorator?

2017-12-20 Thread Irv Kalb
I am trying to work through the concept of the @property decorator with respect to object oriented programming. I believe that I understand how the @property and @.setter work - and that they are used to turn what looks like direct access to instance variables into method calls in an object

Re: Accessing Self Inside Decorator with parameters

2017-12-14 Thread Chris Angelico
On Fri, Dec 15, 2017 at 6:44 AM, Brian Herman wrote: > However when I add the arguments decorator I cannot access the self of the > object that calls the decorator. > > def decorator_with_arguments(arg1, arg2): > def decorator(self, method): > """ Th

Accessing Self Inside Decorator with parameters

2017-12-14 Thread Brian Herman
I would like to access the instance variable of a class using a decorator with arguments: I used to do with with a decorator without arguments: def decorator(method): """ This method is a decorator to get a security token each time you have a service call. ""&quo

exception_guard context manager and decorator

2017-06-25 Thread Steve D'Aprano
As discussed in the Python-Ideas mailing list, sometimes we want to suppress a particular kind of exception and replace it with another. For that reason, I'd like to announce exception_guard, a context manager and decorator which catches specified exceptions and replaces them with a

Re: python decorator

2017-02-22 Thread Irmen de Jong
On 22-2-2017 8:39, Argentinian Black ops lll wrote: > Thanks for the quick response...! you saved me a lot of time, thank you! > I don't know if you want/have to use your own custom caching decorator, but are you aware of the lru_cache decorator that's part of the standard

Re: python decorator

2017-02-22 Thread Ian Kelly
On Wed, Feb 22, 2017 at 8:16 AM, Pavol Lisy wrote: > Maybe this technique could be reusable (and maybe part of functools?) > > With this decorator: > > def wrap_args(decorator): > def decor_out(*args, **kwargs): > def decor_in(func): >

Re: python decorator

2017-02-22 Thread Pavol Lisy
ve Cameron's post contains the bones of a solution. > > Here's my untested solution. > > > def func_cache(cache): > # Create a decorator that uses cache. > def decorate(function): > @functools.wraps(function) > def wrapper(*args):

Re: python decorator

2017-02-22 Thread Steve D'Aprano
my untested solution. def func_cache(cache): # Create a decorator that uses cache. def decorate(function): @functools.wraps(function) def wrapper(*args): try: result = cache[args] except KeyError: result = function(*args

Re: python decorator

2017-02-22 Thread Cecil Westerhof
On Wednesday 22 Feb 2017 08:49 CET, Argentinian Black ops lll wrote: > *** SOLVED *** It would be nice if you shared the solution. -- Cecil Westerhof Senior Software Engineer LinkedIn: http://www.linkedin.com/in/cecilwesterhof -- https://mail.python.org/mailman/listinfo/python-list

Re: python decorator

2017-02-21 Thread Argentinian Black ops lll
*** SOLVED *** -- https://mail.python.org/mailman/listinfo/python-list

Re: python decorator

2017-02-21 Thread Argentinian Black ops lll
Thanks for the quick response...! you saved me a lot of time, thank you! -- https://mail.python.org/mailman/listinfo/python-list

Re: python decorator

2017-02-21 Thread Cameron Simpson
On 21Feb2017 22:44, alfredocabre...@gmail.com wrote: I have a python function and a decorator that works just fine. def fun_cache(function): memo = {} def wrapper(*args): if args in memo: return memo[args] else: rv = function(*args) memo

python decorator

2017-02-21 Thread alfredocabrera4
I have a python function and a decorator that works just fine. def fun_cache(function): memo = {} def wrapper(*args): if args in memo: return memo[args] else: rv = function(*args) memo[args] = rv return rv return wrapper

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
def celsius(self, value):<-- > overwrites previous celsius >self.value = value > >celsius = celsius.setter(celsius) <-- error here > The difference is that the decorator line is evaluated before the function is defined. Try this: class Temperature: def __init__(self):

Decorator

2017-02-08 Thread ast
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 class Temperature: def __init__(self): self.value = 0 # @property def celsius(self): return self.va

Re: Improved nestedmodule decorator implementation

2016-07-05 Thread Steven D'Aprano
On Tue, 5 Jul 2016 07:31 pm, Gregory Ewing wrote: > I wrote: >> The only shortcoming I can think of is that a nestedmodule >> inside another nestedmodule won't be able to see the names >> in the outer nestedmodule > > Actually, that's not correct -- the version I posted before > actually crashes

Improved nestedmodule decorator implementation

2016-07-05 Thread Gregory Ewing
I wrote: The only shortcoming I can think of is that a nestedmodule inside another nestedmodule won't be able to see the names in the outer nestedmodule Actually, that's not correct -- the version I posted before actually crashes if you try to refer to a name in an outer nestedmodule from an in

A nestedmodule decorator (Re: Namespaces are one honking great idea)

2016-07-05 Thread Gregory Ewing
Steven D'Aprano wrote: There's only so far I can go without support from the compiler. It turns out one can go surprisingly far. Here's something I cooked up that seems to meet almost all the requirements. The only shortcoming I can think of is that a nestedmodule inside another nestedmodule wo

Re: Meta decorator with parameters, defined in explicit functions

2016-07-03 Thread Lawrence D’Oliveiro
On Monday, July 4, 2016 at 12:06:21 PM UTC+12, Chris Angelico wrote: > On Mon, Jul 4, 2016 at 9:59 AM, Lawrence D’Oliveiro wrote: >> #begin try_func >> result.__name__ = func.__name__ >> result.__doc__ = func.__doc__ >> return \ >> result >> #end try_func

Re: Meta decorator with parameters, defined in explicit functions

2016-07-03 Thread Ian Kelly
On Sun, Jul 3, 2016 at 6:06 PM, Lawrence D’Oliveiro wrote: > On Monday, July 4, 2016 at 12:02:23 PM UTC+12, Ian wrote: >> I'm talking about the docstring of the *decorator*, not func. > > *Sigh*. Originally somebody was complaining that the lambda version didn’t >

Re: Meta decorator with parameters, defined in explicit functions

2016-07-03 Thread Lawrence D’Oliveiro
On Monday, July 4, 2016 at 12:02:23 PM UTC+12, Ian wrote: > I'm talking about the docstring of the *decorator*, not func. *Sigh*. Originally somebody was complaining that the lambda version didn’t allow for good (or any) docstrings. So I posted my version, and I went to all the e

Re: Meta decorator with parameters, defined in explicit functions

2016-07-03 Thread Chris Angelico
On Mon, Jul 4, 2016 at 9:59 AM, Lawrence D’Oliveiro wrote: > #begin try_func > result.__name__ = func.__name__ > result.__doc__ = func.__doc__ > return \ > result > #end try_func This is the bit we're talking about. You're doing manually what functools.

Re: Meta decorator with parameters, defined in explicit functions

2016-07-03 Thread Ian Kelly
On Sun, Jul 3, 2016 at 5:25 PM, Lawrence D’Oliveiro wrote: > On Monday, July 4, 2016 at 10:39:30 AM UTC+12, Ian wrote: >> Sorry, but you're the one who doesn't seem to get it. Because it's a >> decorator, "your" function is replacing the caller's fun

Re: Meta decorator with parameters, defined in explicit functions

2016-07-03 Thread Lawrence D’Oliveiro
again, the only docstrings I am setting are for the intermediate functions *I* generate. Suppose we have the following: def try_func(func, arg) : "sample function to be turned into a decorator." def result() : "returns func unchanged."

Re: Meta decorator with parameters, defined in explicit functions

2016-07-03 Thread Chris Angelico
On Mon, Jul 4, 2016 at 9:25 AM, Lawrence D’Oliveiro wrote: > On Monday, July 4, 2016 at 10:39:30 AM UTC+12, Ian wrote: >> Sorry, but you're the one who doesn't seem to get it. Because it's a >> decorator, "your" function is replacing the caller's fun

Re: Meta decorator with parameters, defined in explicit functions

2016-07-03 Thread Lawrence D’Oliveiro
On Monday, July 4, 2016 at 10:39:30 AM UTC+12, Ian wrote: > Sorry, but you're the one who doesn't seem to get it. Because it's a > decorator, "your" function is replacing the caller's function in the > caller's namespace. No it is not

Re: Meta decorator with parameters, defined in explicit functions

2016-07-03 Thread Ian Kelly
nction ... > > Which I am not “clobbering” at all. Do you get that yet? My docstrings apply > to *my* functions, not those supplied or generated by the caller. Sorry, but you're the one who doesn't seem to get it. Because it's a decorator, "your" function is replacin

Re: Meta decorator with parameters, defined in explicit functions

2016-07-03 Thread Lawrence D’Oliveiro
On Sunday, July 3, 2016 at 11:53:46 PM UTC+12, Ian wrote: > On Sat, Jul 2, 2016 at 11:37 PM, Lawrence D’Oliveiro wrote: >> That is a function that I am generating, so naturally I want to give it a >> useful docstring. Am I “clobbering” any objects passed in by the caller, >> or returned by the call

Re: Meta decorator with parameters, defined in explicit functions

2016-07-03 Thread Ian Kelly
or_with_args >> ... def my_decorator(func, *args, **kwargs): >> ... """Returns func unmodified.""" >> ... return func >> ... >>>>> my_decorator.__doc__ >> 'generates a decorator which applies my_decorator to the given argumen

Re: Meta decorator with parameters, defined in explicit functions

2016-07-02 Thread Lawrence D’Oliveiro
ed >>> function's name and docstring: >> >> Where am I doing that? > > Using the implementation you posted: > >>>> @decorator_with_args > ... def my_decorator(func, *args, **kwargs): > ... """Returns func unmodified.""

Re: Meta decorator with parameters, defined in explicit functions

2016-07-02 Thread Ian Kelly
implementation you posted: >>> @decorator_with_args ... def my_decorator(func, *args, **kwargs): ... """Returns func unmodified.""" ... return func ... >>> my_decorator.__doc__ 'generates a decorator which applies my_decorator to the given arguments' -- https://mail.python.org/mailman/listinfo/python-list

Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions

2016-07-02 Thread Ben Finney
each function, is a way to address that. Which is my motivation for this thread. > but a simple signature transform for a decorator definitition You're making my case for me: To anyone who doesn't already know exactly what's going on, that is not at all obvious from the cod

Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions

2016-07-02 Thread dieter
uccess) that the "thing" is not "complex" at all but a simple signature transform for a decorator definitition and that the nested function implementation is natural for this purpose. -- https://mail.python.org/mailman/listinfo/python-list

Re: Meta decorator with parameters, defined in explicit functions

2016-07-01 Thread Lawrence D’Oliveiro
On Saturday, July 2, 2016 at 5:10:06 PM UTC+12, Ian wrote: > You should use functools.wraps instead of clobbering the decorated > function's name and docstring: Where am I doing that? > Just to satisfy my own curiosity, do you have something against > putting the return keyword and the returned ex

Re: Meta decorator with parameters, defined in explicit functions

2016-07-01 Thread Ian Kelly
On Fri, Jul 1, 2016 at 11:32 PM, Ben Finney wrote: > Ian Kelly writes: > >> You should use functools.wraps instead of clobbering the decorated >> function's name and docstring: >> >> @functools.wraps(decorator) >> def decorate(*args, **kw

Re: Meta decorator with parameters, defined in explicit functions

2016-07-01 Thread Ben Finney
Ian Kelly writes: > You should use functools.wraps instead of clobbering the decorated > function's name and docstring: > > @functools.wraps(decorator) > def decorate(*args, **kwargs): > ... Thanks. Can you write the full implementation with th

Re: Meta decorator with parameters, defined in explicit functions

2016-07-01 Thread Ian Kelly
On Fri, Jul 1, 2016 at 4:08 PM, Lawrence D’Oliveiro wrote: > On Tuesday, June 28, 2016 at 5:03:08 PM UTC+12, Ben Finney wrote: >> There is a clever one-line decorator that has been copy-pasted without >> explanation in many code bases for many years:: >> >> d

Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions

2016-07-01 Thread Ben Finney
Ben Bacarisse writes: > By replying I'm not accepting the premise -- I have no idea if there > is widespread fear and suspicion of lambdas among Python users but it > seems unlikely. I can testify, as the person who started this thread, that there is no fear or suspicion of lambda here. I use it

Re: Meta decorator with parameters, defined in explicit functions

2016-07-01 Thread Lawrence D’Oliveiro
On Tuesday, June 28, 2016 at 5:03:08 PM UTC+12, Ben Finney wrote: > There is a clever one-line decorator that has been copy-pasted without > explanation in many code bases for many years:: > > decorator_with_args = lambda decorator: lambda *args, **kwargs: lambda > func:

Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions

2016-07-01 Thread Ben Bacarisse
dieter writes: >> Lawrence D’Oliveiro wrote: >>> I don’t know why this fear and suspicion of lambdas is so widespread among >>> Python users ... former Java/C# programmers, perhaps? By replying I'm not accepting the premise -- I have no idea if there is widespread fear and suspicion of lambdas

Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions

2016-07-01 Thread dieter
Peter Otten <__pete...@web.de> writes: > Lawrence D’Oliveiro wrote: > >> On Tuesday, June 28, 2016 at 5:03:08 PM UTC+12, Ben Finney wrote: >> >>> I would like to see a more Pythonic, more explicit and expressive >>> replacement with its component parts easily understood. >> >> I don’t know why t

Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions

2016-06-30 Thread Peter Otten
Lawrence D’Oliveiro wrote: > On Thursday, June 30, 2016 at 7:26:01 PM UTC+12, Peter Otten wrote: >> foo = lambda : >> >> there is syntactic sugar in Python that allows you to write it as >> >> def foo(): >> return >> >> with the nice side effects that it improves the readability of traceb

Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions

2016-06-30 Thread Steven D'Aprano
On Thursday 30 June 2016 17:43, Lawrence D’Oliveiro wrote: > On Thursday, June 30, 2016 at 7:26:01 PM UTC+12, Peter Otten wrote: >> foo = lambda : >> >> there is syntactic sugar in Python that allows you to write it as >> >> def foo(): >> return >> >> with the nice side effects that it im

Re: Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions

2016-06-30 Thread Lawrence D’Oliveiro
On Thursday, June 30, 2016 at 7:26:01 PM UTC+12, Peter Otten wrote: > foo = lambda : > > there is syntactic sugar in Python that allows you to write it as > > def foo(): > return > > with the nice side effects that it improves the readability of tracebacks > and allows you to provide a do

Fear and suspicion of lambdas, was Re: Meta decorator with parameters, defined in explicit functions

2016-06-30 Thread Peter Otten
func=lambda x, y: x + y): ... the alternative def reduce(items, func=add): ... looks more readable in my eyes even though somewhere -- under the rug or in the operator module -- you need def add(x, y): """ >>> add(3, 4) 7 """ return

Re: Meta decorator with parameters, defined in explicit functions

2016-06-29 Thread Steven D'Aprano
On Thu, 30 Jun 2016 12:43 pm, Lawrence D’Oliveiro wrote: > On Tuesday, June 28, 2016 at 5:03:08 PM UTC+12, Ben Finney wrote: >> There is a clever one-line decorator that has been copy-pasted without >> explanation in many code bases for many years:: >> >> d

  1   2   3   4   5   6   7   8   9   10   >