confused about python decorators
- by nbv4
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...