Important update to earlier post on MySQL Installation

I have updated an earlier post on how to install MySQL 5.5 on Ubuntu 10.04 LTS.

Importantly, the new instructions include how to add the MySQL 5.5 libs to the loader path. This is very important as it’s highly likely that you’ll build software that depends on these MySQL libraries.

The updated instructions can be viewed at: MySQL 5.5 on Ubuntu 10.04 LTS.

Install Python 2.7.2 from source on Ubuntu 10.04 LTS

The first thing I did was to create a wwwuser that I plan to run pyramid under. As a result, I am intentionally installing Python 2.7.2 under only 1 user account, and am leaving the system wide python installation unchanged.

useradd wwwuser
passwd wwwuser
cd /home
mkdir wwwuser
chown wwwuser:wwwuser wwwuser

Copy all of the hidden files into the /home/wwwuser folder. I did this from my desktop files.

vi /etc/passwd
Update the shell to be:
/bin/bash

su - wwwuser
ln -s .profile .bash_profile
mkdir bin
mv Python-2.7.2.tgz bin
cd bin
tar -xzf Python-2.7.2.tgz
cd Python-2.7.2
./configure --prefix /home/wwwuser/bin/Python-2.7.2
make
make install

vi ~/.profile
Update path to:
PATH="$HOME/bin:/home/wwwuser/bin/Python-2.7.2/bin:$PATH"

source ~/.profile

which python
/home/wwwuser/bin/Python-2.7.2/bin/python

python --version

The output of the python --versioncommand should now be Python 2.7.2.

Install Tornado on Windows 7

Please see my prior post on how to Setup virtualenv on Windows.

Open PowerShell, then enter the following commands:

cd C:\Python27

.\env_tornado\Scripts\activate.bat

.\Scripts\easy_install.exe pip

pip.exe install tornado -E .\env_tornado

 

If you want to install in the global site packages, then skip pip above and use:

.\Scripts\easy_install.exe tornado

That’s it. Time to start writing code.

Setup virtualenv on Windows

I’m going to assume you have Python 2.7 correctly installed on Windows at C:\Python27. These instructions are applicable to Windows XP and Windows 7.

 

Open Windows PowerShell, then enter:

cd C:\Python27

.\Scripts\virtualenv.exe –no-site-packages env_tornado

And now to activate the new environment:

.\env_tornado\Scripts\activate.bat

The new virtual environment is now setup and ready for use

Output XML using Pyramid and Mako

I recently had to connect a .NET desktop application to a web service that was built using Pyramid. My first thought was to use JSONP. However, JSONP is only useful for small amounts of data…and I had a lot of data to send/receive. Obviously, communication via an XmlHttpRequest with JSON was not an option due to limitations imposed by the Same Origin Policy (ref: http://en.wikipedia.org/wiki/Same_origin_policy).

In the end, we chose to use our existing Mako template infrastructure to render XML that the .NET clients could consume. If you’re a .NET developer, then you know that XML parsing in .NET is superb, which is why we went with XML rather than JSON over HTTP.

On the Pyramid side, there were really only 2 steps:

  1. Create Mako templates that would render XML
  2. Set the content type to text/xml

Render XML with Mako

This step was easy, we just created templates that use XML instead of XML.

<?xml version=”1.0″ encoding=”utf-8″?>
<companies>
% if companies:
% for row in companies:
<company>
<id>${row[‘id’]}</id>
<name>${row[‘name’]}</name>
</company>
% endfor
% else:
${result}
% endif
</companies>

Set the Response Content Type

This was also a very easy step. We just set the content_type immediately before the call to return. Importantly, the code below causes Pyramid to return the XML template with a content type of text/xml rather than the default text/html.

request.response.content_type = “text/xml”
return {‘companies’: companies}

Summary

Rendering XML with Pyramid and Mako was exceptionally simple. You can reuse the same view and url, and simply change the the template based on how the view was called.

One last note. You’ll notice that one negative side effect we have not corrected for is the use of \n\r in the rendered XML. This of course bloats network traffic, uses more memory on the client-side (if you’re using a DOM parser), and is all around crappy for all but the smallest of files. Next step will be to create some type of processor to eliminate the newline character.