From 480dc9824a30de23f1a1ce6ec56ae2d8735fe645 Mon Sep 17 00:00:00 2001 From: Julia Alexeeva Date: Tue, 5 Nov 2024 18:04:18 +0200 Subject: [PATCH 1/4] 'solution' --- app/main.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 68287892..2a2c2f0e 100644 --- a/app/main.py +++ b/app/main.py @@ -2,5 +2,14 @@ def cache(func: Callable) -> Callable: - # Write your code here - pass + cached_data = {} + + def wrapper(*args, **kwargs) -> Callable: + parameters = tuple(args) + if parameters not in cached_data.keys(): + result = func(*args, **kwargs) + cached_data[parameters] = result + return result + else: + return cached_data.get(parameters) + return wrapper From c8da4375510fe40d68f43c4eafb7e3e0c04a38da Mon Sep 17 00:00:00 2001 From: Julia Alexeeva Date: Tue, 5 Nov 2024 18:11:37 +0200 Subject: [PATCH 2/4] add text output ('Calculating new result' and 'Getting from cache'). --- app/main.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/app/main.py b/app/main.py index 2a2c2f0e..7cfd8f42 100644 --- a/app/main.py +++ b/app/main.py @@ -9,7 +9,9 @@ def wrapper(*args, **kwargs) -> Callable: if parameters not in cached_data.keys(): result = func(*args, **kwargs) cached_data[parameters] = result + print("Calculating new result") return result else: + print("Getting from cache") return cached_data.get(parameters) return wrapper From d242dd1113bf4dbf5f3ba2d82b71d04d671a01d0 Mon Sep 17 00:00:00 2001 From: Julia Alexeeva Date: Tue, 26 Nov 2024 14:09:47 +0200 Subject: [PATCH 3/4] Remade dictionary methods, removed excessive variables --- app/main.py | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/app/main.py b/app/main.py index 7cfd8f42..ba812d26 100644 --- a/app/main.py +++ b/app/main.py @@ -1,17 +1,14 @@ -from typing import Callable +from collections.abc import Callable def cache(func: Callable) -> Callable: - cached_data = {} + storage = {} - def wrapper(*args, **kwargs) -> Callable: - parameters = tuple(args) - if parameters not in cached_data.keys(): - result = func(*args, **kwargs) - cached_data[parameters] = result + def wrapper(*args) -> Callable: + if args not in storage: print("Calculating new result") - return result + return storage.setdefault(args, func(*args)) else: print("Getting from cache") - return cached_data.get(parameters) + return storage.get(args) return wrapper From c0004cc63d30ac805f6862ec4989976f5aadd0e9 Mon Sep 17 00:00:00 2001 From: Julia Alexeeva Date: Tue, 26 Nov 2024 20:12:14 +0200 Subject: [PATCH 4/4] Removed redundant 'return', conditions in 'else/if' switched (to avoid using 'not'. --- app/main.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/app/main.py b/app/main.py index ba812d26..055d04a3 100644 --- a/app/main.py +++ b/app/main.py @@ -5,10 +5,11 @@ def cache(func: Callable) -> Callable: storage = {} def wrapper(*args) -> Callable: - if args not in storage: - print("Calculating new result") - return storage.setdefault(args, func(*args)) - else: + if args in storage: print("Getting from cache") - return storage.get(args) + else: + print("Calculating new result") + storage.setdefault(args, func(*args)) + return storage.get(args) + return wrapper