-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservers.py
96 lines (66 loc) · 2.97 KB
/
servers.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/python
# -*- coding: utf-8 -*-
from typing import List, Dict, Optional, TypeVar
from abc import ABC, abstractmethod
import re
class Product:
def __init__(self, name: str, price: float) -> None:
match_pattern = "^[a-zA-Z]+[0-9]+$"
if not re.fullmatch(match_pattern, name):
raise ValueError
self.name: str = name
self.price: float = price
def __eq__(self, other) -> bool:
return self.name == other.name and self.price == other.price
def __hash__(self):
return hash((self.name, self.price))
class ServerError(Exception):
def __init__(self, server, msg=None):
if msg is None:
msg = f"An error occured with server {server}"
super().__init__(msg)
self.server = server
class TooManyProductsFoundError(ServerError, ValueError):
def __init__(self, msg):
super().__init__(msg)
class Server(ABC):
n_max_returned_entries: int = 3
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
def get_entries(self, n_letters: Optional[int] = 1) -> List[Product]:
match_pattern = "^[a-zA-Z]{{{n_letters}}}[0-9]{{2,3}}$".format(n_letters=n_letters)
entries_list = [product for product in self._get_products_list() if re.match(match_pattern, product.name)]
if len(entries_list) > Server.n_max_returned_entries:
raise TooManyProductsFoundError("Too many products on server.")
return sorted(entries_list, key=lambda product: product.price)
@abstractmethod
def _get_products_list(self, n_letters: Optional[int] = 1) -> List[Product]:
raise NotImplementedError
HelperType = TypeVar('HelperType', bound=Server)
class ListServer(Server):
def __init__(self, products: List[Product], *args, **kwargs):
self.products = products
super().__init__(*args, *kwargs)
def _get_products_list(self, n_letters: Optional[int] = 1) -> List[Product]:
return self.products
class MapServer(Server):
def __init__(self, products: List[Product], *args, **kwargs):
self.products: Dict[str, Product] = {elem.name: elem for elem in products}
super().__init__(*args, *kwargs)
def _get_products_list(self, n_letters: Optional[int] = 1) -> List[Product]:
return list(self.products.values())
class Client:
def __init__(self, server: HelperType):
self.server = server
def get_total_price(self, n_letters: Optional[int]) -> Optional[float]:
try:
if n_letters is None:
matching_products = self.server.get_entries()
else:
matching_products = self.server.get_entries(n_letters)
except TooManyProductsFoundError:
return None
else:
if len(matching_products) == 0:
return None
return sum([product.price for product in matching_products])