b a d p o p c o r n

Django and Dreamhost, Take 2

Written by Ben on November 13, 2006 | No Comments

After installing my own custom Python build, I went back and tweaked how Django ran on my Dreamhost account. My previous Django installation experience is still mostly valid. I just figured I could “upgrade” the installation to take advantage of Python 2.5 and eggs.

I installed three eggs to get Django running

# For Django
python ez_setup.py MySQL-python
python ez_setup.py http://www.saddi.com/software/flup/dist/flup-r2030.tar.gz
python ez_setup.py http://www.djangoproject.com/download/0.95/tarball/

The first was mySQL support; second was for Django’s built-in fcgi support; and finally Django itself.

I had a second goal in this upgrade. I wanted to partition off the “/-/” subtree (For example, http://example.com/-/admin/) for Django, instead of using the full subdomain. I had to make a couple of changes to the project’s urls.py file (to handle the ‘-’) and to the .htaccess file:

# urls.py change example for the admin application.
(r’^-/admin/’, include(’django.contrib.admin.urls’)),

The .htaccess file:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^-$ /-/ [R]
RewriteRule ^-/(.*)$ /_/mysite.fcgi/-/$1 [QSA,L]
</IfModule mod_rewrite.c>

And finally, a new mysite.fcgi script found in the /_/ directory (notice that it’s an underscore, not a dash):

#!/home/user/bin/python/bin/python
import sys, os

# Switch to the directory of your project. (Optional.)
# os.chdir(”/home/user/myproject”)

# Set the DJANGO_SETTINGS_MODULE environment variable.
os.environ['DJANGO_SETTINGS_MODULE'] = “myproject.settings”

from django.core.servers.fastcgi import runfastcgi
runfastcgi(["method=threaded", "daemonize=false"])

Posted in Python, Web

No Comments

Python, Eggs and Dreamhost

Written by Ben on October 29, 2006 | 3 Comments

Dreamhost, my web host, does not yet support Python2.5, and one can’t install Python eggs into Dreamhost’s own Python installations. The solution? I just installed a custom build of Python2.5 into my $HOME/opt directory. The following is the script I created to automate that process:

# Obtain Python2.5 and unpack
mkdir $HOME/downloads/
cd $HOME/downloads/
wget http://www.python.org/ftp/python/2.5/Python-2.5.tgz
tar -zxvf Python-2.5.tgz

# Creating the location under which python (and other apps) are to be installed.
mkdir $HOME/opt

# Go into the Python2.5 directory, make && make install the thing
cd Python-2.5
./configure –prefix=$HOME/opt –enable-unicode=ucs4
make && make install

# Create a bin directory for Python, and link the bin to the real executable
mkdir -p $HOME/bin/python/bin
ln -s $HOME/opt/bin/python $HOME/bin/python/bin/python

After the installation, I edited the bash_profile file to add the bin path:

export PATH=$HOME/bin/python/bin:$HOME/opt/bin:$PATH

Now with a custom installation of Python, I can use eggs to manage my Django projects’ library dependencies. :)

Posted in Python, Web

3 Comments

Python Conditional Expressions

Written by Ben on September 19, 2006 | 2 Comments

Python had lacked a C equivalent of the “condition ? true_value : false_value” expression. So we used a work around until now… conditional expressions finally made it in to Python’s 2.5 release.

In Python2.4:

x = ((condition) and true_value) or false_value

In Python2.5:

x = true_value if condition else false_value

Yeah, the statement looks better in Python2.5.

Posted in Python

2 Comments

Map, Filter, and Reduce over Python Dictionaries

Written by Ben on March 16, 2006 | No Comments

I wanted to apply some Functional Programming ideas to solve a problem in some Python code I had, which involved manipulation of data found in a couple dictionaries. Unfortunately, the built in map, filter, reduce, and list composition methods can only be used over iterable lists.

Luckily, I remembered that equivalent dictionaries in LISP are all represented as a list-of-lists: ( (key1, val1), (key2, val2) ); python map & filter functions can then be used over this converted datastructure.

After a little digging, I found that it’s really easy to do this conversion in Python:

# Uses the list composition to make the key value pairs over a dictionary.
dict2list = lambda dic: [(k, v) for (k, v) in dic.iteritems()]

# Use the built in dictionary constructor to convert the list.
list2dict = lambda lis: dict(lis)

# Example Usage follows:

dict1 = { ‘a’:1, ‘b’:2, ‘c’:3, ‘d’:4 }

# Filter function
is_even = lambda item: item[1] % 2 == 0
# Map functions
add_two = lambda item: (item[0], item[1] + 2)
add_sss = lambda item: (”%s SSS” % item[0], item[1])
# Reduce function
add_all = lambda x, y: (”%s %s” % (x[0], y[0]), x[1] + y[1])

# Make sure you have the [] around the reduce call.
list2dict([reduce(add_all, map(add_two, map(add_sss, filter(is_even, dict2list( dict1 )))))])