[ create a new paste ] login | about

Link: http://codepad.org/8n3xxgOC    [ raw code | output | fork ]

TheDicktator - Python, pasted on May 24:
import smtplib
import random
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

##
## FRONT END FUNCTIONS
##
def getEmail():
    username = raw_input('Enter gmail ID: ')
    username = username.split('@', 1)[0]
    username = username+'@gmail.com'
    password = raw_input('Enter password: ')
    return [username, password]

def getPeople():
    listPeople = {}
    counter = 0
    totalPeople = int(input('How many people? '))
    while counter < totalPeople:
        newName = raw_input('Enter person\'s name: ')
        while newName in listPeople:
            newName = raw_input('Name already taken. Enter a new name: ')
        newEmail = raw_input('Enter ' + newName + '\'s e-mail ID: ')
        listPeople[newName] = newEmail
        counter = counter + 1
    return listPeople

##
## BACK END FUNCTIONS
##

def assignSanta(peopleList):
    santaDict = {}
    notAssigned = peopleList.keys()
    notSanta = peopleList.keys()
    while len(notAssigned) > 0:
        if len(notSanta) == 1 and len(notAssigned) == 1:
            santaDict[notSanta[0]] = notAssigned[0]
            return santaDict
        else:
            positionSanta = random.randint(0, (len(notSanta)-1))
            santaName = notSanta[positionSanta]
            notAssignedTemp = notAssigned[:]
            if santaName in notAssignedTemp:
                notAssignedTemp.remove(santaName)
            positionKid = random.randint(0, (len(notAssignedTemp)-1))
            kidName = notAssignedTemp[positionKid]
            santaDict[santaName] = kidName
            del notSanta[notSanta.index(santaName)]
            del notAssigned[notAssigned.index(kidName)]

def testTable(santaTable):
    testOutcome = True
    for element in santaTable:
        if element == santaTable[element]:
            testOutcome = False
            break
    return testOutcome
    

def emailSantaNames(peopleList, santaTable, username, password):
    server = smtplib.SMTP('smtp.gmail.com:587')
    server.starttls()
    server.login(username, password)
    for element in santaTable:
        msg = MIMEMultipart()
        msg['From'] = username
        msg['Subject'] = 'Secret Santa!'
        msg['To'] = peopleList[element]
        body = element + ', you have to buy a gift for ' + santaTable[element]
        msg.attach(MIMEText(body))
        string = msg.as_string()
        server.sendmail(username, peopleList[element], string)
        del msg
    server.quit()
    return None

##
## IMPLEMENTATION
##

identification = getEmail()
peopleList = getPeople()
santaTable = assignSanta(peopleList)
assert testTable(santaTable), 'Improper assignment'
emailSantaNames(peopleList, santaTable, identification[0], identification[1])

raw_input('Email sent')


Output:
1
2
3
4
5
6
Enter gmail ID: Traceback (most recent call last):
  Line 83, in <module>
    identification = getEmail()
  Line 10, in getEmail
    username = raw_input('Enter gmail ID: ')
EOFError


Create a new paste based on this one


Comments: