From d1c4c101eac51fae67266a67bce3b5c4734dd9ab Mon Sep 17 00:00:00 2001 From: Lytillis Date: Thu, 28 Sep 2023 16:27:12 +0300 Subject: [PATCH 1/4] Cache decorator solution --- app/main.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..a8cb6406 100644 --- a/app/main.py +++ b/app/main.py @@ -2,5 +2,13 @@ def cache(func: Callable) -> Callable: - # Write your code here - pass + stored_runs = {} + + def wrapper(*args): + if args in stored_runs: + print("Getting from cache") + return stored_runs[args] + print("Calculating new result") + stored_runs.update({args: func(*args)}) + return stored_runs[args] + return wrapper From d794a30825dc5c064d27a4138f0f8cc81621cff3 Mon Sep 17 00:00:00 2001 From: Lytillis Date: Thu, 28 Sep 2023 16:31:25 +0300 Subject: [PATCH 2/4] Fixed flake8 issue --- app/main.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/main.py b/app/main.py index a8cb6406..fc8ab94b 100644 --- a/app/main.py +++ b/app/main.py @@ -4,7 +4,7 @@ def cache(func: Callable) -> Callable: stored_runs = {} - def wrapper(*args): + def wrapper(*args) -> int: if args in stored_runs: print("Getting from cache") return stored_runs[args] From e124736f2a76c3ec48430ca769a06c367de5d51b Mon Sep 17 00:00:00 2001 From: Lytillis Date: Fri, 29 Sep 2023 08:10:05 +0300 Subject: [PATCH 3/4] Resolved wrapper functions annotation --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index fc8ab94b..822b54f2 100644 --- a/app/main.py +++ b/app/main.py @@ -1,10 +1,10 @@ -from typing import Callable +from typing import Callable, Any def cache(func: Callable) -> Callable: stored_runs = {} - def wrapper(*args) -> int: + def wrapper(*args) -> Any: if args in stored_runs: print("Getting from cache") return stored_runs[args] From e8460e2be6328518b22a58db8d53506c56106fe6 Mon Sep 17 00:00:00 2001 From: Lytillis Date: Sat, 14 Oct 2023 15:49:25 +0300 Subject: [PATCH 4/4] Minor changes --- app/main.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/main.py b/app/main.py index 822b54f2..b14488e6 100644 --- a/app/main.py +++ b/app/main.py @@ -5,10 +5,10 @@ def cache(func: Callable) -> Callable: stored_runs = {} def wrapper(*args) -> Any: - if args in stored_runs: + if args not in stored_runs: + print("Calculating new result") + stored_runs[args] = func(*args) + else: print("Getting from cache") - return stored_runs[args] - print("Calculating new result") - stored_runs.update({args: func(*args)}) return stored_runs[args] return wrapper