Advertisement
ksieradzinski

Untitled

Mar 18th, 2025
13
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. from time import sleep
  2. from requests import get
  3.  
  4.  
  5. def cache(my_function):
  6. cached_data = {}
  7. def wrapper(*args, **kwargs):
  8. cache_key = args, tuple(sorted(kwargs.items()))
  9. nonlocal cached_data
  10. if cache_key not in cached_data:
  11. result = my_function(*args, **kwargs)
  12. cached_data[cache_key] = result
  13.  
  14. return cached_data[cache_key]
  15.  
  16. return wrapper
  17.  
  18.  
  19. @cache
  20. def get_currency_data(code: str):
  21. sleep(5)
  22. response = get(f"https://api.nbp.pl/api/exchangerates/rates/C/{code}/")
  23. return response.json()
  24.  
  25. @cache
  26. def sum_two_numbers(a, b):
  27. return a + b
  28.  
  29. # my_function = cache(get_currency_data)
  30. # print(my_function("EUR"))
  31.  
  32. # result = cache(get_currency_data)("EUR")
  33. # print(result)
  34.  
  35. eur = get_currency_data("EUR")
  36. print(eur)
  37.  
  38.  
  39. eur = get_currency_data("EUR")
  40. print(eur)
  41.  
  42. usd = get_currency_data("USD")
  43. print(usd)
  44.  
  45.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement