« | » Main « | »

How to raffle a book

Wednesday 25 September 2013

I'm an organizer for Boston Python, and we like to raffle off books at events. I've got a technique I like for doing it. Here it is recorded for posterity.

We use meetup.com to handle RSVPs for events. I have a small program that queries the meetup.com API for people who have RSVP'd "yes" to an event. I use it to get a list of names, then I shuffle the names, make an iterator, then pick names off it. I do this on the big screen in an interactive prompt:

>>> import random
>>> from attendees import yes
>>> people = yes(120746442)     # The event number from meetup.com
>>> len(people)
>>> random.shuffle(people)      # Give them a good shake
>>> pick = iter(people)         # Make an iterator
>>> print next(pick)            # Now pick people!
Ned Batchelder
>>> print next(pick)
Jessica McKellar
>>> print next(pick)
Jason Michalski
>>> # etc...

Iterables like the list of people's names are usually iterated completely with a for loop or a list comprehension. But you can step them one item at a time if you like. iter() creates an iterator from an iterable (confusing, I know...), then you can use next() to get the next item from the iterable.

There aren't many reasons to use iterators like this, but occasionally it can be useful.

« | » Main « | »