repl.it
@EdgarOnghena/

SecretSanta

Python

Give a list of people with their email adresses, and they will be automatically emailed the name of the person they're the Santa of

fork
loading
Files
  • main.py
  • mail.py
  • people.py
main.py
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
import json
import random
from people import people
from mail import send_mail, close_mail
from base64 import b64encode


def choose(people: list) -> dict:
    """Returns a dict that maps a santa to his 'child'"""
    pairs = {}
    random.shuffle(people)

    assert len(people) > 0

    for i in range(-1, len(people) - 1):
        pairs[people[i]] = people[i + 1]

    return pairs

names = list(people.keys())
combinations = choose(names)

# Print base64-encoded JSON of the combinations, so you can save them in case something goes wrong, and not be spoiled in the mean time
print(b64encode(json.dumps(combinations).encode()))

# Send a mail for each combination
for santa, child in combinations.items():
    email = people[santa]
    send_mail(santa, child, email)
    print('.', end='')

print()
print("{} emails sent.".format(close_mail()))
Fetching token
?