|
| 1 | +from collections.abc import Iterable, Sequence |
| 2 | +from dataclasses import dataclass |
| 3 | +from typing import Generator, NewType, TypeVar, TYPE_CHECKING |
| 4 | + |
| 5 | + |
| 6 | +type UserData = dict[str, str | int] |
| 7 | +type OptUserData = UserData | None |
| 8 | + |
| 9 | + |
| 10 | +@dataclass |
| 11 | +class User: |
| 12 | + user_id: str |
| 13 | + name: str |
| 14 | + age: int |
| 15 | + |
| 16 | + def __bool__(self) -> bool: |
| 17 | + return bool(self.user_id) |
| 18 | + |
| 19 | + |
| 20 | +@dataclass |
| 21 | +class Person(User): |
| 22 | + pass |
| 23 | + |
| 24 | + |
| 25 | +def fetch_url(url: str) -> UserData: |
| 26 | + return { |
| 27 | + "user_id": "1234", |
| 28 | + "name": "steve", |
| 29 | + "age": 99, |
| 30 | + } |
| 31 | + |
| 32 | + |
| 33 | +def get_user_data(user: User) -> OptUserData: |
| 34 | + if not user: |
| 35 | + return None |
| 36 | + |
| 37 | + data = fetch_url(user.user_id) |
| 38 | + return data |
| 39 | + |
| 40 | + |
| 41 | +def run_get_user_data() -> None: |
| 42 | + user1 = User("1234", "steve", 99) |
| 43 | + data1 = get_user_data(user1) |
| 44 | + print(f"{user1=} got {data1=}") |
| 45 | + |
| 46 | + data2: OptUserData |
| 47 | + |
| 48 | + data2 = data1 |
| 49 | + print(f"{user1=} got {data2=}") |
| 50 | + |
| 51 | + user3 = User("", "", 0) |
| 52 | + data3 = get_user_data(user3) |
| 53 | + print(f"{user3=} got {data3=}") |
| 54 | + |
| 55 | + user4 = Person("5432", "woz", 1024) |
| 56 | + data4 = get_user_data(user4) |
| 57 | + print(f"{user4=} got {data4=}") |
| 58 | + |
| 59 | + |
| 60 | +Celsius = NewType("Celsius", float) |
| 61 | +Fareng = NewType("Fareng", float) |
| 62 | + |
| 63 | + |
| 64 | +def convert_c_to_f(temp: Celsius) -> Fareng: |
| 65 | + return Fareng(temp * 9 / 5 + 32) |
| 66 | + |
| 67 | + |
| 68 | +def gen_temps(start: Celsius) -> Generator[Celsius, None, str]: |
| 69 | + yield start |
| 70 | + val = yield Celsius(start + 10) |
| 71 | + yield Celsius(start + 15) |
| 72 | + |
| 73 | + return "finish" |
| 74 | + |
| 75 | + |
| 76 | +def get_max_temp(temps: Iterable[Celsius]) -> Celsius | None: |
| 77 | + if not temps: |
| 78 | + return None |
| 79 | + |
| 80 | + return max(temps) |
| 81 | + |
| 82 | + |
| 83 | +def get_first_temp(temps: Sequence[Celsius]) -> Celsius | None: |
| 84 | + if not temps: |
| 85 | + return None |
| 86 | + |
| 87 | + return temps[0] |
| 88 | + |
| 89 | + |
| 90 | +def run_convert_c_to_f() -> None: |
| 91 | + temp1 = Celsius(-40.0) |
| 92 | + faren1 = convert_c_to_f(temp1) |
| 93 | + print(f"{temp1=}, {faren1=}, {type(temp1)=}, {type(faren1)=}") |
| 94 | + |
| 95 | + max_temp1 = get_max_temp([Celsius(-40.0), Celsius(5.1), Celsius(36.0)]) |
| 96 | + print(f"{max_temp1=}") |
| 97 | + |
| 98 | + max_temp2 = get_max_temp([]) |
| 99 | + print(f"{max_temp2=}") |
| 100 | + |
| 101 | + temps3: tuple[Celsius, ...] = (Celsius(-40.0), Celsius(5.1), Celsius(36.0)) |
| 102 | + max_temp3 = get_max_temp(temps3) |
| 103 | + print(f"{max_temp3=}") |
| 104 | + |
| 105 | + temps4 = gen_temps(Celsius(10.0)) |
| 106 | + max_temp4 = get_max_temp(temps4) |
| 107 | + print(f"{max_temp4=}") |
| 108 | + |
| 109 | + first_temp1 = get_first_temp([Celsius(-40.0), Celsius(5.1), Celsius(36.0)]) |
| 110 | + print(f"{first_temp1=}") |
| 111 | + |
| 112 | + first_temp2 = get_first_temp([]) |
| 113 | + print(f"{first_temp2=}") |
| 114 | + |
| 115 | + f_temps3: tuple[Celsius, ...] = (Celsius(-40.0), Celsius(5.1), Celsius(36.0)) |
| 116 | + first_temp3 = get_first_temp(f_temps3) |
| 117 | + print(f"{first_temp3=}") |
| 118 | + |
| 119 | + # f_temps4 = gen_temps(Celsius(10.0)) |
| 120 | + # first_temp4 = get_first_temp(f_temps4) |
| 121 | + # print(f"{first_temp4=}") |
| 122 | + |
| 123 | + |
| 124 | +# T = TypeVar("T") |
| 125 | + |
| 126 | + |
| 127 | +def get_first_temp_generic[T](temps: Sequence[T]) -> T | None: |
| 128 | + if not temps: |
| 129 | + return None |
| 130 | + |
| 131 | + return temps[0] |
| 132 | + |
| 133 | + |
| 134 | +def run_generic() -> None: |
| 135 | + first_temp1 = get_first_temp_generic([Celsius(-40.0), Celsius(5.1), Celsius(36.0)]) |
| 136 | + print(f"{first_temp1=}") |
| 137 | + |
| 138 | + first_temp2: Fareng | None = get_first_temp_generic( |
| 139 | + [Fareng(-40.0), Fareng(5.1), Fareng(36.0)] |
| 140 | + ) |
| 141 | + print(f"{first_temp2=}") |
| 142 | + |
| 143 | + # first_temp3: Celsius | None = get_first_temp_generic( |
| 144 | + # [Fareng(-40.0), Fareng(5.1), Fareng(36.0)] |
| 145 | + # ) |
| 146 | + # print(f"{first_temp3=}") |
| 147 | + |
| 148 | + |
| 149 | +if __name__ == "__main__": |
| 150 | + print(f"{TYPE_CHECKING=}") |
| 151 | + |
| 152 | + run_get_user_data() |
| 153 | + print("\n------------\n") |
| 154 | + |
| 155 | + run_convert_c_to_f() |
| 156 | + print("\n------------\n") |
| 157 | + |
| 158 | + run_generic() |
| 159 | + print("\n------------\n") |
0 commit comments