Last active
December 31, 2015 14:09
-
-
Save Widdershin/7998488 to your computer and use it in GitHub Desktop.
The code to generate matches for /r/longboarding's gift exchange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import fuckitdb | |
import os | |
import random | |
email = os.environ["FUCKITDB_EMAIL"] | |
password = os.environ["FUCKITDB_PASSWORD"] | |
database = fuckitdb.Database("SecretSanta", email, password) | |
MESSAGE_TEMPLATE = \ | |
"""Hi /u/{gifter.username}! | |
Your Reddit Secret Santa match is ready! | |
Your match is /u/{giftee.username}. | |
Here are your match's details: | |
{giftee.name} | |
{giftee.address} | |
{giftee.city}, {giftee.state} | |
{giftee.country} {giftee.zip_code} | |
Here's what your match said when we asked what they'd like to receive: | |
> {giftee.shipping} | |
And here's what your match said about the type of skating they do: | |
> {giftee.skating} | |
Good luck! Remember, you need to ship your gift by the 31st of December. | |
Keep an eye out for the Gift Posts Megathread in mid January! | |
""" | |
@fuckitdb.register(database) | |
class User(fuckitdb.Model): | |
def __init__(self, timestamp, username, name, address, | |
city, state, country, zip_code, gear, shipping, | |
skating, rematch, international, id=None): | |
super(User, self).__init__(id) | |
self.username = self.field("username", username.strip()) | |
self.name = self.field("name", name) | |
self.address = self.field("address", address) | |
self.city = self.field("city", city) | |
self.state = self.field("state", state) | |
self.country = self.field("country", country.strip()) | |
self.zip_code = self.field("zip_code", zip_code) | |
self.shipping = self.field("shipping", shipping) | |
self.gear = self.field("gear", gear) | |
self.skating = self.field("skating", skating) | |
self.rematch = self.field("rematch", rematch == "Yes") | |
self.international = self.field("international", | |
international == "Yes") | |
def pick_viable_giftee(self, users): | |
viable_giftees = [] | |
for user in users: | |
if not (not self.international and self.country.lower() != | |
user.country.lower() or user == self): | |
viable_giftees.append(user) | |
if viable_giftees: | |
return random.choice(viable_giftees) | |
return None | |
@fuckitdb.register(database) | |
class Match(fuckitdb.Model): | |
def __init__(self, gifter, giftee, id=None): | |
super(Match, self).__init__(id) | |
self.gifter = self.field("gifter", gifter) | |
self.giftee = self.field("giftee", giftee) | |
def main(): | |
print("Getting users") | |
users = User.get_instances() | |
print("Shuffling users") | |
random.shuffle(users) | |
print("Generating matches") | |
giftee_pool = users[:] | |
for user in sorted(users, key=lambda x: x.international): | |
gifter = user | |
giftee = user.pick_viable_giftee(giftee_pool) | |
if giftee: | |
giftee_pool.remove(giftee) | |
match = Match(gifter.username, giftee.username) | |
match.commit() | |
print("Matched {} to {}".format(match.gifter, match.giftee)) | |
else: | |
print("Couldn't find a match for {}".format(user.username)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment