From 9b35a73694343a98fc08e149ebe07142ab785f4b Mon Sep 17 00:00:00 2001 From: Misha Sanin <109426701+mikesanin@users.noreply.github.com> Date: Sun, 22 Oct 2023 14:04:15 +0300 Subject: [PATCH] Update main.py without imports --- app/main.py | 44 +++++++++++++++++++++++--------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/app/main.py b/app/main.py index d9a9749de..31b5be91f 100644 --- a/app/main.py +++ b/app/main.py @@ -1,30 +1,32 @@ -from typing import List, Union +from typing import List, Optional class Person: - people = {} + people: dict[str, "Person"] = {} - def __init__(self, name: str, age: int) -> None: - self.name = name - self.age = age - self.wife: Union[None, "Person"] = None - self.husband: Union[None, "Person"] = None + def __init__(self, name: str, age: int, spouse: Optional["Person"] = None): + self.name: str = name + self.age: int = age + self.spouse: Optional['Person'] = spouse Person.people[name] = self - def create_person_list(people: List[dict]) -> List[Person]: - person_list = [] - for person_dict in people: - name = person_dict["name"] - age = person_dict["age"] - person = Person(name, age) - if "wife" in person_dict and person_dict["wife"]: - person.wife = Person.people.get(person_dict["wife"]) - if person.wife: - person.wife.husband = person - if "husband" in person_dict and person_dict["husband"]: - person.husband = Person.people.get(person_dict["husband"]) - if person.husband: - person.husband.wife = person + person_list: List[Person] = [] + + for person_data in people: + name: str = person_data["name"] + age: int = person_data["age"] + spouse_name: str = person_data.get("wife") or person_data.get("husband") + + spouse: Optional[Person] = Person.people.get(spouse_name) + + person: Person = Person(name, age, spouse) person_list.append(person) + + if spouse: + if "wife" in person_data: + person.wife = spouse + else: + person.husband = spouse + return person_list