Skip to main content

A package that implements the Secret Santa algorithm and notifies participants by email of whom they have to gift.

Project description

Secret Santa

Python implementation of a Secret Santa generator that notifies participants by email of whom they have to gift. Allows for defining restrictions between participants to prevent somebody from gifting specific people.

Enjoy it and have fun 😁 I'll be happy to know if you use it, so don't hesitate to contact me!

In this page

Features

This package provides the following functionalities:

  • 100% free Secret Santa generator: generate with very few lines of code the pairings for your yearly family Secret Santa! You only need to specify the participants.
  • Specify avoid rules: easily specify which participants should avoid whom and let the algorithm work its magic! If you provide too many rules and the scenario cannot be solved, you will be rapidly alerted.
  • Communication via email: communicate each participant by email whom they have to gift. You can send the emails using any account that you own.
  • Email body customisation: provide your own email template for the automatically generated emails.
  • Dry run mode: test your rules in a safe environment that prevents emails from being sent.

Installation instructions

Install it using pip

This software has been packaged and uploaded to PyPi, the Python Package Index. To install it, simply run the following command:

pip install secret-santa-bpguasch

You can now review the usage instructions.

Use it in an AWS Lambda function

AWS Lambda is a serverless compute service that lets you run code without provisioning or managing servers. This Python package is provided as well as an AWS Lambda Layer for you to use in your functions. Follow these steps to add the Secret Santa Lambda Layer to your AWS Lambda function:

  1. From this repository, download the python.zip file
  2. Navigate to the AWS Lambda section in the AWS Console, and select Layers under Additional resources
  3. Select Create layer, give it a name and upload the file you recently downloaded
  4. Open the Lambda function in which you would like to use the layer
  5. Scroll to the bottom of the page and select Add a layer
  6. Select Custom layers and choose the layer you just created

Usage instructions

In your Python script, start by importing the package:

from secret_santa_bpguasch import *

The application will email each participant whom they have to gift. For this reason, you have to provide some configuration parameters to send emails using one of your accounts:

email_config = EmailServer(
    host="smtp.gmail.com",
    port=465,
    username='',
    password=''
)

Most likely you will need to create a dedicated App password to log into your email account programmatically. Taking Gmail as an example, you can learn more about it here. As the username field, you should specify your email address.

Next, define some configuration parameters for the Secret Santa Generator:

game_config = Game(
    name="Smith Secret Santa",
    budget=30,
    subject="Smith's family Secret Santa 2022"
)

The game name will appear as the email sender identity (name). The subject represents the email subject and the budget value is used to compose the default email body. By default, each participant will receive and email with the following body:

¡Hello, giver_name! You are receiver_name's Secret Santa this year. Remember that the maximum budget is $budget. Happy shopping and merry Christmas :)

Optionally, you can define in the game configuration a method that generates the email body for a pairing of giver-receiver. Said method must receive a list as an argument and return a string. You can use this feature to customise the email as you want with HTML. To provide your generator method, you can do the following:

def my_custom_email_body_generator(pairing: list) -> str:
    return 'The giver is {} and the receiver is {}'.format(pairing[0], pairing[1])

Then, you can include your method in the configuration as follows:

game_config = Game(
    name="Smith Secret Santa",
    budget=30,
    subject="Smith's family Secret Santa 2022",
    body_generator_func=my_custom_email_body_generator
)

By doing this, your method will be automatically invoked whenever an email has to be sent to the giver.

Next, you need to supply the participants in the form of a dictionary:

participants = {
    'borja': {
        "email": "borja@secretsanta.com",
        "avoidGiftingTo": ["mark"]
    },
    'john': {
        "email": "john@secretsanta.com",
        "avoidGiftingTo": []
    },
    'mark': {
        "email": "mark@secretsanta.com",
        "avoidGiftingTo": []
    }
}

Finally, you can create an instance of SecretSanta to generate the pairings:

secret_santa = SecretSanta(
    game_config,
    email_config,
    participants
)

# Call the method dry run to run tests and verify your configuration. 
# When you generate pairings in dry run mode, no emails are sent.
secret_santa.dry_run()

# When you are confident about the configuration, you can call the play method
#secret_santa.play()

Full working example

The whole script together would look as follows:

from secret_santa_bpguasch import *


