forked from ArjanCodes/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_email_function.py
117 lines (94 loc) · 2.61 KB
/
4_email_function.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
from dataclasses import dataclass
from functools import lru_cache, partial
from typing import Protocol
from email_tools.service_v2 import send_email
SMTP_SERVER = "smtp.gmail.com"
PORT = 465
EMAIL = "[email protected]"
PASSWORD = "password"
class EmailSender(Protocol):
def __call__(self, to_email: str, subject: str, body: str) -> None: ...
@lru_cache
def bmi(weight: float, height: float) -> float:
return weight / (height**2)
def bmi_category(bmi_value: float) -> str:
if bmi_value < 18.5:
return "Underweight"
elif bmi_value < 25:
return "Normal"
elif bmi_value < 30:
return "Overweight"
else:
return "Obese"
@dataclass
class Stats:
age: int
gender: str
height: float
weight: float
blood_type: str
eye_color: str
hair_color: str
@dataclass
class Address:
address_line_1: str
address_line_2: str
city: str
country: str
postal_code: str
def __str__(self) -> str:
return f"{self.address_line_1}, {self.address_line_2}, {self.city}, {self.country}, {self.postal_code}"
@dataclass
class Person:
name: str
address: Address
email: str
phone_number: str
stats: Stats
def split_name(self) -> tuple[str, str]:
first_name, last_name = self.name.split(" ")
return first_name, last_name
def update_email(self, email: str, send_message: EmailSender) -> None:
self.email = email
send_message(
to_email=email,
subject="Your email has been updated.",
body="Your email has been updated. If this was not you, you have a problem.",
)
def main() -> None:
# create a person
address = Address(
address_line_1="123 Main St",
address_line_2="Apt 1",
city="New York",
country="USA",
postal_code="12345",
)
stats = Stats(
age=30,
gender="Male",
height=1.8,
weight=80,
blood_type="A+",
eye_color="Brown",
hair_color="Black",
)
person = Person(
name="John Doe",
email="[email protected]",
phone_number="123-456-7890",
address=address,
stats=stats,
)
print(address)
# compute the BMI
bmi_value = bmi(stats.weight, stats.height)
print(f"Your BMI is {bmi_value:.2f}")
print(f"Your BMI category is {bmi_category(bmi_value)}")
# update the email address
send_message = partial(
send_email, smtp_server=SMTP_SERVER, port=PORT, email=EMAIL, password=PASSWORD
)
person.update_email("[email protected]", send_message)
if __name__ == "__main__":
main()