Cantor never bores (1)

Given a set of countable sets K, such that K is totally ordered by inclusion, videlicet for every A,B\in K either A\subseteq B or A\supseteq B. Intuitively, for at every step in this chain one element at least must be added, one expects the set K to be countable as well.

Suppose K is countable. Then the union, \bigcup K is a countable union of countable sets, hence countable. (Suppose k: \mathbb N \to K is an enumeration of K and f_i: \mathbb N \to k(i) enumerations of the elements of the chain. Then f_0(0), f_1(0), f_0(1), f_2(0), f_1(1), f_0(2), \ldots enumerates \bigcup K.)

Thus \bigcup K is an upper bound of K. In the poset of countable subsets of some set U, of which \bigcup K is a subset, every non-empty chain has an upper bound. Hence, using Zorn’s lemma there is a maximal element, say M.

Suppose U is uncountable, then there exists a \star \in U \backslash M. M \cup \{\star\} is most definitely also countable and M \subset M \cup \{\star\} which contradicts M’s maximality. We are forced to conclude that there exists an uncountable chain of countable sets.

Cantor’s set theory keeps surprising.

Update: an example of such a chain is the set of the countable ordinals.

Another update: a “more concrete” example are the downsets in \mathbb Q without the empty set and \mathbb Q itself. These downsets correspond to real numbers, see Dedekind Cuts.

PijsMarietje

At the faculty for sciences there are canteens for students. In each of these, there’s sound equipment connected to linux boxes. On each of those linux boxes, we run a music-request-server called Marietje. I just finished writing a front-end in Javascript. It wasn’t a pain. As instead, the use of jQuery was a bliss.

The frontend for one of those boxes and the source code (see the ajax folder).

Damned DOM (1)

When I wanted to react to any changes to a input textbox immediately, my first instrinct was to use onChange. onChange, however, is called when the input loses focus. onKeyPress then? Isn’t called on backspaces. onKeyDown, maybe? It does get called, but the effect of the keystroke isn’t yet applied, for the return value determines whether it that is done in the first place. (Same story for onKeyPress by the way.) onKeyUp does work a bit, except if someone is holding down a single key, for a while.

The solution: hook onKeyUp and use setTimeout with a timeout of 0. Yugh. I hate DOM.

Big Fat Disclaimer: I actually tested this only on one browser.

Unicode to ASCII (1)

When I want to generate usernames from real names, which can contain non-ascii characters, you can’t simply ignore the unicode characters. For instance, danielle@blaat.org is the right e-mail address for DaniŽlle, danille@blaat.org isn’t.

There’s trick. Unicode has got a single code for Ž itself, but it has also got a code which (simplified) adds ® on top of the previous character. The unicode standard defines a normal form in which (at least) all such characters, which can be, are represented using such modifiers. If you then simply ignore the non-ascii representable codes, you’ll get the desired result.

In python: unicodedata.normalize('NFKD', txt).encode('ASCII', 'ignore').

However, this isn’t the right solution. For instance, in german, one prefers ue as a replacement of Ł over u.

Django annoyances: no reverse select_related

Consider

for page in Page.objects.all():
  print page.title
  for comment in page.comments.all():
    print comment
.

There will be a single query to fetch all pages, but there will be for every page another query to fetch its comments. Luckily, Django has got a nice trick up its sleave: select_related. Would I use instead of Page.objects.all(), Page.objects.select_related('comments').all() then Django will use a single joined query to prefetch comments for each page.

However, Django’s select_related only supports forward one-to-many references. No many-to-many; certainly no reverce many-to-many; no reverse one-to-many and no, not even reverse one-to-one (yet). A developer claims it’s impossible (which is bullshit), another asks for patches, which means he doesn’t care doing it himself.

It’s quite easy to manually code around the missing reverse select_related, but it takes too many ugly lines compared to the single word it could’ve been.

On-demand singleton for Python

Some singletons eat slightly more resources, than you want to give them for free. For instance, if you have a home-brew threadpool singleton, you don’t want it to create its threads if you are not going to use it. The solution: a simple function that creates a stub which proxies attribute access to an ad-hoc created instance.

Usage: create_ondemand_singleton('mylibrary.Threadpool', MyThreadPoolClass).

iPhone WiFi weirdness

I’m having trouble with the wireless on my 3G iPhone. Either an action works perfectly (for instance downloading e-mail or visiting a website), or it will just time-out. I just jailbroke it and installed sshd. ssh-ing to my it via 3G miraculously works perfectly. However, when connecting to it via WiFi it’ll simply refuse data. That is, until you let the iPhone try to get data itself by for instance visiting a website. Then suddenly the ssh packets come through again. In the background (with screen) I’m running a very frequent ping to my local gateway which makes ssh work perfectly again via WiFi. A very unsatisfying hack.

It seems like an aggressive power safe on the WiFi.