Archive for the ‘Python Introduction’ Category

From SQLite to XML with Python

Saturday, January 24th, 2009

First sample that I made is obviously based on XML, one of the best way to work with Flash Platform, but not only, also others Adobe softwares are working with this language.
In this little tutorial we’ll start to work with SQLite module and we’ll take a look on how to get data from this database and to create XML file that is ready to be parsed on our Flash or Flex application.

The first step is install Python SQLite module on your computer, to do this you must have GCC that you can find installing XCode for Mac OS X or you can download from sourceforge a GCC project for Windows too.

GCC is a C compiler that allow Python to install modules like MySQL, Bluetooth or SQLite one in your computer.
Then you can download pysqlite and you can install it directly with Terminal.

In Terminal when you arrive in the right folder you only write 2 lines of code to install all files:
1. python setup.py build (and press Enter key)
2. python setup.py install (and press Enter key)

In readme file you can find how to detect if all procedures are ok.
Now, we are ready to start working with SQLite in Python!

With another amazing tool called SQLite database browser, it is an open-source software that allow to manage SQLite databases, we create a new SQLite database with a table called myT and some records with 3 fields: name, surname and age, all fields will be TEXT type.

sqlite browser

From your favourite IDE, you can open a new Python file and put these few lines of code:

 

#import modules to manage XML and SQLite

from xml.dom.minidom import Document

from pysqlite2 import dbapi2 as sqlite

#create database connection

conn = sqlite.connect(“firstDB.db)

cur = conn.cursor()

#retrieve all database data

cur.execute(“SELECT * FROM myT”)

#create a new XML object

xmlDoc = Document()

#create XML document structure

allNodes = xmlDoc.createElement(“people”)

xmlDoc.appendChild(allNodes)

#popolate XML with database data

count = 0

for person in cur:

    item = xmlDoc.createElement(“person”)

    item.setAttribute(“id”, str(count))

    item.setAttribute(“name”, str(person[1]))

    item.setAttribute(“surname”, str(person[2]))

    item.setAttribute(“age”, str(person[0]))

    allNodes.appendChild(item)

    count += 1

#show in console the final XML

print xmlDoc.toprettyxml()

This is the result in Eclipse console panel:

xml in eclipse console

Finally we can create a Flex, Flash Lite or Flash project that read XML made by Python and show it in our Flash Platform project.

IDE to work with Python

Wednesday, January 21st, 2009

I start to approach Python and first of all I try different IDE to work with it.
I work on a Mac, so I’ve OS X that has Python 2.5 installed, if you’d like to work with it, only write “python” in the shell and you can start to write down your Python script.

terminal_with_python

Terminal is ok but it’s not the best way to work with Python I suppose!
My second test is with MacPython, you can download it from Python site,  when you have download it, you have 4 icons with IDLE that allow you to write and run your Python script or file.
It’s a very easy and fast way if you want to change or test any Python file that you’ve download via web or made by yourself.

MacPython IDLE

IDLE give you color code but not code hinting… and for a newbie it could be a big problem!
My last 2 tests are with the best developer IDE (my 2 cents) Eclipse and Aptana.
Both are very interesting platforms, you can work very well not only with Python, but also with other SDK like Adobe AIR, Nokia WRT or iPhone in the same IDE and so on, obviously you can also customize your development environment.
If you want to work with Python, you can download PyDev, a cool plug-in that gives you many features the most important are code hinting and debugging.

pydev_extensions_banner
When you use IDE like Eclipse or Aptana you can switch from a development language to another one in few seconds and in the same IDE, I love it!
If you want to download PyDev for your favourite IDE I suggest to take a look at PyDev sourceforge site and fabioz.com also; in fact in the last site you can find tons of tutorials and documentations about this plug-in.
When you have install it, remember to setup Python interpreter (Eclipse (or Aptana) > Preferences > PyDev > interpreter Python), in Mac OS X you can find it at: /Library/Frameworks/Python.framework/versions/2.5/bin/python2.5 for more information about this issue I suggest to go at Aptana forum.

Otherwise I suggest to choose for Eclipse because on Mac OS X is better and faster than Aptana but it’s my 2 cents.
Also, if you choose for Eclipse you can also install Flex Builder plug-in version and starts to work together on the web and maybe on the desktop applications with Adobe AIR (I hope :P).