confused about python decorators
Posted
by nbv4
on Stack Overflow
See other posts from Stack Overflow
or by nbv4
Published on 2010-03-12T05:48:31Z
Indexed on
2010/03/12
5:57 UTC
Read the original article
Hit count: 515
I have a class that has an output() method which returns a matplotlib Figure instance. I have a decorator I wrote that takes that fig instance and turns it into a Django response object.
My decorator looks like this:
class plot_svg(object):
def __init__(self, view):
self.view = view
def __call__(self, *args, **kwargs):
print args, kwargs
fig = self.view(*args, **kwargs)
canvas=FigureCanvas(fig)
response=HttpResponse(content_type='image/svg+xml')
canvas.print_svg(response)
return response
and this is how it was being used:
def as_avg(self):
return plot_svg(self.output)()
The only reason I has it that way instead of using the "@" syntax is because when I do it with the "@":
@plot_svg
def as_svg(self):
return self.output()
I get this error:
as_svg() takes exactly 1 argument (0 given)
I'm trying to 'fix' this by putting it in the "@" syntax but I can't figure out how to get it working. I'm thinking it has something to do with self
not getting passed where it's supposed to...
© Stack Overflow or respective owner