From 7a4b857a0765490599dae89bbe1a55406e43f973 Mon Sep 17 00:00:00 2001 From: Rost Date: Sat, 30 Sep 2023 18:23:12 +0300 Subject: [PATCH 1/4] create ceche decorator --- app/main.py | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..79bc1ade 100644 --- a/app/main.py +++ b/app/main.py @@ -1,6 +1,16 @@ -from typing import Callable +from typing import Callable, Any def cache(func: Callable) -> Callable: - # Write your code here - pass + cache_storage = {} + + def wrapper(*args) -> Any: + nonlocal cache_storage + if args in cache_storage: + print("Getting from cache") + return cache_storage[args] + else: + print("Calculating new result") + cache_storage[args] = func(*args) + return cache_storage[args] + return wrapper From 29d450e57e3e19aa67260a7e0a7c5531ea943519 Mon Sep 17 00:00:00 2001 From: Rost Date: Sat, 30 Sep 2023 18:33:34 +0300 Subject: [PATCH 2/4] delete nonlocal --- app/main.py | 1 - 1 file changed, 1 deletion(-) diff --git a/app/main.py b/app/main.py index 79bc1ade..39eb04a2 100644 --- a/app/main.py +++ b/app/main.py @@ -5,7 +5,6 @@ def cache(func: Callable) -> Callable: cache_storage = {} def wrapper(*args) -> Any: - nonlocal cache_storage if args in cache_storage: print("Getting from cache") return cache_storage[args] From 36c16c1d3065f3992ce2405afa9737ecbe3f8371 Mon Sep 17 00:00:00 2001 From: Rost Date: Sat, 30 Sep 2023 18:39:58 +0300 Subject: [PATCH 3/4] removed else --- app/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 39eb04a2..768a606a 100644 --- a/app/main.py +++ b/app/main.py @@ -8,8 +8,8 @@ def wrapper(*args) -> Any: if args in cache_storage: print("Getting from cache") return cache_storage[args] - else: - print("Calculating new result") - cache_storage[args] = func(*args) - return cache_storage[args] + + print("Calculating new result") + cache_storage[args] = func(*args) + return cache_storage[args] return wrapper From 14377adda51e5de50076a354d6b781bf51f622d2 Mon Sep 17 00:00:00 2001 From: Rostyslav Chornyi <145897105+RVChornyy@users.noreply.github.com> Date: Thu, 5 Oct 2023 15:18:48 +0300 Subject: [PATCH 4/4] added else and removed return --- app/main.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 768a606a..b36ec4f1 100644 --- a/app/main.py +++ b/app/main.py @@ -7,9 +7,8 @@ def cache(func: Callable) -> Callable: def wrapper(*args) -> Any: if args in cache_storage: print("Getting from cache") - return cache_storage[args] - - print("Calculating new result") - cache_storage[args] = func(*args) + else: + print("Calculating new result") + cache_storage[args] = func(*args) return cache_storage[args] return wrapper