September, 2008


26
Sep 08

Default paramater values with python

Default argument values for functions are a nice shortcut in python. They save you from writing many wrapper functions and make the code more readable and also easier to use. They can be used wrong though:

>>> def foo(a=[]):
...     a.append('bar')
...     return a
...
>>> foo()
['bar']
>>> foo()
['bar', 'bar']

Like described in idiomatic python, using a value which is a refenence (like a list, dict or instance) can lead to odd results. This is because the arguments are evaluated at compile time, not at runtime when the function is called! Instead of creating an empty list as one (inexperienced) would suspect, the default value becomes a reference to a list.

This can be nasty when it is used like this:

>>> def bar(time=datetime.now()):
...     print time
...
>>> bar()
2008-09-26 12:32:23.598052
>>> bar()
2008-09-26 12:32:23.598052

Just like in idiomatic Python, this is a good fix:

>>> def bar(time=None):
...     if time == None:
...             time = datetime.now()
...     print time
...

>>> bar()
2008-09-26 12:35:02.258992
>>> bar()
2008-09-26 12:35:03.059305

Instead of the default argument datetime.now(), using None, and later setting the current time instead of None works fine!


26
Sep 08

Removing the link boxes from Latex’ Hyperref (and color)

Aah Latex… A love hate relation ship. Working on my thesis I use the hyperref package for some nice linking support in my document. unfortunately this also causes them to get link boxes around them. Here’s the code to remove them:

\usepackage[pdftex]{hyperref}
\hypersetup{
    colorlinks,%
    citecolor=black,%
    filecolor=black,%
    linkcolor=black,%
    urlcolor=black
}

This makes the links use colors inststead of boxes/frames, and sets the used colors to black. Fixed! Although this should be a standard convenience option imho.


19
Sep 08

Django Tabbed Admin

A while ago i was playing with the excellent pinax (django-hotclub) project and i felt like the admin index page was kind-of overwhelming due to all the different apps listed in it.

So i put a simple app together that has one template tag and a slight modification of the "admin/index.html" template to create simple tabs in the django admin interface.

To minimize my effort ;-) i used jquery and ui.tabs.js to create the tabs at runtime. With one mandatory tab called "main" (changeable in settings.py), which contains all apps not mapped to a specific tab, and an attribute in settings called "ADMIN_TABS" that maps different apps to different tabs.

It's still more-or-less a proof-of-concept applicatio, but it shows how easy these kinds of modifications can be in the django admin interface.

Screenshots

Mercurial repository

Afterthoughts:

I would like to extend this to something like shown in the Gondola screencast by Peter Baumgartner at a later time where a specific tab can also short-cut to common usage patterns.


13
Sep 08

Japanese books about Django and Python

Japanese Django book

When looking for English books in a bookstore i came across the programming section of the 8 story book store and i could not resist to take a look and see if i could find any Python or Django books there.

Look at my picture from what i've found here: http://trbs.net/photos/python-django-books-in-japanese/

Regretfully these where only about two bookshelves worth of Python books. Compare that too one full cabinet of Ruby and Rails books, one cabinet for Perl, a hole alley of Java books, an alley for c and c++ and lastly an alley for php/asp/vb/c# and other some other languages. *I'd even found some scheme, lisp and cobolt books in there :) *

Worthy to note was that the Microsoft languages section was far less in total then i'd expected. While on the other hand a relatively minor language like Ruby had far more. Ofcourse japan is the homeland of Ruby so that might explain it's high number of books ;) (On a site note; probably the only people that could have invented such a syntax horrible language are the Japanese. Of-course I'm purposely forgetting about Perl here)

Anyways i did not want to withhold u guys of these pictures, specially the one from the Django book that has a lovely cover, good layout and lots of examples, i even found a lot of debugging instructions and examples in there.

P.S. The pictures where taking with the build-in camera of the N810 so quality is very poor.


11
Sep 08

CERN – Largest particle accelerator

Geneva, 10 September 2008. The first beam in the Large Hadron Collider at CERN was successfully steered around the full 27 kilometres of the world’s most powerful particle accelerator at 10h28 this morning. This historic event marks a key moment in the transition from over two decades of preparation to a new era of scientific discovery.

A great moment for science and the world !


9
Sep 08

Japanese Lessions

Nothing beats your first official Japanese lesson they must have thought ;-)

Well it was very amusing to say the least. I just started practicing Hiragana less then a week ago and my self-study-lessons date back from before 2004. The fun things is that i clearly remember some things from the self-study i did way back, specially in the form of a passive (very incomplete) mind-dictionary. I remember a lot of the simple words and it all came back to me fairly quickly during this first lesson.

However that's not to say that this is going to be easy :( Now i have a big text and workbook on my desk which i have to study in besides working in the lab if i want to have any change of learning some basic Japanese.

So which me luck guys and girls :)

One thing is for sure, if the rest of the lessons will be this amusing i will have a great time studying Japanese :)


8
Sep 08

clean_pyc and compile_pyc in Django-extensions

I recently added two new commands to the django-extensions project.

  • clean_pyc, removes all compiled python bytecode files from your project directory
  • compile_pyc, compiles all python files to bytecode files in your project directory

These are handy shortcuts to what you would normally do with something like "$ find -name "*.py[co]" -delete" or python's compileall import. The main added advantage is a little piece of code that checks the correct working directory. So it will only compile or delete files in your project root, even if you call manage.py from somewhere else.

One other thing to note is that you will always see files being deleted when you call "$ ./manage.py clean_pyc -v" this is because when manage.py gets loaded python will compile files in your project root which are then in turn deleted by the command ;-)

Check it out at: http://code.google.com/p/django-command-extensions/


6
Sep 08

Electro-House-Party

Originally the plan was to go and climb Mt. Fuji, but after getting on the train i was swept into this outdoors Electro/House party. I just love how Tokyo does this to you. Regretfully no pictures because it was very dark, so my compact didn't work so well.

This turned out very very well, specially because the weather was terrible and climbing without good gear in this weather would have been terrible. Hopefully i will still get the change somewhere in the coming weeks.

In good Japanese fashion the party was spread out over couple of fields, some with live acts (mostly young espirering artists) and other with DJ's. And lots of little stalls with foods and drinks in between.

Needless to say i really enjoyed myself :-)

Already wondering what the next week will bring...


5
Sep 08

Django 1.0 Released

Congratulations everybody Django 1.0 has landed

Thanks for all the hard work of the Django core team !!!


5
Sep 08

The First Post

Always a very important moment in history, when the first post happens :)