-
Notifications
You must be signed in to change notification settings - Fork 2
/
decorator_pattern.py
40 lines (27 loc) · 1.1 KB
/
decorator_pattern.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import time
from typing import Callable
from dependency_injector import providers
from dependency_injector.containers import DeclarativeContainer
class ShopCartService:
def checkout(self) -> None:
print("Checkout is in progress...")
time.sleep(3)
class TimedShopCartService(ShopCartService):
def __init__(self, shop_cart_service: ShopCartService) -> None:
self._shop_cart_service = shop_cart_service
@staticmethod
def _timer(func: Callable) -> None:
tic = time.perf_counter()
func()
toc = time.perf_counter()
elapsed_time = toc - tic
print(f"Elapsed time: {elapsed_time:0.4f} seconds")
def checkout(self) -> None:
self._timer(self._shop_cart_service.checkout)
class Container(DeclarativeContainer):
# shop_cart_service = providers.Factory(ShopCartService)
shop_cart_service = providers.Factory(TimedShopCartService, providers.Factory(ShopCartService))
if __name__ == "__main__":
container = Container()
shop_cart_service: ShopCartService = container.shop_cart_service()
shop_cart_service.checkout()