-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathfunctions.py
More file actions
41 lines (30 loc) · 1.11 KB
/
functions.py
File metadata and controls
41 lines (30 loc) · 1.11 KB
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
# Avoid this:
# import csv
# def process_users(users, min_age, filename, send_email):
# adults = []
# for user in users:
# if user["age"] >= min_age:
# adults.append(user)
# with open(filename, mode="w", newline="", encoding="utf-8") as csv_file:
# writer = csv.writer(csv_file)
# writer.writerow(["name", "age"])
# for user in adults:
# writer.writerow([user["name"], user["age"]])
# if send_email:
# # Emailing logic here...
# return adults, filename
# Favor this:
import csv
def filter_adult_users(users, *, min_age=18):
"""Return users whose age is at least min_age."""
return [user for user in users if user["age"] >= min_age]
def save_users_csv(users, filename):
"""Save users to a CSV file."""
with open(filename, mode="w", newline="", encoding="utf-8") as csv_file:
writer = csv.writer(csv_file)
writer.writerow(["name", "age"])
for user in users:
writer.writerow([user["name"], user["age"]])
def send_users_report(filename):
"""Send the report."""
# Emailing logic here...