Rasmus​.krats​.se

Denna sida på svenska

Posts tagged “python”

Reminiscing this and that, on the web since 1994.

Nicer URL mappings in Django

Posted 2013-05-11 17:15. Last updated 2018-01-05 14:00. Tagged , , .

URL mappings in django consists more or less of a list of (regex, handler) tuples, if the regular expression matches the requested URL, the handler is used to serve the request. The regular expressions themselfes tends to be rather long and not as readable as I would want them to. Let’s see if we can fix that.

Update 2018-01-05 14:00: Django 2.0 was recently released and supports a scheme simliar to what I present here (but more generic). If you use this, consider upgrading to Django 2.0 and see the documentation on the URL dispatcher.

Here’s a few lines of urls.py from the main app of my blog server, as an example:

Read whole Nicer URL mappings in Django.

Destruktorer och defer

Publicerad 2014-11-09 00:10. Taggat , , , , , .

En av de saker jag verkligen gillar i c++ är att man kan ha lokala variabler som är objekt i kombination med att objekt har destruktorer. När man lämnar ett scope där det finns lokala variabler kommer deras destruktorer omedelbart att köras, oavsett hur man lämnar scopet. Det ger ett trevligt sätt att hantera öppna filer, databaskoppel, mutexar och annat som behöver stängas. Andra språk har andra, ofta krångligare, sätt att få motsvarande resultat.

Språket go har en helt annan aproach, som är värd att titta lite närmare på.

Eftersom hantering av databaskoppel och filer har en hel del andra komplikationer och genvägar i olika språk så tar jag ett annat, enklare, exempel: att mäta och logga exekveringstid för en funktion. Här är ett sätt att göra det i c++:

Läs hela Destruktorer och defer med 3 kommentarer.

Detect encoding-tags in python

Posted 2012-04-22 19:50. Tagged , , , .

When iterating over a set of input files in Python, how to decode the lines of each file with the correct encoding?

I wrote a hook for the fileinput module to look for a -*- coding: foo -*- marker in the first lines of each file.

Read whole Detect encoding-tags in python.

Tech update on Rasmus.krats.se

Posted 2011-07-21 15:00. Tagged , , , .

I’ve run a little django on this site for a long time beside the actual content (which was static files, created with make and xslt). Now I’ve gone all out and run the entire site in django.

At the same time, I’ve modernized the markup using some semantic html5. Some javascript and css should make it work even in browsers that doesn’t really support it (like MSIE 8 and older). The graphical design has got an update to, even if it remains rather similar to the way it has looked since 2009. I hope it is both nice looking and useable on both big screens and small phones.

Read whole Tech update on Rasmus.krats.se.

Chordlab: chopro to pdf

Posted 2008-02-12 00:15. Last updated 2010-10-20 05:55. Tagged , , , , .

Chordlab is a command line program I wrote to create pdf songbooks from chopro source.

Normal usage is to give chordlab a list of chopro files (each containing one or more songs). Chordlab then produces a pdf of one page for each song. Long songs can span several pages, as needed. Since the lines of a song text is often short, chordlab supports pages with multiple columns.

Update 2010-10-20: To make chordlab more truly open, and since I sucked at hosting a publicly accessible monotone repository, I have now moved chordlab to github, where it is availiable as kaj/chordlab.

Read whole Chordlab: chopro to pdf.

Comment on rasmus.krats.se

Posted 2008-08-30 09:05. Tagged , , , .

Now you can write comments on my pages! I only allow comments from logged-in users. Of course, you don’t want to create yet another web account that you have to remember the password for. I understand that. I don’t want to administrate yet another user database either.

The solution is OpenID. Now one account, in a central place you select, is enough for any number of web sites! (or one for each pseudonym you choose to use).

Maybe you don’t even have to create a single account — chances are you have one already! If you have an account at e.g. KTH, LiveJournal or WordPress, you can use that! If not, there is a rather long list of providers that you can use.

Read whole Comment on rasmus.krats.se.