The Daily WTF
Rachel is doing some Python/Django work on an application that, among other things, handles a pile of Internet addresses, a mix of IP addresses and domain names. Since each of those has a very different query path (domains could be filtered on the TLD or the name portion of the domain, for example), Rachel implemented some helper objects built on Django observables, the IPAddressObservable and the DomainObservable.
This way, when someone wanted to search the database for an address, some code like this could run:
OBSERVABLE_TYPES = {“ipaddress”: IPAddressObservable, “domain”, DomainObservable} # … snip … def search(self, query): q = Observable.objects.none() for klass in OBSERVABLE_TYPES.values(): q |= klass.get_search_query(query) return self.get_queryset().filter(q)
As you can see, this code was planned to be somewhat extensible. The actual observable types are stored in a dictionary. The search method starts with an empty query, and then for every observable in OBSERVABLE_TYPES, we get a
To read the full article click on the 'post' link at the top.