from cwt import Idea as Blog


Install PIL in Snow Leopard

It seems like many people install PIL in Snow Leopard but the installed library can't open any Jpeg file. Today I try to fix that problem in a Macbook that just upgraded its OS to Snow Leopard (so we have to install everything again).

First, I found that the Macbook owner already install libjpeg via fink (http://www.finkproject.org/). Unfortunately, libjpeg in fink was pre-compiled without --enable-static which required by PIL.

So, my solution is so simple. I just uninstall the libjpeg that came with fink by:

$ sudo apt-get uninstall libjpeg-shlibs

If you already installed other packages that require libjpeg and libjpeg-shlibs they will be removed also. However, if you really need a functioning PIL, you have to do this.

Next, I download libjpeg latest version (http://www.ijg.org/files/jpegsrc.v7.tar.gz), untar it, and configure it:

$ tar zxvf jpegsrc.v7.tar.gz
$ cd jpeg-7
$ ./configure --enable-shared --enable-static
$ make
$ sudo make install

Now, you just installed libjpeg into /usr/local/lib. So, it's time to install PIL. Download the PIL source code from http://effbot.org/downloads/Imaging-1.1.6.tar.gz then untar it:

$ tar zxvf Imaging-1.1.6.tar.gz
$ cd Imaging-1.1.6

This is the trick, you must open setup.py with your prefered text editor then looking for the line JPEG_ROOT = None and then it to JPEG_ROOT = libinclude("/usr/local") then save the file and continue:

$ python setup.py build

If everything fine, you can install PIL to your system library. If the PIL need something that didn't exist, it will tell you on the error message. You may install the missing library via fink or download and compile it by yourself. Then install the PIL as root:

$ sudo python setup.py install --optimize=1

Done. :)

VirtualEnv in Snow Leopard

I just install OS X 10.6 (Snow Leopard) in my iMac. Python 2.6.1 is installed automatically with X Code, however I found a problem with virtualenv.

After install virtualenv by command:

$ sudo easy_install -Z -O1 virtualenv

And try to create a new virtual environment:

$ virtualenv myenv1

It just freeze and use 100% on one of my dual core. After google for a while, I found this solution:

$ sudo easy_install -Z -O1 http://bitbucket.org/ianb/virtualenv/get/tip.zip

Then everything working like a charm. (The cause of this problem is easy_install was trying to install from the SVN which didn't contain fix patch for Snow Leopard yet.)

Django Dynamic Form

Create dynamic form for render as an empty web form.

from django import forms
form_config = {'login':'CharField', 'email':'EmailField', 'active':'BooleanField'}
dynamic_form = forms.Form()
for key in form_config.keys():
dynamic_form.fields.insert(-1, key, getattr(forms, form_config.get(key))())
print dynamic_form.as_table()

output

Create dynamic form and validate the input data.

from django import forms
form_config = {'login':'CharField', 'email':'EmailField', 'active':'BooleanField'}
dynamic_form = forms.Form()
dynamic_form.__init__({'login':'cwt', 'email':'cwt.bashell.com', 'active':True})
for key in form_config.keys():
dynamic_form.fields.insert(-1, key, getattr(forms, form_config.get(key))())

Validation

>>> dynamic_form.is_valid()
<<< False

>>> dynamic_form.errors
<<< {'email': [u'Enter a valid e-mail address.']}

Note that when you want to validate the data, you must initialize dynamic_form with the input data before insert the fields.

differentiate lists by set

Given 2 lists:

  • list_1 = ['a', 'b', 'c', 'd', 5, 6, 7]
  • list_2 = ['a', 's', 'd', 'f', 5, 7, 9]

I want to know these answers:

  • The members that exist in both lists.
  • The members that only exist in list_1.
  • The members that only exist in list_2.

So, How to do something like that in python?

If you just learning something in python about list and loop, you may try get each member from list_1 and check that it is in the list_2 or not.

However, there is a very easier method. If you ever learn about "Set" in your mathematics course, you should know about "intersection".

You can convert a list to a set so easily by casting it to set:

  • set_1 = set(list_1)
  • set_2 = set(list_2)

Then just do the "set way":

  • Intersection means members that exist in both sets. So the answer for the first question is
    • in> set_1.intersection(set_2)
    • out> set(['a', 'd', 5, 7])
  • Well, we want the list back not the set, so cast it back to list
    • in> list(set_1.intersection(set_2))
    • out> ['a', 'd', 5, 7]
  • So what are the members that only exist on list_1 or list_2? You can do the operation "minus" (-) like this:
    • in> list(set_1 - set_2)
    • out> ['c', 'b', 6]
    • in> list(set_2 - set_1)
    • out> [9, 's', 'f']

Done...

Author:

  • chaiwat

Archives:

Powered by Django.