Sunday, June 27, 2010

django localization

After digging into django source code, I found a way to format a number or into a proper localization format.

Here is how to do it.

>>> from django.utils import formats
>>> from decimal import Decimal
>>> formats.localize(Decimal(12342341))
u'12,342,341'

You can check out the source code of django.utils.formats, and you can also find some other functions to localize date/time format, and also to sanitize numbers which are localized.

So, remember the DRY principle, and make sure you don't reinvent the wheel by creating existing functionalities.

I'm enjoying django more and more.

Thursday, June 24, 2010

Python Idioms, Idiomatic Python

Get to know more about Python Idioms.

Just some samples of nice python idioms:

result = 'ok' if x > 10 else 'not ok'
result = [ 3 * d.Count for d in data if d.Count > 4 ]

Here are some links which I found very interesting:
http://python.net/~goodger/projects/pycon/2007/idiomatic/handout.html
http://bayes.colorado.edu/PythonIdioms.html

I will find more links and add them to this post later on.

Use xrandr to set Monitor Screen Resolution

I had a problem with gdm and also with lxde desktop session screen resolution. This is probably problem which occurs on old monitors.
The problem is, in gdm login screen the screen resolution is set to 800x600, which is not good.
So, I found a way on how to set the screen resolution for gdm from this link.

Taken from that link, here is how to do it:
Setting xrandr commands in kdm/gdm startup scripts
Both KDM and GDM have startup scripts that are executed when X is initiated. For GDM, these are in /etc/gdm/ , while for KDM this is done at /etc/kde4/kdm/Xsetup. In either case, you can paste in an xrandr command line string into one of these scripts. For GDM, try putting them right before initctl -q emit login-session-start DISPLAY_MANAGER=gdm in /etc/gdm/Init/Default

Monday, June 21, 2010

Canon Pixma MP145

Here's the steps:

Download the following from canon website:
1. cnijfilter-common_2.80-1_i386.deb
2. cnijfilter-mp140series_2.80-1_i386.deb

There will be problem related with dependency if you want to install them directly (my case is with Ubuntu 10.04).
Both packages depend on libcupsys2, while in my version of Ubuntu it should be libcups2.

So, next step is to modify those packages to use the proper package dependency.

$ mkdir modified-deb
$ dpkg -x cnijfilter-common_2.80-1_i386.deb modified-deb/cnijfilter-common_2.80-1_i386
$ dpkg -e cnijfilter-common_2.80-1_i386.deb modified-deb/cnijfilter-common_2.80-1_i386/DEBIAN

after that, then edit modified-deb/cnijfilter-common_2.80-1_i386/DEBIAN/control and replace licupsys2 with libcups2.

Repackage the updated version:
$ cd modified-deb
$ dpkg -b cnijfilter-common_2.80-1_i386

Do the same thing with the other package, cnijfilter-mp140series_2.80-1_i386.deb.
Finally, install both packages, and you are done.

Wednesday, June 16, 2010

Scp in Nautilus

Performing scp in nautilus turns out to be so easy. Since I use custom port for my ssh server, I just need to enter the following in nautilus location (ctrl+L for shortcut):

ssh://server_ip:custom_port

Then nautilus will ask your username and password for ssh. That's it.

Django 1.2.1 Localization

I just finished playing around with django 1.2.1 localization. I did that because after finished upgrading to django 1.2.1, I was amazed with the Indonesian language localization in the admin page. Exactly what I need!
I had a bit trouble working with localization, which turned out that it is related with the language support of the OS, in my case, Ubuntu 10.04. Another thing is that I was doing this on ubuntu live-usb flash. I can take that usbflash, plug it in any computer, boot up, and continue my work.

First of all, go to the following directory and check whether your OS has support for your preferred language.

$ cd /var/lib/locales/supported.d
$ ls

In my case, I did not have 'id' in that directory. So, I had to perform the following:

$ sudo aptitude install language-pack-id-base language-pack-id

Then perform the following:

$ pwd
/var/lib/locales/supported.d
$ cat id # my preferred language
id_ID.UTF-8 UTF-8


After that let's perform some test in python.

>>> import locale
>>> from datetime import datetime
>>> locale.setlocale(locale.LC_ALL, 'id_ID.UTF-8')
'id_ID.UTF-8' # I took the id_ID.UTF-8 from the content of /var/lib/locales/supported.d/id
>>> datetime.now().strftime('%A %B')
'Rabu Juni'

Right on! Things are working as expected.

Now, it's time to use localization in django forms.
First set the following configuration in settings.py:
USE_L10N = True
USE_THOUSAND_SEPARATOR = True

Then, create a simple form which implements localization.

class TestForm(forms.Form):
deposit = forms.DecimalField(localize=True)
tanggal = forms.DateTimeField(localize=True)

...and you are done. All you need to do is just create a view which has an instance of TestForm and bind it a data (dictionary), and then render it.

The neat thing is when you want to display 'Decimal' data in that form, you will see the proper local thousand separator format being displayed. It will also take local format as an input and recognized and convert it properly.

Enjoy django 1.2.1!