SHARE
TWEET

Untitled

a guest Jun 21st, 2015 218 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #/usr/bin/env python
  2. import smtplib
  3. import sys
  4.  
  5. from random import choice
  6.  
  7. # Pairings aren't recipricol (i.e. just because I've been assigned my mother's
  8. # stocking doesn't mean she was assigned me. To keep everything straight, we
  9. # need 2 lists.
  10. family = ["Sister", "Brother-in-Law", "Father", "Me", "Grandmother", "Mother", "Fiance"]
  11. not_chosen = ["Sister", "Brother-in-Law", "Father", "Me", "Grandmother", "Mother", "Fiance"]
  12.  
  13. # There's an outside chance that these are not real e-mail addresses
  14. emails = {
  15.         "Sister": "sister@email.com",
  16.         "Brother-in-Law": "brother-in-law@email.com",
  17.         "Father": "father@email.com",
  18.         "Me": "me@email.com",
  19.         "Grandmother": "grandmother@email.com",
  20.         "Mother": "mother@email.com",
  21.         "Fiance": "fiance@email.com"
  22.         }
  23. assignments = {}
  24.  
  25. # Go through each family member and give them a stocking assignment randomly.
  26. # The stocking assignment cannot be that family member.
  27. for member in family:
  28.  
  29.     stocking_assignment = member
  30.     ctr = 0
  31.  
  32.     # Keep going until you pick an assignment who isn't the family member in
  33.     # question.
  34.     while stocking_assignment == member:
  35.         stocking_assignment = choice(not_chosen)
  36.         ctr += 1
  37.  
  38.         # By this point the loop's run over 100 times. If we STILL haven't
  39.         # found an assignee that isn't the family member, we can safely
  40.         # assume that the current family member is the only possible
  41.         # assignment left - so let's break out of the script and try again.
  42.         if ctr > 100 and stocking_assignment == member:
  43.             print "INFINITE LOOP!!!! FAILZ!!!111"
  44.             sys.exit(1)
  45.  
  46.     # We have someone who isn't the family member!
  47.     assignments[member] = stocking_assignment
  48.     not_chosen.remove(stocking_assignment)
  49.  
  50. session = smtplib.SMTP("smtp.gmail.com:587")
  51. session.starttls()
  52.  
  53. credentials = {
  54.                   "username": "me",
  55.                   "password": "NotGonnaTellYou"
  56.               }
  57.  
  58. session.login(credentials["username"], credentials["password"])
  59. message = """
  60.         Subject: Stocking assignment
  61.  
  62.         Hi %s! The stocking drawing has occurred! You have been randomly
  63.         assigned:\n%s
  64.         """
  65. # Communicate these random assignments via e-mail.
  66. for person in emails:
  67.     address = emails[person]
  68.     session.sendmail(address, address, message %(person, assignments[person]))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top