Guest User

Untitled

a guest
Jun 25th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. @cacheable('/path/to/cache/file')
  2. def my_function(a, b, c):
  3. return 'something'
  4.  
  5. def decorator(f):
  6. def decorated(*args,**kwargs):
  7. cache = Cache(cachepath)
  8. if cache.iscached(*args,**kwargs):
  9. ...
  10. else:
  11. res = f(*args,**kwargs)
  12. cache.store((*args,**kwargs), res)
  13. return res
  14. return decorated
  15.  
  16. def cache(filepath)
  17. def decorator(f):
  18. def decorated(*args,**kwargs):
  19. cache = Cache(cachepath)
  20. if cache.iscached(*args,**kwargs):
  21. ...
  22. else:
  23. res = f(*args,**kwargs)
  24. cache.store((*args,**kwargs), res)
  25. return res
  26. return decorated
  27. return decorator
  28.  
  29. def mydecorator(func):
  30. def wrapper(*args, **kwargs):
  31. return func(*args, **kwargs)
  32. return wrapper
  33.  
  34. @mydecorator
  35. def foo(a, b, c):
  36. pass
  37.  
  38. def addint(val):
  39. def decorator(func):
  40. def wrapped(*args, **kwargs):
  41. result = func(*args, **kwargs)
  42. return result + val
  43. return wrapped # returns the decorated function "add_together"
  44. return decorator # returns the definition of the decorator "addint"
  45. # specifically built to return an extra 5 to the sum
  46.  
  47. @addint(5)
  48. def add_together(a, b):
  49. return a + b
  50.  
  51. print add_together(1, 2)
  52. # prints 8, not 3
Add Comment
Please, Sign In to add comment