Valentine’s day special!

This is my Valentine’s gift to you: I took it upon myself to streamline the organisation of a ‘Secret Valentine’ event in the building where I live, and wrote this code to take care of the tedious tasks of pairing the participants in an appropriate way, as well as sending emails to inform them of their assigned platonic partner for the day. 

To make sure I would get something out of it myself as well, I forced myself to write it in Python and get some experience with this very hip and happening language all the cool kids seem to be using, endorsed by among others Randall Munroe (xkcd): 

XKCD: Python

Conversely, if you type

>>> import antigravity

into a Python command line, it opens the above comic in your default browser

There’s also this comic (sadly ‘import soul’ doesn’t natively do anything in Python… yet). 

Also related to xkcd, there is a Python library to access and retrieve links to comics from xkcd.com.

Back to the Secret Valentine/Secret Santa problem. 
To be precise, I have used Python before but never beyond introductory exercises, Software Carpentry workshops, maintaining and debugging scripts from The Open Multitrack Testbed (an online multitrack audio repository with semantic database that I recently launched), modelling the navigation system for a robot (modded Roomba) I helped develop as an undergraduate - I’m told one of them is known as ‘BDM’ and still happily driving around in IBCN’s wireless lab at UGent

Valentine preparations

Preparations for the Secret Valentine event by my partner

While there is some excellent work on elegant solutions to this problem, many rooted in graph theory, I’ve chosen the lazy route - also known as efficient in terms of coding effort if slightly inefficient from a computation point of view. After all it is a weekend project, a solution for a fairly mundane task, that I already spent way too much time on (mainly Googling Python syntax). But as it hurts the proud coder in me, I may update this at some point. 
For some background reading, see this and this discussion and this very mathematical paper

So what I do here is to generate a random ‘index vector’ of integers, where if the first position’s value is X, person 1 has to find a gift for person X. Then I check for two more or less obvious conditions:

1. The value at position X cannot be X; otherwise person X would have to find a present for himself. 
2. The value at position X cannot be Y, if the value at position Y is X; otherwise person X receives a present from the person he also has to give a present to, i.e. person Y. 

The latter is purely a matter of preference, and there could be situations where you wouldn’t want this condition implemented. 

A third condition was added due to the nature of the event, where if couples or families would take part, it would be dull if one would have to find a present for a family member - this would likely happen outside of this event already and not really add to the Secret Santa/Valentine experience in this case. 

3. The value at position X cannot be Y, if person X and Y share the same room numbers. 

To accommodate this, the signup sheet (using Google Forms) included a room number field as well. 

If a randomly generated vector did not meet any of these requirements for any element in the vector, a new random vector was generated. This usually only took a couple of attempts (depending on the length of the list and the number of participants sharing a room).

Intermezzo: an xkcd comic about Valentine’s day:

XKCD: Valentine Dilemma

I created a different Python ‘module’ for pairing and sending out emails, as I wanted to send the organisers of the event a proposal of ‘pairs’ of people, before sending emails to everyone (sending more than one email was not an option). 

That’s it! 
See my GitHub page for the code repository

Overal experience:
I like the language, it’s elegant and intuitive, and overall makes a lot of sense. It’s not too stringent on syntax, meaning there are often many different ways of writing something (the strings which can be ‘string’ or “string” is but one example of this). 
It can be a definite drawback that the code is not checked as much as e.g. C++, i.e. debugging only happens at runtime. 
I have found XCode to be a dreadful editor for Python, even if I like it for C++. I will have to look into other editors (I know there are more than enough out there). 

It seems many are still on version 2.7, even if Python 3 is available and many libraries have been ported to this version - see the above conversation on Twitter. 

On another note, I have found that documenting my work with a blog post such as this one really improves the thinking process, so may do it more often. I doubt it will be of help to someone, but if anything it has been helpful for me. 

Finally, I haven’t been able to find a nice alliterating name for a Valentine’s Secret Santa (Veiled Valentine?). All suggestions are welcome. 

Valentine event

Secret Valentine event

More xkcd comics on Valentine: 63, 223, 701
Python best practice: PEP 8, Python docs, How Not To Write Python Code, Code Like A Pythonista