Latest Entries
Setting up a template_postgis on Lucid
I wasn't able to find instructions in the Django docs for setting up a template_postgis database with postgis-1.5 and Postgres 8.4 on Ubuntu Lucid (10.04). Below is what worked for me. GeoDjango installed via Ubuntu 10.04 packages #!/usr/bin/env bash POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib createdb -E UTF8 template_postgis # Create the template spatial database. createlang -d template_postgis plpgsql # Adding PLPGSQL language support. psql -d postgres -c "UPDATE pg_database SET datistemplate='true' WHERE datname='template_postgis';" psql -d template_postgis -f $POSTGIS_SQL_PATH/postgis.sql # Loading the PostGIS SQL routines psql -d template_postgis -f $POSTGIS_SQL_PATH/spatial_ref_sys.sql psql -d template_postgis -c "GRANT ALL ON geometry_columns TO PUBLIC;" # Enabling users to alter spatial tables. psql -d template_postgis -c "GRANT ALL ON spatial_ref_sys TO PUBLIC;" GeoDjango built from source Use the above script, but change POSTGRIS_SQL_PATH to: POSTGIS_SQL_PATH=`pg_config --sharedir`/contrib/postgis-1.5 Make sure to run sudo ldconfig after compiling everything, or else postgis won't be able to find geos. You'll see errors like this: ...
Bitten by Python's Pass-By-Reference
I just got bitten by an interesting bug related to Python's pass-by-reference feature. I had a class method (my_method) with a keyword argument (my_kwarg) that defaulted to {}. I thought that each time my_method was called, if my_kwarg wasn't passed then it would default to an empty dict. In reality, when the class was read by Python it evaluated the {} and made the default value a reference to the dict in memory. So each time my_method was called without passing my_kwarg, it was defaulting to that memory reference which contained data from a previous use. Here's an example class: class MyClass(object): def my_method(self, my_kwarg={}): return my_kwarg And here's an iPython shell session illustrating what I mean: In [2]: first_class = MyClass() In [3]: kwarg = first_class.my_method() In [4]: kwarg Out[4]: {} In [5]: kwarg.update({'foo': 'bar'}) In [6]: kwarg Out[6]: {'foo': 'bar'} In [7]: kwarg = first_class.my_method() In [8]: kwarg ...
Provisioning a new Ubuntu server for Django
I've been a long-time satisfied user of Webfaction, but recently I've had a strong urge to move to VPS hosting so that I can have greater control over the environment. After some research, I went with Rackspace Cloud because of the incredibly cheap low-end options. My site doesn't use a huge amount of bandwidth, so Rackspace looks to be the most feature-packed and still cost-effective option. A friend of mine, Kevin Whitaker, recently posted a great article about getting up and running with Django in a server environment for testing or production. He used Ubuntu, Postgres, Nginx, and FastCGI to make up his stack. I've never set up Nginx before, so his post was a great help in getting Nginx configured. My stack is slightly different, however, since I prefer to use Gunicorn instead of FastCGI and I use supervisord to manage my processes. I also use virtualenv to manage ...
Using virtualenvwrapper to start processes and swap config files
I do my Django development work locally on OS X, so I have several different daemons installed on my machine. I used to keep Postgres, MySQL, lighttpd, memcached, and more set up to autostart and run continuously, but I didn't like the burden on performance (real or imagined). Also, I switch between projects frequently and I often need to switch config files based on the project I'm about to work on. My solution is to use the end-user customization hooks that Doug Hellmann's excellent virtualenvwrapper provides. If you're not familiar with virtualenvwrapper, it makes it easy to manage your virtualenvs in one place and switch between them quickly and conveniently. The project provides several scripts that are meant to be added to by the end-user to customize behavior. Configuration For my configuration files, I create multiple versions with the changes needed for each project, and I then create a symlink ...
Dynamically Updating IP Addresses with Update-ip
A couple of years ago, I write a post including a snippet to automatically update your DNS overrides with WebFaction. That script served my purpose well until I moved to an ISP that gave me a static IP. Recently, though, I moved to a new apartment and signed up for service through Clear's 4G network in Dallas. I'm back to a dynamic IP, and so I dusted off the old script. Being the coding geek that I am, I decided to expand it into a full-blown command-line utility that could support multiple services. I cooked up an updater class and a DNS service base class, added some tests and a setup script, and posted it to Github. Installation is as easy as pip install update-ip, and from there you can use it from the command line with minimal effort. Right now, Webfaction is the only DNS service supported, but I ...
Snippet: Django Columns Filter
I was looking around for an easy way to split lists of items into columns in a way that the number of items in each column would be less than or equal to the number of items in the first column. This prevents a final column with several more items than the others. I came across an old post from Annie Mullin that established a templatetag to do this using a generator. I liked the idea, but felt that a templatetag was too unwieldy - a filter would be simpler and easier to use in a template. So, I turned it into one: @register.filter def columns(lst, cols): """ Break a list into ``n`` lists, typically for use in columns. >>> lst = range(10) >>> for list in columns(lst, 3): ... list [0, 1, 2, 3] [4, 5, 6] [7, 8, 9] """ try: cols = int(cols) lst = list(lst) except ...
Categories
By Month
- July, 2010
- June, 2010
- May, 2010
- April, 2010
- March, 2010
- November, 2009
- October, 2009
- August, 2009
- April, 2009
- March, 2009
- February, 2009
- December, 2008