From 9050016ebd1fd58cd73a3f0df690716d0bc15f18 Mon Sep 17 00:00:00 2001 From: Many Date: Tue, 12 Sep 2023 18:32:59 +0300 Subject: [PATCH 1/4] 'Solution' --- app/main.py | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 057166eb..522de3a8 100644 --- a/app/main.py +++ b/app/main.py @@ -1,3 +1,17 @@ -def cache(func): - # Write your code here - pass +from typing import Callable +result_cache = {} # Словарь для кэшированных результатов + + +def cache(func: Callable) -> Callable: + def new_result(*args, **kwargs) -> None: + name_cache = func, args + if name_cache in result_cache: + print("Getting from cache") + return result_cache[name_cache] + + else: + result = func(*args, **kwargs) + result_cache[name_cache] = result + print("Calculating new result") + return result + return new_result From f7a9737a971cffc1830acb84277bab9f1b6e7e59 Mon Sep 17 00:00:00 2001 From: Many Date: Tue, 12 Sep 2023 18:50:23 +0300 Subject: [PATCH 2/4] 'Solution' --- app/main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 522de3a8..9b8f1fc7 100644 --- a/app/main.py +++ b/app/main.py @@ -1,5 +1,5 @@ from typing import Callable -result_cache = {} # Словарь для кэшированных результатов +result_cache = {} def cache(func: Callable) -> Callable: @@ -8,7 +8,6 @@ def new_result(*args, **kwargs) -> None: if name_cache in result_cache: print("Getting from cache") return result_cache[name_cache] - else: result = func(*args, **kwargs) result_cache[name_cache] = result From 51b42157b1e01c7ff573d645da23ebac355829fc Mon Sep 17 00:00:00 2001 From: Many Date: Tue, 12 Sep 2023 19:39:57 +0300 Subject: [PATCH 3/4] 'Solution' --- app/main.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 9b8f1fc7..69bc8a01 100644 --- a/app/main.py +++ b/app/main.py @@ -1,9 +1,9 @@ -from typing import Callable +from typing import Callable, Any result_cache = {} -def cache(func: Callable) -> Callable: - def new_result(*args, **kwargs) -> None: +def cache(func: Callable) -> Any: + def new_result(*args, **kwargs) -> Any: name_cache = func, args if name_cache in result_cache: print("Getting from cache") From ef9976bfc7fecea6fb9064f6ecb5df0222e25d38 Mon Sep 17 00:00:00 2001 From: Many Date: Tue, 12 Sep 2023 19:41:20 +0300 Subject: [PATCH 4/4] 'Solution' --- app/main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/main.py b/app/main.py index 69bc8a01..f414d528 100644 --- a/app/main.py +++ b/app/main.py @@ -1,8 +1,8 @@ -from typing import Callable, Any +from typing import Any result_cache = {} -def cache(func: Callable) -> Any: +def cache(func: Any) -> Any: def new_result(*args, **kwargs) -> Any: name_cache = func, args if name_cache in result_cache: