-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbirthday_date.py
More file actions
53 lines (40 loc) · 1.48 KB
/
birthday_date.py
File metadata and controls
53 lines (40 loc) · 1.48 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
42
43
44
45
46
47
48
49
50
51
52
53
from datetime import datetime, timedelta
from collections import defaultdict
from pprint import pprint
def make_list():
users = []
with open("list_collegs.txt") as fd:
line = fd.readlines()
for el in line:
line_lst = el.strip("\n").split(" ")
dict = {"name": line_lst[0], "birthday": datetime.strptime(
line_lst[1], "%d-%m-%Y")}
users.append(dict)
return users
def get_next_week_start(d: datetime):
diff_days = 7 - d.weekday()
return d + timedelta(days=diff_days)
def prepare_birthday(d: datetime):
try:
return d.replace(year=datetime.now().year).date()
except ValueError as e:
return d.replace(year=datetime.now().year, day=28).date()
def get_birthdays_per_week(users):
birthdays = defaultdict(list)
today = datetime.now().date()
next_week_start = get_next_week_start(today)
start_period = next_week_start - timedelta(2)
end_period = next_week_start + timedelta(4)
happy_users = [user for user in users if start_period <=
prepare_birthday(user["birthday"]) <= end_period]
for user in happy_users:
current_bd = prepare_birthday(user["birthday"])
if current_bd.weekday() in (5, 6):
birthdays["Monday"].append(user["name"])
else:
birthdays[current_bd.strftime("%A")].append(user["name"])
return birthdays
if __name__ == "__main__":
users = make_list()
result = get_birthdays_per_week(users)
pprint(result)