Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from time import sleep
- from requests import get
- def cache(my_function):
- cached_data = {}
- def wrapper(*args, **kwargs):
- cache_key = args, tuple(sorted(kwargs.items()))
- nonlocal cached_data
- if cache_key not in cached_data:
- result = my_function(*args, **kwargs)
- cached_data[cache_key] = result
- return cached_data[cache_key]
- return wrapper
- @cache
- def get_currency_data(code: str):
- sleep(5)
- response = get(f"https://api.nbp.pl/api/exchangerates/rates/C/{code}/")
- return response.json()
- @cache
- def sum_two_numbers(a, b):
- return a + b
- # my_function = cache(get_currency_data)
- # print(my_function("EUR"))
- # result = cache(get_currency_data)("EUR")
- # print(result)
- eur = get_currency_data("EUR")
- print(eur)
- eur = get_currency_data("EUR")
- print(eur)
- usd = get_currency_data("USD")
- print(usd)
Add Comment
Please, Sign In to add comment