我試圖構建我的第一個裝飾器並在類中實現它。python3從類到裝飾器的自變量
# decorator class
class Cache(object):
def __init__(self,filename,**kwargs):
self.time_ago = datetime.now() - timedelta(**kwargs)
self.filename = filename
def __call__(self,fn):
if not os.path.isfile(self.filename):
return self.cache(fn(self))
time_ago = self.time_ago
filename = self.filename
c_age = datetime.fromtimestamp(os.path.getctime(filename))
m_age = datetime.fromtimestamp(os.path.getmtime(filename))
print (c_age)
print (m_age)
print (time_ago)
if c_age < time_ago or m_age < time_ago:
return self.cache(fn(self))
else:
return self.read()
def cache(self,data):
with open(self.filename,'r+') as ef:
ef.write(data)
return ef.read()
def read(self):
f = open(self.filename,'r')
data = f.read()
f.close()
return data
我想打電話給在下面的類裝飾:
class Zabb(object):
@Cache('nodes.json',minutes=1)
def getNodes(self):
return "Get Nodes"
我這樣稱呼它:
z = Zabb()
nodes = z.getNodes()
我得到以下錯誤:
Traceback (most recent call last):
File "./deco.py", line 52, in <module>
nodes = z.getNodes()
TypeError: 'str' object is not callable
我已經接近完成這項工作。我究竟做錯了什麼?
'__call__'需要返回高階函數(包裹) –