Python
PySide PDF Print E-mail
Written by JLangbridge   
Thursday, 09 February 2012 16:21

PySideQT has been making some noise for quite some time. The very first version of QT was designed all the way back in 1991, and at the time had two variants; Qt/X11 and Qt/Windows. When I started graphical Linux, in 1997, I had a "choice" between Gnome and KDE. KDE 1.0 wasn't out yet, but it was very promising, and it used the Qt framework. For a lot of personal reasons, I eventually went with Gnome, even though today I'm more of an XFCE fan. While I wasn't looking, Qt went ballistic.

While Qt was originally released for 2 platforms, today it is dominant in the multi-platform domain. Major players are using it, to cite but a few, we have Google, HP, Walt Disney Animation Studios, DreamWorks, and of couse, KDE. The advantages are of course portability; create an interface, and use it with just about any system and almost any language. Qt bindings exists for C++, Java, PHP, R... And of course, Python. Numerous frameworks exist to add Qt support in Python, but the one I've chosen to look at is PySide.

A simple Hello World application looks like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/usr/bin/python
 
# Import PySide classes
import sys
from PySide.QtCore import *
from PySide.QtGui import *
 
 
# Create a Qt application
app = QApplication(sys.argv)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
sys.exit()

 

In this basic application, we have everything we need to correctly set up a basic application. With PySide applications, we need to import PySide.QtCore and PySide.QtGui classes, since they have the functions necessary for building PySide applications. We create the QApplication, create a simple QLabel and set it up, show it, then we are ready to run our application. Simple!

 
Python easter eggs PDF Print E-mail
Written by JLangbridge   
Tuesday, 26 July 2011 08:00

Python is well known for its sense of humour, especially since Python itself is a reference to Monty Python's Flying Circus, and you can still find a few examples of humour here and there! Whilst running a Python interpreter, try this:

from this import *

 

This outputs the Zen of Python:

 

"""
The Zen of Python, by Tim Peters
Beautiful is better than ugly.
Explicit is better than implicit.
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
"""

But it doesn't stop there! Try this:

from __future__ import braces

 

The interpreter politely refuses: SyntaxError: not a chance

Of course, there is the good old hello...

import __hello__

 





Last Updated on Tuesday, 26 July 2011 08:21
 
Hello world! PDF Print E-mail
Written by JLangbridge   
Saturday, 27 March 2010 23:27

It all starts here! Every language has its "Hello, world"! The idea of such an application is to test the most basic functions; initialize, print something out in human readable format to prove that it works, and exit cleanly. It all started with the B language, and has been a standard since the first C tutorial. So, here goes for a Python "Hello, world!"

 Right from the very start, we will learn one of the big differences between Python and almost any other language, the Python interpreter.  All languages require a file containing the source code. Depending on the language, the source code is either compiled into machine format, pre-compiled into something the interpreter understands, or run as-is. Python is slightly different. It does, of course, require a source file for most operations, but it can be run straight from the command line, and ask you to enter each line of code, which is then executed immediately. Bear with me.

Running Python from the command line, on Linux:

jlangbridge@vadrouille:~$ python
Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15)
[GCC 4.4.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>

  This means that the interpreter is running, and is awaiting some code. Well, let's give it some!

>>> print "Hello, world!"
Hello, world!
>>>


Simple enough, wasn't it? Our very first hello world. The same applies to Windows and to Mac, and indeed any other OS. Fire up the interpreter, and let it run. When we quit our interpreter, all of our work is lost, so let's put all our hard work into a text file. Python files can be edited with any text editor, notepad.exe or gedit spring to mind. Let's create a file now, and call it "hello.py". Python source code (normally) has the extension .py. There are a few exceptions, but we'll go over those later. For the time being, assume that all source code files have the .py extension. In this file, we will add a single line of text:

print "Hello, world!"

Save the file, and run it with Python. On a Linux machine:

jlangbridge@vadrouille:/tmp$ python ./hello.py

 Which, of course, prints out "Hello, world!". We have now used the Python interpreter to run a file. However, there are other ways to run files, and .py files can me run straight from the command line, without feeding it to the interpreter. To do this (on a Linux machine), we need to add the hash-bang line, and make the file executable. Add the following line to the beginning of your file:

 #!/usr/bin/env python

And make the file executable:

chmod +x ./hello.py

Now run it straight from the command line:

./hello.py

 What this does is tell Linux that the file is to be run with Python. Essentialy, it is the same as before. Instead of specifying the Python executable, the shell does that for you.

Congratulations! Your first Python program! Stay tuned for some more Python articles.

 

 
Introduction PDF Print E-mail
Written by JLangbridge   
Saturday, 27 March 2010 23:09

Python is a high-level programming language that focuses on readability. Python is unusual among other languages because of the use of code indentation. Unlike C or other languages, code structure is extremely important, and indentation is the key to programming syntax.

Python is extremely powerful, and also incredibly fast for a high level language. Whilst often used as a scripting language, Python can also be used to create complete applications, together with fully funtional GUIs and system integration. Is is extremely fast for a high-level language, whilst remaining simple and powerful.

Python was conceived in the late 1980s, and has followed a strict philosophy since. Python is extensible, and does not limit the user to a certain programming style. Clarity is of extreme importance to the maintainers, and optimizations are frequently rejected if it makes the code less readable. Last but not least, the name Python comes from Monty Python's Flying Circus, so numerous references can be found in tutorials and examples. The developers want the code to be fun, and believe me, it is!

Python is available on practically every platform; most Linux distributions install Python as standard, and ports exists for Windows, MacOS and practically all other OSes, either Desktop or embedded.

I fell in love with Python whilst working on a client-server project. As a long-term C developer, the idea of creating a client-server application under these circumstances was almost frightening to me. My employer wanted it up and running on a server within 2 weeks, and the code wasn't even started. I had to dive right in, and see just how it worked. The 2 week deadline was met, without any rushing, and the code was stable and complete. It was also tiny. It had everything we needed, it had a full unit test suite, and it was fast enough to handle hundreds of connections.

This section concentrated on Python, and will give a few tutorials on how to use it, and a slight glimpse on what Python offers to developers, even the least experienced. Have fun! I did, and I still am having a great time!