This commit is contained in:
Eden Kirin
2023-02-16 11:09:23 +01:00
commit e1d3226056
9 changed files with 195 additions and 0 deletions

0
lib/__init__.py Normal file
View File

37
lib/gen_1.py Normal file
View File

@ -0,0 +1,37 @@
from dataclasses import dataclass
from typing import List, Optional
@dataclass
class UserBase:
id: int
first_name: str
last_name: str
@dataclass
class Employee(UserBase):
department: str
@dataclass
class Partner(UserBase):
company: str
class UserContainer(list):
def get_user_by_id(self, id: int) -> Optional[UserBase]:
for user in self:
if user.id == id:
return user
return None
class EmployeeContainer(UserContainer):
def get_by_department(self, department: str) -> List[Employee]:
return [user for user in self if user.department == department]
class PartnerContainer(UserContainer):
def get_by_company(self, company: str) -> List[Partner]:
return [user for user in self if user.company == company]

40
lib/gen_2.py Normal file
View File

@ -0,0 +1,40 @@
from dataclasses import dataclass
from typing import Generic, List, Optional, TypeVar
@dataclass
class UserBase:
id: int
first_name: str
last_name: str
@dataclass
class Employee(UserBase):
department: str
@dataclass
class Partner(UserBase):
company: str
UserContainerType = TypeVar("UserContainerType", bound=UserBase)
class UserContainer(List[UserContainerType]):
def get_user_by_id(self, id: int) -> Optional[UserContainerType]:
for user in self:
if user.id == id:
return user
return None
class EmployeeContainer(UserContainer[Employee]):
def get_by_department(self, department: str) -> List[Employee]:
return [user for user in self if user.department == department]
class PartnerContainer(UserContainer[Partner]):
def get_by_company(self, company: str) -> List[Partner]:
return [user for user in self if user.company == company]