Bleach: a new Python library

James Socol has released Bleach, a new Python library. As he says:

We’ve released another Python library: Bleach, for sanitizing HTML and automatically linking URLs in text, that we’re using on addons.mozilla.org and support.mozilla.com. It’s based on html5lib, which we chose because of its foundation in web standards and its active development community. Bleach is available on Github and PyPI.

Bleach uses a tree-walking approach to automatically link URLs that I think is pretty interesting. I wrote a short post outlining the method.

You can find more info at http://blog.mozilla.com/webdev/2010/02/25/new-python-library-bleach/.

Django book

Just finished to download the online version of Django book, the most famous book on this great Python web framework. Django makes some common tasks trivial. For example, here's how this framework handles browser detection:

# GOOD (VERSION 1)
def ua_display_good1(request):
    try:
        ua = request.META['HTTP_USER_AGENT']
    except KeyError:
        ua = 'unknown'
    return HttpResponse("Your browser is %s" % ua)

# GOOD (VERSION 2)
def ua_display_good2(request):
    ua = request.META.get('HTTP_USER_AGENT', 'unknown')
    return HttpResponse("Your browser is %s" % ua)

Since Python is an object-oriented language, you can use this feature to access methods and properties, while in other languages you have to deal with superglobal arrays (just as PHP does). Using objects, on the other hand, is a more secure approach. For example, to sanitize user input, you need only one line of code in Python and Django! That's why I love Python and Django.

Thinking Hybrid - Python/C++ Integration

The author says:

That’s exactly the thing that makes C/C++ so different from most other languages. It distinguishes between reference and value calls. Python does not know about these things. People have tried to resolve this problem by trying to guess which calling method is correct in a place. But as in the Zen of Python is already stated 'Explicit is better than implicit.' So we don’t want the system to guess as things can go terribly wrong. Therefore, so called 'calling policies' (mentioned on slide 32 in the flash animation here) have to be used to disambiguate in a way that the programmer wants.