commit 6aa81c2734b48a08ad811205c1adb720d00f53e4 Author: Lukas Winkler Date: Fri May 8 14:20:41 2020 +0200 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..8005a11 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +config.py +.idea/ +__pycache__/ +*.pyc diff --git a/config.sample.py b/config.sample.py new file mode 100644 index 0000000..6f21110 --- /dev/null +++ b/config.sample.py @@ -0,0 +1,7 @@ +people = { + "Test1": "test1@example.com", + "Test2": "test2@example.com", + "Test3": "test3@example.com" +} + +sender = "Randomizer " diff --git a/derangement.py b/derangement.py new file mode 100644 index 0000000..013e460 --- /dev/null +++ b/derangement.py @@ -0,0 +1,25 @@ +from random import shuffle +from typing import List + + +def is_derangement(original: List, shuffled: List) -> bool: + return not any([x == y for (x, y) in zip(original, shuffled)]) + + +def get_derangement(people: List) -> List: + while True: + shuffled = people[:] # make a copy + shuffle(shuffled) + if is_derangement(people, shuffled): + return shuffled + + +def test(): + a = [1, 2, 3, 4] + assert is_derangement(a, [2, 1, 4, 3]) + assert not is_derangement(a, [1, 2, 4, 3]) + assert not is_derangement(a, [1, 3, 4, 2]) + assert is_derangement(a, get_derangement(a)) + + +test() diff --git a/mail.py b/mail.py new file mode 100644 index 0000000..4a184d2 --- /dev/null +++ b/mail.py @@ -0,0 +1,17 @@ +import smtplib +from email.mime.text import MIMEText + +from config import sender + +s = smtplib.SMTP('localhost') + + +def notify(name: str, email: str, chosen_person: str) -> None: + to = f"{name} <{email}>" + + msg = MIMEText(f"Hello {name},\n\nThe chosen person is {chosen_person}.") + msg['Subject'] = f"Result of shuffle for {name}" + msg['From'] = sender + msg['To'] = to + print(msg.as_string()) + s.sendmail(sender, [to], msg.as_string()) diff --git a/main.py b/main.py new file mode 100644 index 0000000..7d56da8 --- /dev/null +++ b/main.py @@ -0,0 +1,14 @@ +from config import people +from derangement import get_derangement +from mail import notify + +names = list(people.keys()) + +shuffled = get_derangement(names) +print(shuffled) + +i = 0 +for name, email in people.items(): + chosen_person = shuffled[i] + notify(name, email, chosen_person) + i += 1