This repository was archived by the owner on Apr 18, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomChoreAssign.py
More file actions
114 lines (81 loc) · 2.99 KB
/
Copy pathrandomChoreAssign.py
File metadata and controls
114 lines (81 loc) · 2.99 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
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
#! python3
# randomChoreAssign.py - Randomly assigns list of chores to list of people,
# and emails each their assigned chore.
import ast, os, random, re, smtplib
# Edit these values as needed.
chores_master = ['bathroom', 'dishes', 'floor', 'kitchen', 'laundry']
family_path = os.path.join(os.getcwd() + '\\family.txt')
log_path = os.path.join(os.getcwd() + '\\log.txt')
family = {}
# For recurring runs.
if os.path.exists(family_path):
# Read and save the dict of family members.
family_file = open(family_path, 'r')
family = ast.literal_eval(family_file.read())
family_file.close()
pass
# Else log_file doesn't exist, write family members.
else:
while True:
new_family = input('Please input a family member\'s name: ')
new_email = input('Please input a family member\'s email: ')
family[new_family] = new_email
prompt = input('Are there any additional family members? ')
# Continue to write family members until otherwise specified.
if re.search(r'^y', prompt, flags=re.IGNORECASE):
pass
# Write family to txt file for later reference.
else:
family_file = open(family_path, 'w')
family_file.write(str(family))
family_file.close()
break
# Log into email server.
my_email = input('Please enter your email address: ')
my_password = input('Please enter your password: ')
smtpObj = smtplib.SMTP('smtp.gmail.com', 587)
smtpObj.ehlo()
smtpObj.starttls()
smtpObj.login(my_email, my_password)
# Stored separately as list will be popped off.
chores = chores_master.copy()
assignments = {}
prev_assignments = {}
# Populate previous assignments.
if os.path.exists(log_path):
prev_assign_file = open(log_path, 'r')
prev_assignments = ast.literal_eval(prev_assign_file.read())
prev_assign_file.close()
# Family member, family member's email address.
for k, v in family.items():
# If log is already populated, pick a chore at random then check for previous assignments.
if len(prev_assignments) > 0:
while True:
random_chore = random.choice(chores)
# If the choice is different than the previous, proceed.
if random_chore != prev_assignments[k]:
break
# Else fetch a chore at random.
else:
random_chore = random.choice(chores)
# Pop the core from the list.
chores.remove(random_chore)
# Send as email.
email_message = 'Subject: This Week\'s Chore Assignment.\n' +\
'Dear %s,\n\nYour assignment this week is to clean the %s.\n\nMuch appreciated!' %\
(k, random_chore)
print('Sending message to %s...' % v)
# Check for any errors.
send_status = smtpObj.sendmail(my_email, v, email_message)
if send_status != {}:
print('There was a problem sending to %s: %s.' % (v, send_status))
# Write the chore to assignments to be logged.
assignments[k] = random_chore
# Close the email object.
smtpObj.quit()
# Save assignments to the log for future reference.
# Opening in write mode rewrites all content to null.
log_file = open(log_path, 'w')
log_file.write(str(assignments))
log_file.close()
print('All assignments have been sent out. Great job, everybody!')