Play Our Online Football Game and Help Raise Money for Charities

Posted in Python, The Real World by Dan on August 8th, 2014

If you’re not in the UK or you’re not interested in the Beautiful Game and helping charities then this post isn’t for you. I’ve generally tried to keep the articles on this site at least tangentially related to software development but on this occasion the link is a bit more tenuous than usual.

Super10 promo image

I’d like to ask you to consider playing our online charity football game, Super10. You can win a share of several hundred pounds in cash prizes and, more importantly, you can help to raise money for charities. The charity aspect is two-pronged. Firstly you can win money for one of the four organisations that the game is supporting this season (Demelza Hospice Care for Children, Kent, Surrey and Sussex Air Ambulance, Pilgrims Hospices, and homelessness charity Porchlight) and secondly all additional proceeds go to Donations With A Difference (DWAD), the newly-registered charity, of which I am now a trustee, that is running the competition. DWAD will use this money to make grants to individuals and organisations to support the improvement of physical health, mental health and education in the UK.

Super10 is a little bit like fantasy football except that instead of picking players, you pick ten clubs – seven from the English game and three from the top European leagues. These teams score points for you throughout the season. Compared to traditional fantasy football it’s a low-maintenance game as there are no substitutions to deal with each week and transfers only occur during the January transfer window. You can make your selections and mostly forget about them. That said, it does have a way of making you care deeply about the results of teams you previously had no attachment to. Never been to Peterborough? Doesn’t matter, you’ll still feel the despair as they concede a late equaliser to Port Vale.

So what’s the software angle? Well Super10 has been around for a few seasons, certainly much longer than I’ve been involved, but this year we’re trying to take it beyond its previously limited scope. That has meant getting it fully online including being able to take payments online so that we can extend the game to a much wider audience and therefore raise more money. To enable this I’ve built the responsive Super10 website using Python, Django and Bootstrap. Much of the website was in use for most of last season but now we’ve extended it to open up Super10 to the whole country for the first time.

We’re restricting Super10 to the UK only for now because we are not familiar enough with the laws governing this kind of competition in other countries. The deadline for entries is noon (BST) on Saturday 16th August. Give it a go and get your friends to play too.



Django ModelChoiceField and HTML <optgroup>

Posted in Python by Dan on December 21st, 2013

I’ve been dabbling with Django over the last couple of months. I’ve played around with it a couple of times previously but this time I’ve actually built something reasonably substantial, which has meant that I’ve had to delve a bit deeper. One of the minor problems I solved today was how to group items in the HTML <select> element generated by a form’s ModelChoiceField. HTML has the <optgroup> tag for this purpose.

It’s not immediately obvious how you can get ModelChoiceField to use optgroups without over-riding the render method and re-implementing the HTML generation yourself, or bypassing ModelChoiceField completely and building something based on the basic ChoiceField (which does support optgroups). The only potential solutions I found from searching took the former approach (here and here). The reason I’m writing this post is because I think I’ve found a better, more concise solution that might be of use to future searchers.

ChoiceField accepts a choices parameter to its constructor. In the simple case this is just a list of items (each item is value/label pair). However, it can also accept a list of groups where each group is a tuple consisting of the group label and a list of items. The problem is that ModelChoiceField is different in that it has a queryset parameter instead, so there is no way to pass in the group information.

However, a comment in the source code says that we can set a property called choices after constructing the ModelChoiceField instance and the queryset will be ignored. The HTML <select> will instead be populated from this data structure with <optgroup> elements as required.

Assuming we want to group items by a field on the model, we can build the list of tuples from the queryset by sub-classing ModelChoiceField and over-riding the constructor. In this example I’m assuming that the field is a list of countries grouped by continent, where the continent is just a text field on the country model.

from django.forms import ModelChoiceField
from itertools import groupby
from operator import attrgetter
class CountryChoiceField(ModelChoiceField):
    def __init__(self, *args, **kwargs):
        super(CountryChoiceField, self).__init__(*args, **kwargs)
        groups = groupby(kwargs['queryset'], attrgetter('continent'))
        self.choices = [(continent, [(, self.label_from_instance(c)) for c in countries])
                        for continent, countries in groups]

In order for this to work, the queryset must be sorted by the field that it is to be grouped by so that items in the same group are adjacent. You can do this before you pass it to the constructor or you can change the code above to call order_by on the queryset.

Book Review: Programming Collective Intelligence

Posted in Evolutionary Computation, Python, Software Development by Dan on December 13th, 2007

It’s called “Programming Collective Intelligence” and is presented as a book for building “Smart Web 2.0 Applications” but it is essentially an extremely accessible explanation of a wide array of machine learning and data-mining algorithms. How do sites like Amazon and Last.FM make recommendations? How do search engines work? How does Google News manage to categorise and present the most important news articles without human intervention? How do you build a useful spam filter?

All of these questions are answered and compelling example applications are built step-by-step to demonstrate the power of the ideas presented here. Decision trees, genetic algorithms, neural networks, support vector machines, genetic programming, Bayesian classifiers and non-negative matrix factorisation are some of the techniques covered and all without the dry, maths-heavy text that normally fills books on these topics.

The examples throughout are exclusively in Python, which may have put me off had I realised this when I ordered it. I have nothing against Python except for my complete lack of experience with it. However, the examples are easy enough to understand for anybody familiar with other high-level languages. As result of reading the book, I may actually try my hand at a bit of Python hacking now.

How well do these techniques work? Well I’d never have found out about this book but for Amazon’s automated recommendations system. I’d thoroughly recommend this book to anyone looking to learn about interesting AI techniques without wading through opaque academic papers.

(If you find the genetic algorithms and genetic programming topics interesting, check out the Watchmaker Framework for Evolutionary Computation and some of the books recommended there.)