Created
December 8, 2013 22:10
-
-
Save nwh/7864552 to your computer and use it in GitHub Desktop.
secret santa assignments in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import smtplib | |
import json | |
from email.mime.text import MIMEText | |
message = '''Hello {santa}! | |
It's the secret santa elf here! You have been allocated to be secret | |
santa for {name} ({email}). Remember you only have a budget of $50, so | |
good luck! | |
But remember its got to be a secret! | |
Merry Christmas, | |
The Secret Santa Elf | |
''' | |
def send_email(pair): | |
mime_msg = MIMEText(message.format(name=pair['to_name'],email=pair['to'], | |
santa=pair['santa'])) | |
me = '[email protected]' | |
you = pair['santa'] | |
mime_msg['Subject'] = 'Secret Santa!!!' | |
mime_msg['From'] = me | |
mime_msg['To'] = you | |
# Send the message via our own SMTP server, but don't include the | |
# envelope header. | |
s = smtplib.SMTP_SSL('[smtp server]') | |
s.login('[smtp user]','[smtp pass]') | |
s.sendmail(me, [you], mime_msg.as_string()) | |
s.quit() | |
if __name__ == '__main__': | |
email_to_name = json.load(open('email_to_name.json')) | |
match_list = json.load(open('match_list.json')) | |
for m in match_list: | |
m['to_name'] = email_to_name[m['to']] | |
for m in match_list: | |
send_email(m) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import csv | |
import json | |
import random | |
def bad_match(match_dict,exc_dict): | |
for santa,target in match_dict.items(): | |
if target in exc_dict[santa]: | |
return True | |
return False | |
def random_match(email_list): | |
people_list = list(email_list) | |
random.shuffle(people_list) | |
match_dict = dict() | |
for i in range(len(people_list)-1): | |
match_dict[people_list[i]] = people_list[i+1] | |
match_dict[people_list[-1]] = people_list[0] | |
return match_dict | |
def get_match(exc_dict): | |
email_list = exc_dict.keys() | |
match_dict = random_match(email_list) | |
while bad_match(match_dict,exc_dict): | |
match_dict = random_match(email_list) | |
match_list = [{'santa':k, 'to':v} for k,v in match_dict.items()] | |
return match_list | |
def load_csv(csv_filename): | |
email_to_name = dict() | |
exclusions = dict() | |
with open(csv_filename) as csv_file: | |
csv_reader = csv.DictReader(csv_file) | |
for row in csv_reader: | |
email = row['email'].strip() | |
name = row['name'].strip() | |
exe = [row['exclude'].strip()] | |
email_to_name[email] = name | |
exclusions[email] = exe | |
#print(name + ' ' + email + ' ' + exe[0]) | |
return (email_to_name, exclusions) | |
if __name__ == '__main__': | |
email_to_name, exc_dict = load_csv('hh_secret_santa.csv') | |
match_list = get_match(exc_dict) | |
# save match list | |
json.dump(match_list,open('match_list.json','w'),indent=2) | |
# save email to name dict | |
json.dump(email_to_name,open('email_to_name.json','w'),indent=2) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment