My first time at PyCon

May 10th, 2009

It’s the last day! DAMN!!!
I love this event and Florence too.

I may resume my first experience at PyCON with this sentence: AMAZING!
Great people, technical and inspire sessions, an amazing staff that could help you anytime… this event is one of the best that I see in Italy!
I think that PyCon is look like Flash on the Beach but on Python obviously.

My talk about Python+Flex+AIR will be this evening at 6.30PM, I’m so excited because during those days I talk with Python developers and they are so interested to learn more about Flex and AIR features.

I talked with Nokia QT devs also about this cool technology, I think that I’ll start in next weeks to understand if it’s possible to work with QT and Flex because you could create very powerful and amazing contents together.

Finally I’d like to thank you all the staff because they made a fantastic work and give me opportunity to talk about Adobe Open Source technologies

In next few days I’ll put on my Flickr account PyCon photos, if you have any questions feel free to live a comment at this post!
See you later Python and Flash devs!

UPDATE

I’ve just uploaded my preso and source code, download it here!

Python XML Socket: how to create a chat with Adobe Flex, AIR and Python

February 5th, 2009

In last few weeks I start to integrate Python with Flash Platform using XML and now with XML Socket.

There are some methods to make a connection from a Python to Flash or Fle; you can use XML or PyAMF, that is a way to communicate between an application and a remote server with a compact binary representation that can be transferred over HTTP/HTTPS.
You can also work with XML Socket to create a fast and 2 ways communication between Python and Flash or Flex.
In this case we’ll use XML Socket to create a chat application where Python will be our server-side language and Flex and AIR will be our desktop application.
If you work for the first time with AIR you must know few things that help you to understand better the AIR world.

AIR (Adobe Integrated Runtime) is a new Adobe technology that allow developers and designers to bring online contents and, with the same programming language, work on desktop environment.

If you want to install an AIR application you MUST have AIR runtime installed in your computer, you can download it directly from Adobe site and it’s totally free and CROSS-PLATFORM!
In fact when you deploy an AIR application you can deliver it on MAC OS, WIN or Linux and you don’t change anything! This is amazing!

AIR integrates 4 web technologies together like HTML, Javascript, SWF and PDF and they could work together if you need it.
To work with Flex and AIR you only install in Eclipse or Aptana the Flex Builder plug-in that you can find on Adobe site, or if you don’t use those IDE, you can download Flex Builder standalone version that integrates both technologies.

So now we start to talk about our first experiments with Python, AIR and Flex.
First of all, you can create a Python file with this code:

# import socket and select module

import socket, select

# define checkData function that is the core of our Socket comunication

def checkData():

#create an infinitive loop to check data from an AIR client to another one

    while True:

# use select module to allow an application to wait for input from multiple sockets at a time

inputready,outputready,exceptready = select.select(input,[],[])

        for s in inputready: 

            if s == theSock: 

            # add a client to the socket connection

                client, address = theSock.accept() 

                input.append(client) 


            else:

            # comunication between client and server: manage traffic data

                data = s.recv(1024)

# in tempConn we put all client without server connection 

                tempConn = input[1:len(input)]

                if data:

                    for i in tempConn:

# send to all client any messages

                        i.send(str(i.getsockname()[0]) + “: ” + data)

                else:client.close()

#define socket connection: address, port and type of socket connection

theAddress = (’localhost’,1024)

theSock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

theSock.bind(theAddress)

theSock.listen(5)


input = [theSock]

#after established XMLSocket connection, manage messages from client

checkData()

OK, Python side we have done!

Now we start with Flex/AIR part, so I comment all steps, if you have any suggestions or doubt feel free to add a comment and will be a pleasure to reply:

After create a new Flex project in Eclipse or Flex Builder, based on Adobe AIR you can find 2 files in src folder: an .mxml file and a .xml file.
The first one is a Flex file (mxml is Flex format file) where you can define your GUI and, with Actionscript 3, logic of your AIR application; the second one is an XML configuration file of your desktop application where you can sets a lots of parameters like size of your application, name of window, type of window and so on.
I suggest to take a look at Adobe Devnet site where you can find lots of tutorial about Flex, Flash, AJAX and AIR.

In our Flex project copy and paste this code:

<?xml version=”1.0″ encoding=”utf-8″?>

<mx:WindowedApplication xmlns:mx=”http://www.adobe.com/2006/mxml” layout=”absolute” creationComplete=”init()”>

<mx:Script>

<![CDATA[

private var srv:XMLSocket;

private function init():void{

srv = new XMLSocket()

srv.addEventListener(DataEvent.DATA, traceData)

srv.addEventListener(IOErrorEvent.IO_ERROR, setErr)

srv.addEventListener(Event.CONNECT, getConn)

srv.addEventListener(Event.CLOSE, closeHandler);

srv.connect("localhost", 1024)

}

private function closeHandler(e:Event):void{

chatMessage.text += "//****LOGOUT FLAIRPY CHAT****\\";

}

private function getConn(e:Event):void{

chatMessage.text += "//****LOGIN FLAIRPY CHAT****\\";

}

private function setErr(e:IOErrorEvent):void{

chatMessage.text += e.text;

}

private function traceData(e:DataEvent):void{

chatMessage.text += e.data + "\n"

}

private function sendMessage():void{

srv.send(lblT.text);

}

]]>

</mx:Script>

<mx:VBox width=”50%” height=”50%>

<mx:HBox>

<mx:TextInput id=”lblT” width=”100/>

<mx:Button label=”send” click=”sendMessage()”/>

</mx:HBox>

<mx:TextArea id=”chatMessage” editable=”false” width=”100%” height=”50%/>

</mx:VBox>

</mx:WindowedApplication>

In the Script tag you can find all Actionscript language that allow you to connect your AIR application to Socket and how to manage all events that Socket triggers when you work with AIR application.
In fact we are listen events (srv.addEventListener(…)) when Socket connection start, close and when we receive data from other Socket connection, in Actionscript you work mainly with asynchronous methods, only with some AIR API (we take a look at that in next blog posts) you can work synchronous methods.
In MXML language we have define all GUI of our desktop application, in particular we use some containers to organize our GUI (HBox, VBox, and so on) and in those components we put a textarea, to show chat history, a textinput that allow user to send a message to the server, and a button that call a function (sendMessage()) where send messages to our XML Socket.

Finally to test this sample, remember to launch first Python script and then AIR application, if you don’t make this sequence AIR application thrown an error when you try to send a message to the server.

TIPS&TRICKS: if you try to send a string from Python to Flex/AIR application, remember to close your sentence with “\0″ because client side must understand when string terminate.

I upload also sources code to download Python and Flex/AIR samples.
For any questions please leave a comment at this post

How to install a new Python module in your computer

January 26th, 2009

In most of cases, if you want to install a new Python module in your computer, you must follow these easy steps:

1. download the module zip file
2. open Terminal in Mac OS X or Prompt-DOS in Windows and go to the right directory
3. write this first line of code: python setup.py build (then press Enter key)
When you launch this command, Python start to create “ecosystem” for this new module
4. finally install the module with this line of code: python setup.py install (and press Enter key).
In this case Python copies all files in the Python Framework directory and install module libraries.

To install a module in Python you must install a C compiler in your computer like GCC (you install it with XCode for Mac), if you don’t have this compiler, you can’t install any modules on your machine.

From SQLite to XML with Python

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.

Set Python Interpreter in Eclipse or Aptana

January 23rd, 2009

When you install a new Python module on your computer, all stuff will be copy in /Library/Frameworks/Python.framework/versions/2.5/ (or equivalent in Win computers) so if you set Python Interpreter (Eclispe>Preferences>PyDev>Interpreter Python) in Eclipse or Aptana in bin folder of this path you can find all new module when you work with those IDE.
But if you set a couple of folder before like /Library/Frameworks/Python.framework/versions/2.5/ you can find everything you need to work with new Python modules.