/kb

personal knowledgebase

Archive for the ‘Programming’ Category

Translation of Django Core

without comments

cd django_trunk
svn update
# Generate .po files:
bin/django-admin.py makemessages -l nn 
bin/django-admin.py makemessages -d djangojs -l nn
cd conf/locale/nn/LC_MESSAGES

Edit .po files. Search for text string fuzzy and empty double quotes (“”) to find untranslated strings.

Finally, create diffs:

svn diff django.po > nn-django-translation-update.diff
svn diff djangojs.po > nn-djangojs-translation-update.diff

Written by hgrimelid

August 7th, 2010 at 9:31 pm

Posted in Programming

Tagged with ,

Compile Vim in Snow Leopard

without comments

./configure –enable-pythoninterp –with-macsdk=10.6

via Chris Moyer: OSX, Vim, and Python.

Written by hgrimelid

April 28th, 2010 at 11:01 am

Posted in Editors,Programming,Shell

Tagged with ,

Git: Start a new branch after making changes

without comments

Sometimes I realize that my latest changes actually should belong to another branch than the one I’m currently working on. The easy way to add the changes to a new branch is to use the stash command.

[… changes …]
git stash
git stash branch name_of_new_branch
git commit []
git push origin name_of_new_branch

Ref. git stash.

Written by hgrimelid

April 23rd, 2010 at 12:39 pm

Posted in Programming,Shell

Tagged with

Translation of Django Apps

with one comment

I recently created a Django app that needed translation of the Django administration. It took some googling and messing around to make it work, so this quick and dirty walkthrough will hopefully be helpful for others.

Here’s how I did it:

Add translations for all model fields:

class Stuff(models.Model):
    title = models.CharField(_('title'), max_length=255)
    the_other_model = model.ForeignKey('othermodel', verbose_name=_('the other model'), verbose_name_plural=_('the other models'))
 
    class Meta:
      verbose_name = _('stuff')
      verbose_name_plural = _('stuffs')

Then the language (.po) files must be created:

mkdir locale
python manage.py makemessages -l no

Then edit your language files in locale/_LANGUAGE_/LC_MESSAGES/django.po. When you’ve finished editing the language file must be compiled:

python manage.py compilemessages

If you want to translate app names as well, you can add something like this to your project __init__.py:

from django.utils.translation import ugettext_lazy as _
 
_(u'auth')
_(u'configuration')
_(u'sites')
_(u'appname')
_(u'Auth')
_(u'Configuration')
_(u'Sites')
_(u'Appname')

This feels like a dirty hack but I couldn’t find any other way to do it.

Source: Django documentation – Internationalization and localization, and some Google Groups thread that I can’t find right now, for the app name translation.

Written by hgrimelid

March 4th, 2010 at 3:50 pm

Posted in Programming

Tagged with ,

Install PIL (Python Imaging Library) in Leopard

without comments

Installing modules when using pip, virtualenv and virtualenvwrapper is a breeze, but it took some time before I realized what was the easiest way to install PIL.

I do it this way:

pip -E PATH_TO_VIRTUALENV install http://dist.repoze.org/PIL-1.1.6.tar.gz

This installs a slightly repackaged version of PIL. The latest PIL version is 1.1.7, but as far as I know it’s not available in this form.

Written by hgrimelid

February 7th, 2010 at 1:09 pm

Posted in Programming

Tagged with , , , , ,

Chapter 7 in “Python Web Development with Django” – some notes

without comments

I am currently in the process of learning Django and have just finished chapter 7 in Python Web Development with Django. Here are some notes on how I got it working (except for the index page which still isn’t completely as expected) on an ubuntu hardy installation.

The photo gallery example assumes an apache2 + mod_python setup. I didn’t really find it straightforward to get this running from the description in the appendix, so here is how I got it to work (after installation of apache2 and mod_python, of course):

In /etc/apache2/sites-available/default, change lines:

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride None
                Order allow,deny
                allow from all
        </Directory>

To:

        <Directory /var/www/>
                Options Indexes FollowSymLinks MultiViews
                AllowOverride AuthConfig
                Order allow,deny
                allow from all

                AddHandler mod_python .py
                PythonHandler mod_python.publisher
                PythonDebug On
        </Directory>

Then I did the changes in /etc/apache2/httpd.conf, but slightly different from the book by adding a PythonOption django.root (didn’t work for me with the setup in the book):

<Location "/gallery/">
      SetHandler python-program
      PythonHandler django.core.handlers.modpython
      SetEnv DJANGO_SETTINGS_MODULE gallery.settings
      PythonOption django.root /gallery
      PythonDebug On
      PythonPath "['/path/to/django/root', '/home/user/django-stuff'] + sys.path"
</Location>

With an additional entry for media files:

<Location "/gallery/media">
      SetHandler none
</Location>

Now, I created a directory /var/www/gallery and a /var/www/gallery/media like explained in “Preparing for File Uploads” and edited settings.py:

MEDIA_ROOT = '/var/www/gallery/media/'
MEDIA_URL = 'http://localhost/gallery/media/'
ADMIN_MEDIA_PREFIX = 'http://localhost/gallery/media/admin/'

Where admin is a symbolic link (/var/www/gallery/media/admin) to the admin media directory in the Django installation. The two last entries where not mentioned in the book, but they made it work…

With the settings described above and the PythonOption django.root in httpd.conf, I didn’t need the section about DRY URLs. And it worked – as opposed to the stuff in the book.

With all the configuration stuff in place I typed the code outlined in the chapter and to begin with I couldn’t see any photos, except for the thumb_url in items_list.html (typo: items_listing.html in the last paragraph page 175). I managed to get it working by changing some lines in items_detail.html and photos_detail.html though:

22                 <img src="{{ photo.image.thumb_url }}" />

instead of

22                 <img src="{{ photo.get_image_thumb_url }}" />

and

8 <img src="{{ object.image.url }}" />

instead of:

8 <img src="{{ object.get_image_url }}" />

The alternatives that worked seems more intuitive to me.

I also imported list_detail from django.views.generic in urls.py, but am not sure if this was needed. The photo gallery seems to work fine except for the thumbnails in index.html. I’ve noticed that the code in urls.py for that template is different from the others, using simple.direct_to_template, but I haven’t managed getting that part to work.

Thanks to this thread for getting mod_python up and running: Python and Apache2

Written by Morten Wergeland Hansen

March 8th, 2009 at 8:08 pm

Posted in Programming

Tagged with , , , ,

Strings and equality in Java

without comments

In order to compare objects in Java we are taught to use the equals()-method, except when the goal is to check for object reference, explicitly, then the == operator must be used. However, String objects does not behave entirely as expected, as seen from the following example.

  String s1 = "Fish";
  String s2 = "Fish";
 
  String s3 = new String("Fish");
  String s4 = new String("Fish");
 
  System.out.println(s1 == s2);       // true
  System.out.println(s1.equals(s2));  // true
 
  System.out.println(s3 == s4);       // false
  System.out.println(s3.equals(s4));  // true

The reason for this behaviour is that strings s3 and s4 are created outside of the string pool. In the string pool, String objects with the same value are set to the same object internally in order to save space.

Written by Håvard Grimelid

June 4th, 2008 at 1:32 pm

Posted in Programming

Tagged with ,

Prettify print_r output

without comments

By putting print_r() statements enclosed by html pre-tag, the output is quite a lot more readable.

Written by Håvard Grimelid

May 7th, 2008 at 6:21 pm

Posted in Programming

Tagged with ,