def my_custom_email_body_generator(pairing: list) -> str:
    return 'The giver is {} and the receiver is {}'.format(pairing[0], pairing[1])


if __name__ == '__main__':
    email_config = EmailServer(
        host="smtp.gmail.com",
        port=465,
        username='',
        password=''
    )

    game_config = Game(
        name="Smith Secret Santa",
        budget=30,
        subject="Smith's family Secret Santa 2022",
        body_generator_func=my_custom_email_body_generator
    )   

    participants = {
        'borja': {
            "email": "borja@secretsanta.com",
            "avoidGiftingTo": ["mark"]
        },
        'john': {
            "email": "john@secretsanta.com",
            "avoidGiftingTo": []
        },
        'mark': {
            "email": "mark@secretsanta.com",
            "avoidGiftingTo": []
        }
    }

    secret_santa = SecretSanta(
        game_config,
        email_config,
        participants
    )

    secret_santa.dry_run()

Documentation

The secret-santa-bpguasch Python package is organised in different modules. An overview of the classes contained in each module that can be worked with is provided below. Check the docstring for a detailed description of each method parameter:

secret_santa_bpguasch.config

A module with classes that encapsulate configuration attributes.

Click to show package contents

InvalidConfigurationException

Exception subclass that represents an invalid configuration, either because the data is wrong formatted or because the scenario cannot be solved given the specified restrictions.

EmailServer

Class that encapsulates email server configuration parameters

Constructor
EmailServer(host: str, port: int, username: str, password: str)

Parameters:

Name Type Description
host str Email server host
port int Email server port
username str Email server username
password str Email server password

Game

Class that encapsulates game configuration parameters

Constructor
Game(name: str, budget: float, subject: str, body_generator_func=None)

Parameters:

Name Type Description
name str Game name. Will appear as the sender identity (name)
budget float Budget for the present. Will appear in the default email body
subject str Subject of the email that each participant receives
body_generator_func descriptor Descriptor of a method used to generate a custom email body for a given pairing

secret_santa_bpguasch.algorithm

A module with the class that implements the Secret Santa algorithm.

Click to show package contents

SecretSanta

Class that implements the Secret Santa algorithm and participants notification

Constructor
SecretSanta(game_config: Game, email_config: EmailServer, participants: dict)

Parameters:

Name Type Description
game_config Game Game configuration attributes
email_config EmailServer Email server configuration attributes
participants dict Game participants

The constructor will raise an InvalidConfigurationException exception if the validation of the participants argument fails. The validation will fail if the structure does not have the expected fields and field types or if the scenario cannot be solved due to the specified restrictions.

Methods
Signature Description Return Throws
dry_run() Creates giver-receiver pairings applying the specified participant restrictions. Doesn't send any email communication. Use this method to test your scenario configuration. The first element of the returned value represents the giver and the second the receiver. list -
play() Creates giver-receiver pairings applying the specified participant restrictions. Sends individual emails to participants to let them know whom they have to gift. The first element of the returned value represents the giver and the second the receiver. list -

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

secret_santa_bpguasch-1.0.0.tar.gz (11.7 kB view details)

Uploaded Source

Built Distribution

secret_santa_bpguasch-1.0.0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file secret_santa_bpguasch-1.0.0.tar.gz.

File metadata

  • Download URL: secret_santa_bpguasch-1.0.0.tar.gz
  • Upload date:
  • Size: 11.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.1

File hashes

Hashes for secret_santa_bpguasch-1.0.0.tar.gz
Algorithm Hash digest
SHA256 8433e0a4bbc1fe5e93ec234fd3c92f1280bfd4b529740728f512b55b9c266bfb
MD5 df67ec9d256c463c24b217b34338f0cd
BLAKE2b-256 826723238032021c55d889b6493fe2e26aa6dce6e2092db92aa46a56e5fb6d1a

See more details on using hashes here.

File details

Details for the file secret_santa_bpguasch-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for secret_santa_bpguasch-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4c0c1103a238cdaa61cb77a7c822d3124b411c25e90ff07285bcae8d3564a19a
MD5 ca44013196bde640d77dd514231f207f
BLAKE2b-256 9f8aa60b6e562711a03a16bc8932eb121542bd5702ffcd59039591dae6adacd1

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page