secret_santa.py

This year the family is doing a secret santa, instead of the regular gifts-for-everyone approach. I think it's a good idea as I only have to think up one amazing present instead of seven.

There was talk about who would do the randomizing of names, which ruined the surprise for that one person. I suggested that the internet could probably provide that service, but then hesitated at the thought of putting all our email addresses into a random website.

I decided to write my own secret santa service instead. At first I wrote it in PHP - like this website is - and spent a good hour trying to remember basic PHP concepts like "do lines have to end with semi-colons?" and "how do you print to standard out?". I used to PHP errday, so this made me feel bad. I reasoned that I forgot all my PHP from 2010 because I moved to python after that. I figured I would write secret santa in Python.

Then I spent another few hours trying to remember basic python syntax, and debugging issues. It took me about the same amount of time to remember how to print formatted strings as it did to finalise the script.

But, I ran it, and it worked, so I figured I would share it on my journal as evidence that I still do some scripting occasionally. I even formatted it with CSS to be easier to read... This also took at least an hour to do...



##########################
### SECRET SANTA ###
### Bradism.com 2016 ###
##########################
# Set Values Below:
SMTP_DETAILS = { # Available from your Webhost or ISP
'SMTPserver': 'your.smtp.com',
'sender': 'YOUR_EMAIL_ADDRESS',
'username': 'SMTP_USER_NAME',
'password': 'YOUR_SMTP_PASSWORD'
}

# Create a dictionary of participants, adding their name, email address and a description of what they might want/not want.
# The "gift" field will be included as HTML, so all HTML tags are valid and make sure to escape double-quotes
giftees = {}
giftees['Brad'] = {'email': 'brad@example.com', 'gift': "Yogurt, Cereal, Fruit.<br />No cats!"}
giftees['Dwight'] = {'email': 'dwight@atlanta.com', 'gift': "I'd like to shoot more threes and have someone yell \"Dwight for THREEEE\" even if it looks like missing."}
giftees['Rajon'] = {'email': 'big_game_rondo@gmail.com', 'gift': "<ol><li>Connect 4 Opponents</li><li>A call from Paul Pierce</li></ol>"}
giftees['Gortat'] = {'email': 'marcin@hotmail.com.com', 'gift': "I miss Stan Van Gundy. Also, shaving products."}

#######################################################
### Custom Settings Finished ###
### Shouldn't need to edit anything below this line ###
#######################################################

# Mail Subroutine
def send_mail ( destination_addr, addressee_name, target_name, target_requests, SMTP_DETAILS ):
import sys
import os
import re

sender = SMTP_DETAILS['sender']
destination = [ destination_addr ]

subject="Hi %s - Here's Your Secret Santa" % addressee_name
content="<p>Hi %s</p><p>Your Secret Santa is <b>%s!</b></p><p>Their Likes/Requests are:</p><p>%s</p><p>Good Luck and remember, keep it a secret!</p><p>Love, Santa</p>" % (addressee_name, target_name, target_requests)

from smtplib import SMTP_SSL as SMTP # this invokes the secure SMTP protocol (port 465, uses SSL)
# old version
from email.mime.text import MIMEText

try:
msg = MIMEText(content, 'html')
msg['Subject']= subject
msg['From'] = sender # some SMTP servers will do this automatically, not all

conn = SMTP(SMTP_DETAILS['SMTPserver'])
conn.set_debuglevel(False)
conn.login( SMTP_DETAILS['username'], SMTP_DETAILS['password'] )
try:
conn.sendmail(sender, destination_addr, msg.as_string())
finally:
conn.quit()

except Exception, exc:
sys.exit( "mail failed; %s" % str(exc) ) # give a error message

# Recursive People Shuffler
def shuffle_people ( inarray, people ):
import random
random.shuffle(inarray)

valid = 1
for i in range(0, len(inarray)):
if inarray[i] == people[i]:
valid = 0

if valid == 1:
return inarray
else:
return shuffle_people(inarray, people)

# MAIN
import copy
print "Running Secret Santa"
people = giftees.keys()

pre_shuffled_people = copy.copy(people)
shuffled_people = shuffle_people(pre_shuffled_people, people)

for i in range(0, len(people)):
name = people[i]
target_name = shuffled_people[i]
email = giftees[name]['email']
target_request = giftees[target_name]['gift']

send_mail( email, name, target_name, target_request, SMTP_DETAILS)

print "Secret Santa Complete"

Comments

Add Comment
Toggle Comments Form
Promoted Entry: Five Lessons from Speculate 18

Too much for me summarise in a review. Instead, I thought I'd share a single takeaway and challenge from each of the sessions.

Promoted Entry: The Hidden Life of Trees

I love trees. They're tall and stoic, so I relate to them. I feel a sense of serenity and belonging when walking beneath an ancient forest canopy and that is not just because most ancient forest canopies I've walked under have been adjacent to a thriving craft beer industry


Enjoy what you've read? Want to receive updates and publishing news in your inbox? Sign up to the bradism mailing list. You'll also receive an ebook, free!