def mu_cache(func,maxsize=128):
"Most Used Cache decorator"
cname = "__"+func.__name__+"_Cache"
rname = "__"+func.__name__+"_Rankings"
exec("global "+cname)
exec(cname+" = {}") #here
exec("global "+rname)
exec(rname+" = {}")
def wrap(*args,**kwargs):
cache = eval(cname)
rankings = eval(rname)
try:
res = cache[(args,kwargs)]
except:
res = func(*args,**kwargs)
cache[(args,kwargs)] = res
else:
try:
res = rname[(args,kwargs)]
except:
res=0
rname[(args,kwargs)] = res+1
def delete_smallest():
global rankings
smallest=None
todelete=None
for args in rankings:
r = rankings[args]
if (smallest == None) or r<=smallest:
smallest=r
todelete=args
del cache[todelete]
while len(cache)> maxsize:
delete_smallest()
return wrap
SyntaxError: unqualified exec is not allowed in function 'mu_cache' it contains a nested function with free variables (<pyshell#10>, line 3)
I have no idea what this means; and I am trying to make a copy of functools.lru_cache
but with Most Used instead of Least Recently Used.