tunaktunak tunaktunak - 1 year ago 58
Python Question

Python Decorators - <Classname> object has no attribute '__name__'

I have a tornado method like below and I tried to decorate method to cache stuff. I have the following setup

def request_cacher(x):
def wrapper(funca):
def wrapped_f(self, *args, **kwargs):

return wrapped_f
return wrapper

class PhotoListHandler(BaseHandler):
def get(self):

I am receiving the error,
AttributeError: 'PhotoListHandler' object has no attribute '__name__'

Any ideas?

Answer Source

i think this might work for you,

import tornado.ioloop
import tornado.web
from tornado.gen import coroutine
from functools import wraps

cache = {}

class cached(object):
    def __init__ (self, rule, *args, **kwargs):
        self.args = args
        self.kwargs = kwargs
        self.rule = rule
        cache[rule] = 'xxx'

    def __call__(self, fn):
        def newf(*args, **kwargs):
            slf = args[0]
            if cache.get(self.rule):
            return fn(*args, **kwargs)
        return newf 

class MainHandler(tornado.web.RequestHandler):

    def get(self):
        print "helloo"
        self.write("Hello, world")

def make_app():
    return tornado.web.Application([
        (r"/", MainHandler),

if __name__ == "__main__":
    app = make_app()