tag:blogger.com,1999:blog-2363643920942057324.post4409669009274192117..comments2023-08-09T23:00:54.857+10:00Comments on Graham Dumpleton: Maintaining decorator state using a class.Graham Dumpletonhttp://www.blogger.com/profile/13609779138164842374noreply@blogger.comBlogger12125tag:blogger.com,1999:blog-2363643920942057324.post-85505870824483341402014-03-17T11:02:06.095+11:002014-03-17T11:02:06.095+11:00You need to use an extra level of indirection.
im...You need to use an extra level of indirection.<br /><br />import wrapt<br /><br />def action(pattern):<br />____def wrapper1(wrapped):<br />________@wrapt.decorator<br />________def wrapper2(wrapped, instance, args, kwargs):<br />____________print('wrapped pattern =', wrapped.pattern)<br />____________return wrapped(*args, **kwargs)<br />________wrapped.pattern = pattern<br />Graham Dumpletonhttps://www.blogger.com/profile/13609779138164842374noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-45360768639315506192014-03-16T22:52:36.892+11:002014-03-16T22:52:36.892+11:00Thanks for your reply. In the example I gave you, ...Thanks for your reply. In the example I gave you, you're right: there's no intercept when the function is being called. But at some point I want to add that.<br /><br />I'm going to experiment now with the "alternative approach [...] to use a class, where the wrapper function is the __call__() method of the class".Nicohttps://www.blogger.com/profile/04995520576748268624noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-20312522102388603772014-03-16T21:02:09.170+11:002014-03-16T21:02:09.170+11:00In your case I suspect you shouldn't even be u...In your case I suspect you shouldn't even be using the wrapt decorator factory as you don't want to intercept when the function is being called, you only want to add an attribute to the decorated function and return the original function again.<br /><br /># Replace leading underscores with spaces in the following.<br /><br />def action(pattern):<br />__def wrapper(wrapped):<br />Graham Dumpletonhttps://www.blogger.com/profile/13609779138164842374noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-58348311628750551792014-03-16T20:52:08.592+11:002014-03-16T20:52:08.592+11:00P.S.: I'm trying to combine examples from two ...P.S.: I'm trying to combine examples from two paragraphs in the docs: the one about "Decorators With Optional Arguments" and the one about "Decorating Instance Methods".Nicohttps://www.blogger.com/profile/04995520576748268624noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-55077895200732643212014-03-16T20:50:29.685+11:002014-03-16T20:50:29.685+11:00I read the docs yesterday and again today, and I&#...I read the docs yesterday and again today, and I'm still not seeing it. Could you please point out what needs to be fixed in the example script below?<br /><br />import wrapt<br />def action(pattern):<br /> @wrapt.decorator<br /> def wrapper(wrapped, instance, args, kwargs):<br /> wrapped.pattern = pattern<br /> print('wrapped pattern =', pattern)<br /> Nicohttps://www.blogger.com/profile/04995520576748268624noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-15002883818206014092014-03-16T20:28:58.412+11:002014-03-16T20:28:58.412+11:00You are not constructing the decorator correctly. ...You are not constructing the decorator correctly. Go and read:<br /><br />http://wrapt.readthedocs.org/en/latest/decorators.html#creating-decorators<br /><br />Your decorator wrapper function is meant to have more arguments than that. Specifically, it is meant to have wrapped, instance, args and kwargs arguments as described in the documentation.Graham Dumpletonhttps://www.blogger.com/profile/13609779138164842374noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-29908215868671027332014-03-16T20:22:59.224+11:002014-03-16T20:22:59.224+11:00I think you're right: there's no problem w...I think you're right: there's no problem with print, but there's another problem, and I can't figure out where it is.<br /><br />The short piece of code below appears to work. If I remove the '#' in front of @wrapt.decorator, it looks the decorator isn't called at all, hence no print output. Can you see what's wrong?<br /><br />#!/usr/bin/python3<br />import wrapt<Nicohttps://www.blogger.com/profile/04995520576748268624noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-85302483145445760592014-03-16T11:59:27.591+11:002014-03-16T11:59:27.591+11:00There should be no interaction between print and t...There should be no interaction between print and the use of the decorator factory. Are you sure you defined the decorator correctly? Ignoring the print, does the functionality of the decorator otherwise work?<br />Graham Dumpletonhttps://www.blogger.com/profile/13609779138164842374noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-75245848753777096002014-03-16T05:42:44.544+11:002014-03-16T05:42:44.544+11:00Is there some interaction between your 'wrapt&...Is there some interaction between your 'wrapt' module and print statements? I have an existing piece of code that uses print() inside a decorator for producing debugging output. This works fine if I don't use wrapt.decorator. If I add @wrapt.decorator, all lines of print() produce zero output.Nicohttps://www.blogger.com/profile/04995520576748268624noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-41995885347889536772014-02-27T08:19:40.693+11:002014-02-27T08:19:40.693+11:00Such a series! Very useful articles. The wrapt mod...Such a series! Very useful articles. The wrapt module is adorable.tomislaterhttps://www.blogger.com/profile/02400052243262571045noreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-7809153514645382092014-01-14T00:55:01.979+11:002014-01-14T00:55:01.979+11:00Thank you for taking the time to put these article...Thank you for taking the time to put these articles together!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2363643920942057324.post-74685139429109866972014-01-13T11:01:03.592+11:002014-01-13T11:01:03.592+11:00This series is a tour de force.This series is a tour de force.A. Jesse Jiryu Davishttps://www.blogger.com/profile/08612435176546178055noreply@blogger.com