<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>iamtgc &#187; Python</title>
	<atom:link href="http://iamtgc.com/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>http://iamtgc.com</link>
	<description></description>
	<lastBuildDate>Wed, 21 Jul 2010 12:13:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Importing XML into a database with Python and&#160;SQLAlchemy</title>
		<link>http://iamtgc.com/2008/01/29/importing-xml-into-a-database-with-python-and-sqlalchemy/</link>
		<comments>http://iamtgc.com/2008/01/29/importing-xml-into-a-database-with-python-and-sqlalchemy/#comments</comments>
		<pubDate>Tue, 29 Jan 2008 21:19:47 +0000</pubDate>
		<dc:creator>tgc</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Postgres]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://iamtgc.com/2008/01/29/importimg-xml-into-a-database-with-python-and-sqlalchemy/</guid>
		<description><![CDATA[Let&#8217;s begin by analyzing the XML we want to import into our database, it consists of a book&#8217;s ISBN, title, and author. &#60;!-- books.xml --&#62; &#60;catalog&#62; &#60;book isbn="1-880985-26-8"&#62; &#60;title&#62;The Consumer&#60;/title&#62; &#60;author&#62;M. Gira&#60;/author&#62; &#60;/book&#62; &#60;book isbn="0-679775-43-9"&#62; &#60;title&#62;The Wind-Up Bird Chronicle&#60;/title&#62; &#60;author&#62;Haruki Murakami&#60;/author&#62; &#60;/book&#62; &#60;!-- imagine more entries here... --&#62; &#60;/catalog&#62; Now we can create the database [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s begin by analyzing the XML we want to import into our database, it consists of a book&#8217;s ISBN, title, and author.<br />
<code>&lt;!-- books.xml --&gt;
&lt;catalog&gt;
  &lt;book isbn="1-880985-26-8"&gt;
    &lt;title&gt;The Consumer&lt;/title&gt;
    &lt;author&gt;M. Gira&lt;/author&gt;
  &lt;/book&gt;
  &lt;book isbn="0-679775-43-9"&gt;
    &lt;title&gt;The Wind-Up Bird Chronicle&lt;/title&gt;
    &lt;author&gt;Haruki Murakami&lt;/author&gt;
  &lt;/book&gt;
  &lt;!-- imagine more entries here... --&gt;
&lt;/catalog&gt;</code></p>
<p><span id="more-13"></span><br />
Now we can create the database table.<br />
<code>create table books
(
isbn varchar(14) primary key not null,
title varchar(50),
author varchar(50)
);</code></p>
<p>Our example depends on <a href="http://sqlalchemy.org">SQLAlchemy</a>, which is a &#8220;Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.&#8221;</p>
<p>While our example leverages a Postgres database, given SQLAlchemy&#8217;s database agnostic approach, it can trivially be modified to access a MySQL database.</p>
<p>This code is derived from a SAX parser originally found in <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&#038;location=http%3A%2F%2Fwww.amazon.com%2FProgramming-Python-Mark-Lutz%2Fdp%2F0596009259%3Fie%3DUTF8%26s%3Dbooks%26qid%3D1189212366%26sr%3D8-1&#038;tag=iamtgc-20&#038;linkCode=ur2&#038;camp=1789&#038;creative=9325">Programming Python 3rd Edition</a><img src="http://www.assoc-amazon.com/e/ir?t=iamtgc-20&amp;l=ur2&amp;o=1" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" />.</p>
<p><code># bookhandler.py
from sqlalchemy import *
from sqlalchemy.orm import *

import xml.sax.handler

pg_db = create_engine('postgres:///testdb?user=homer')

metadata = MetaData(pg_db)

books_table = Table('books', metadata, autoload=True)

class Book(object):
    pass

mapper(Book, books_table)

class BookHandler(xml.sax.handler.ContentHandler):
    def __init__(self):
        self.buffer = ""
        self.inField = 0
        self.session = create_session(bind=pg_db)

    def startElement(self, name, attributes):
        if name == "book":
            self.isbn = attributes["isbn"]
        elif name == "title":
            self.inField = 1
        elif name == "author":
            self.inField = 1

    def characters(self, data):
        if self.inField:
            self.buffer += data

    def endElement(self, name):
        if name == "book":
            self.session.begin()
            self.newbook = Book()
            self.newbook.isbn = self.isbn
            self.newbook.title = self.title
            self.newbook.author = self.author
            self.session.save(self.newbook)
            self.session.commit()
        elif name == "title":
            self.inField = 0
            self.title = self.buffer
        elif name == "author":
            self.inField = 0
            self.author = self.buffer
        self.buffer = ""</code><br />
Now that we&#8217;ve set up our sax parser and handler to parse and load the entries from books.xml into the table, lets set up a small script to drive it:<br />
<code># runit.py
import bookhandler
import xml.sax

parser = xml.sax.make_parser()
handler = bookhandler.BookHandler()
parser.setContentHandler(handler)
parser.parse("books.xml")</code></p>
<p>Now let&#8217;s see if it works:<br />
<code>$ ls
bookhandler.py  books.xml       runit.py
$ python ./runit.py
$ psql testdb

testdb=# select * from books;
     isbn      |           title            |     author
---------------+----------------------------+-----------------
 1-880985-26-8 | The Consumer               | M. Gira
 0-679775-43-9 | The Wind-Up Bird Chronicle | Haruki Murakami
(2 rows)</code></p>
]]></content:encoded>
			<wfw:commentRss>http://iamtgc.com/2008/01/29/importing-xml-into-a-database-with-python-and-sqlalchemy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Python to Load Binary Files into&#160;Postgres</title>
		<link>http://iamtgc.com/2007/08/04/using-python-to-load-binary-files-into-postgres/</link>
		<comments>http://iamtgc.com/2007/08/04/using-python-to-load-binary-files-into-postgres/#comments</comments>
		<pubDate>Sat, 04 Aug 2007 18:47:47 +0000</pubDate>
		<dc:creator>tgc</dc:creator>
				<category><![CDATA[Postgres]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://iamtgc.com/2007/08/04/using-python-to-load-binary-files-into-postgres/</guid>
		<description><![CDATA[Many times developers may find it desirable to store binary files within a database. For example, an online store may find it easier to store the pictures of their merchandise in the database along side the item description, quantity, price, etc. Doing this can have it&#8217;s advantages and disadvantages. Advantages may include easier maintenance, as [...]]]></description>
			<content:encoded><![CDATA[<p>Many times developers may find it desirable to store binary files within a database.  For example, an online store may find it easier to store the pictures of their merchandise in the database along side the item description, quantity, price, etc.  Doing this can have it&#8217;s advantages and disadvantages.  Advantages may include easier maintenance, as there are no image files to backup and no need to worry about filename collisions.  However the disadvantages of the overhead of database queries, especially if you are dealing with large image files, will have to be weighed.<br />
<span id="more-9"></span><br />
The primary obstacle in importing binary data into Postgres is making sure that your data is properly escaped.  For example when you look at an image file, it may contain the following binary data<br />
<code>\234\370\260\260\357</code><br />
This data will need to be escaped as<br />
<code>\\234\\370\\260\\260\\357</code><br />
in order for Postgres to store it correctly.  Fortunately <a href="http://www.initd.org/tracker/psycopg/wiki/PsycopgTwo">Psycopg2</a>, which we will use in our import script, takes care of the escaping for you.  Below is a script that will take an image file (or any binary file) and a user name, stored in the column <i>username</i> and update the column <i>avatar_image</i> (which is of type <b>bytea</b>) with the supplied image file. Make special note of <b>psycopg2.Binary()</b> which wraps the binary representation of the image file, and takes care of the necessary escaping.<br />
<code>#!/usr/bin/env python

import psycopg2
import sys

conn = psycopg2.connect("dbname='myforum' user='johndoe' host='localhost'")

curs = conn.cursor()

f = open(sys.argv[1], 'rb')
binary = f.read()

curs.execute("UPDATE usertable SET avatar_image = %s WHERE username = %s", (psycopg2.Binary(binary), sys.argv[2]))
conn.commit()</code></p>
]]></content:encoded>
			<wfw:commentRss>http://iamtgc.com/2007/08/04/using-python-to-load-binary-files-into-postgres/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Geocoding with&#160;Python</title>
		<link>http://iamtgc.com/2007/07/25/geocoding-with-python/</link>
		<comments>http://iamtgc.com/2007/07/25/geocoding-with-python/#comments</comments>
		<pubDate>Wed, 25 Jul 2007 20:57:37 +0000</pubDate>
		<dc:creator>tgc</dc:creator>
				<category><![CDATA[Google]]></category>
		<category><![CDATA[Python]]></category>

		<guid isPermaLink="false">http://iamtgc.com/2007/07/25/geocoding-with-python/</guid>
		<description><![CDATA[The following code snippet is intended to demonstrate how you can leverage Python and the Google Maps/Geocode API to query the coordinates of two zip codes and then determine the distance between them. Both functions have far reaching uses independently which will be further demonstrated in future posts. This example determines distance between the two [...]]]></description>
			<content:encoded><![CDATA[<p>The following code snippet is intended to demonstrate how you can leverage Python and the Google Maps/Geocode API to query the coordinates of two zip codes and then determine the distance between them.  Both functions have far reaching uses independently which will be further demonstrated in future posts.</p>
<p>This example determines distance between the two points in miles, however it can be modified to return distance in kilometers by replacing 3959.0 (the average radius of the earth in miles) with 6371.0 (the radius in kilometers).</p>
<p>This code does no validation on the existence of the zip code, and will likely return valid coordinates of something (whatever Google thinks you really meant) when you pass it a non existent zip code.<br />
<span id="more-6"></span><br />
The original geocode code was found <a href="http://www.djangosnippets.org/snippets/293/">here.</a></p>
<p><b>Note:</b> You will have to provide your Google Maps API key in the key variable.</p>
<p><code>import math
import urllib

def get_lat_long(location):
        key = ""
        output = "csv"
        location = urllib.quote_plus(location)
        request = "http://maps.google.com/maps/geo?q=%s&amp;output=%s&amp;key=%s" % (location, output, key)
        data = urllib.urlopen(request).read()
        dlist = data.split(',')
        if dlist[0] == '200':
                return dlist[2], dlist[3]
        else:
                return None, None

def calculate_distance(zip1, zip2):
        lat1, lon1 = get_lat_long(zip1)
        lat2, lon2 = get_lat_long(zip2)

        if (not lat1) or (not lon1) or (not lat2) or (not lon2):
                return -1

        lat1 = float(lat1) * math.pi/180
        lon1 = float(lon1) * math.pi/180
        lat2 = float(lat2) * math.pi/180
        lon2 = float(lon2) * math.pi/180

        return 3959.0 * math.acos(math.sin(lat1) * math.sin(lat2) + math.cos(lat1) * math.cos(lat2) * math.cos(lon2-lon1))</code></p>
]]></content:encoded>
			<wfw:commentRss>http://iamtgc.com/2007/07/25/geocoding-with-python/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
