This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ----------- File zoos_config_builder.py ----------------- | |
from functools import partial | |
class ZOOsConfigBuilder: | |
... | |
def get_config(self): | |
return { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass | |
from typing import Callable, Dict | |
TZooOwnerName = str | |
TZooSize = int | |
@dataclass | |
class ZooConfiguration: | |
is_open_to_public: bool | |
get_config: Callable[[TZooOwnerName, TZooSize], Dict] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TCityName = str | |
class ZOOsConfigBuilder: | |
def get_configs(self) -> Dict[TCityName, ZooConfiguration]: | |
return { | |
"paris": ZooConfiguration( | |
is_open_to_public=True, | |
get_config=self:_build_paris_config(["girrafe", "lion", "ape"]), | |
), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class City: | |
# 1. add type hints, here we see that name shouldn't* be None | |
name: str = "vienna" | |
# 2. add more type hints | |
def create_zoo(self, owner: str) -> Optional[Zoo]: | |
zoo_configs: Dict[ | |
str, ZooConfiguration | |
] = ZOOsConfigBuilder().get_configs() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import datetime | |
from models import Person | |
class Zoo: | |
def __init__(zoo_config): | |
self._owner_name = zoo_config["owner"] | |
# 1. owner appears to be mandatory, it is actually the owner's name | |
self._free_entrance_day = zoo_confi.get("free_entrance_day", "Monday") | |
# 2. free_entrance_day is not mandatory, it must be the english name | |
# for a week day, can I set it to None in zoo_config or will this |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from dataclasses import dataclass, field | |
from enum import Enum | |
from typing import List, Optional | |
from datetime import date | |
from models import Animal, Person | |
# 1. We define an Enum of possible weekday names | |
class WeekDay(Enum): | |
MON = "Monday" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Zoo: | |
def __init__(zoo_outline: ZooOutline): | |
self._owner_name: str = zoo_outline.owner_name | |
# TODO: It would be great to replace self._owner_name with | |
# reference to an actual Person model instance from the database. | |
# We probably get the owner name by fetching the owner person | |
# from the database. | |
# But this might be too invasive. | |
self._free_entrance_day = zoo_outline.free_entrance_day.value | |
# zoo_outline.free_entrance_day is an Enum, thus we call .value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ZOOsConfigBuilder: | |
def _build_paris_config(animal_types): | |
def _get_config(owner: str, zoo_size: int) -> ZooOutline: | |
animals: List[Animal] = create_animals(animal_types) | |
return ZooOutline( | |
# I also had to rename owner to owner_name and | |
# because I am now instantiating a class | |
# PyCharm automatically underlines the old |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class City: | |
name: str = "vienna" | |
def create_zoo(self, owner_name: str) -> Optional[Zoo]: | |
zoo_configs: Dict[ | |
str, ZooConfiguration | |
] = ZOOsConfigBuilder().get_configs() | |
config = zoo_configs.get(self.name) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ ip -c addr show | |
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000 | |
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 | |
inet 127.0.0.1/8 scope host lo | |
valid_lft forever preferred_lft forever | |
inet6 ::1/128 scope host | |
valid_lft forever preferred_lft forever | |
2: wlp58s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000 | |
link/ether 8c:b2:e0:dc:54:a3 brd ff:ff:ff:ff:ff:ff | |
inet 10.0.0.8/24 brd 10.0.0.255 scope global dynamic noprefixroute wlp58s0 |
OlderNewer