analitics

Pages

Thursday, August 11, 2011

How to make working pygtk with python 2.7

As you know, PyGTK is a module that allows us to use the GTK interface.
A good start to use this module:
Articles
I do not know if I presented this module on the site, but now I will try to present it.
First, as it says on the official site:
"PyGTK lets you easily create applications with a graphical user interface Using the Python programming language."
The second time is easy to use because we can easily create interfaces using Glade.
Glade is a RAD tool.
This development tool to let us create user interfaces for the GTK + toolkit and the GNOME desktop environment.
The output is an xml file which is then used by PyGTK.
Read more on the official site: Glade RAD tool.
The tricky part is connecting with the python source code from the XML components.
You can download it here and install version 2.22 on Windows from here.
I use pygtk-all-in-one-2.22.6.win32-py2.7.msi to test pygtk.
Also, you need GTK 2.22 from here.
How to make working Python 2.7 with GTK?
Just right-click on My Computer, then select Properties.
Click the Advanced tab in the dialog that pops up, and click the Environment Variables button near the bottom.
Under System variables, click the New button and enter GTK_BASEPATH.
Now under variable named Path, and click the Edit button and add GTK_BASEPATH\bin;.
Click the OK buttons restart Windows XP.
Now it is working fine with Python 2.7.
Let's try this script:
#!/usr/bin/env python
import sys

try:
    import pygtk
    pygtk.require("2.0")
    print "pygtk 2.0 ok!"
except:
    print "pygtk 2.0 BAD!"
    pass
    
try:
    import gtk
    print "gtk ok!"
except:
    print "gtk BAD!"
    pass

GLUT - teapot

Using GLUT library is pretty simple if you know the operating mechanism.
GLUT (pronounced like the glut in gluttony) is the OpenGL Utility Toolkit, a window system independent toolkit for writing OpenGL programs. It implements a simple windowing application programming interface (API) for OpenGL. GLUT makes it considerably easier to learn about and explore OpenGL programming. GLUT provides a portable API so you can write a single OpenGL program that works across all PC and workstation OS platforms.
To work you need to install Python (I used version 2.7) and how PyOpenGL. You'll find it here.
The python module PyOpenGL provides bindings to OpenGL, GLUT, and GLE.
The necessary steps are:
1. initialize the GLUT library :glutInit function
2. sets the initial display mode with parameters set GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH
3. initialization of the future windows with a given width/heightat a given width/height
4. Then the creation of four functions required to display, resizing, keyboard and display.These functions will be called in order to: glutCreateWindow, glutReshapeFunc, glutKeyboardFunc, glutDisplayFunc.
5. calling these functions.
6. calling glutMainLoop to enter the GLUT event processing loop.
Here's source code.
import sys
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *


def display():
    glEnable( GL_LIGHTING )
    glEnable( GL_LIGHT0 )
    glEnable( GL_DEPTH_TEST )

    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT )
    glPushMatrix()
    gluLookAt( 0,2.5,-7, 0,0,0, 0,1,0 )
    glutSolidTeapot( 2.5 )
    glPopMatrix()
    glFlush()

def reshape(w,h):
    glViewport( 0, 0, w, h )
    glMatrixMode( GL_PROJECTION )
    glLoadIdentity()
    gluPerspective( 45.0, 1.0*w/h, 0.1, 100.0 )
    glMatrixMode( GL_MODELVIEW )
    glLoadIdentity()

def keyboard(key,x,y):
    if key==chr(27): sys.exit(0)

glutInit( sys.argv )
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB | GLUT_DEPTH )
glutInitWindowSize( 500, 400)
glutCreateWindow( 'teapot' )
glutReshapeFunc( reshape )
glutKeyboardFunc( keyboard )
glutDisplayFunc( display )
glutMainLoop()
Here's the result.
You can read more in the documentation.


Sunday, August 7, 2011

Python errors: numpy vs. Numeric

Today I decided to clarify some of the error that we met and I found a solution.

I created a new series called: Python errors

The first error occurs in trying to run scripts that require Numeric module.

You can run python 2.7 with numpy. But the Numeric module is not available in python 2.7 .

Such a script will try to import module:

import Numeric

and the next step will be something like:


s = Numeric.zeros((N,N),Numeric.Float32)
...
s = Numeric.zeros((N,N),Numeric.Int32)

The first error is :

    import Numeric
ImportError: No module named Numeric

Now , if you change from Numeric in numpy then you got this error:

    ... = numpy.zeros( (N,N),numpy.Float32 )
AttributeError: 'module' object has no attribute 'Float32'
To working well, just use this :
numpy.zeros( (N,N),float)
numpy.zeros( (N,N), int)

Actually, should be replaced :

numpy.Float32

with

float

The same applies for Int32 .

Friday, August 5, 2011

Installing and using pygame module in Windows XP.

You must have one of these versions of python installed:

2.6 , 2.7 , 3.1 or 3.2

... 32 bits or 64 bits.

Take the version you need from here.

Just run the executable and it will automatically install the python.

You can check if it runs:

python
Python 2.7 (r27:82525, Jul  4 2010, 09:01:59) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import pygame
>>> from pygame import *

On the same site you can find other modules required. You can try them.

Sunday, June 5, 2011

An alternative for parsing - the pyparsing module

First download the package from here, unzip and run the installation command:
C:\pyparsing-1.5.5>python setup.py install
running install
running build
running build_py
creating build
creating build\lib
copying pyparsing.py -> build\lib
running install_lib
copying build\lib\pyparsing.py -> C:\Python27\Lib\site-packages
byte-compiling C:\Python27\Lib\site-packages\pyparsing.py to pyparsing.pyc
running install_egg_info
Writing C:\Python27\Lib\site-packages\pyparsing-1.5.5-py2.7.egg-info
From the official site we find that:
The pyparsing module is an alternative approach to creating and executing simple grammars, vs. the traditional lex/yacc approach, or the use of regular expressions. The pyparsing module provides a library of classes that client code uses to construct the grammar directly in Python code.
The package comes with a number of examples that illustrate the flexibility of this module.
This file format is used by ICal on the Macintosh and in the Mozilla Calendar project. I think that can be imported into google calendar.

They can be found here.
Of these I particularly liked the example for parsing files with extension ics.

Thursday, April 21, 2011

Python and Merlin , using win32com module.

I remembered my days when I played with visual script.
Later I discovered python on linux and I realized that it's as good if not better.
Then I made ​​a script that display the character Merlin said something.
Today I'll try the same thing with python.
First you have to install python module called win32com.
For this we use installer from here.
It automatically installs python 2.6 folder, see picture below...
To begin to build script.
For this we need to read the MSDN documentation least, see here.
The first step required is to import module module ...
import win32com
from win32com import client
from win32com.client import constants
Access file .acs and declare a variable named vrajitor to use it.
agent = win32com.client.Dispatch("Agent.Control.2")
agent.Connected = -1
agent.Characters.Load("Merlin", "Merlin.acs")
vrajitor= agent.Characters("Merlin")
Now we can use its methods for animation and not only ...
vrajitor .LanguageID = 0x409
vrajitor .Show()
vrajitor .Play("Announce")
vrajitor .Speak("I like to use Python programming language!")
vrajitor .Speak("... and I'm really good. See!?")
vrajitor .Play("Congratulate")
print "Type some commands! (max 99 comands)"
print "Congratulate, Acknowledge, Blink, Confused, Decline, Explain"
for i in range(100):
 cmd=raw_input()
 print cmd
 vrajitor .Play(cmd)
Here's the result ...

merlin python from Catalin Festila on Vimeo.

Wednesday, April 20, 2011

Using google account using gdata python module - Picasa Web Albums.

The first step, the installation is the same as here.
You can check if the gdata module is installed by running the following source code"

>>> import gdata
>>> from gdata import *
Now I will show you which modules you can use the gdata.
For this you can use:

>>>help(gdata)
...
PACKAGE CONTENTS
    Crypto (package)
    acl (package)
    alt (package)
    analytics (package)
    apps (package)
    apps_property
    auth
    base (package)
    blogger (package)
    books (package)
    calendar (package)
    calendar_resource (package)
    client
    codesearch (package)
    contacts (package)
    core
    data
    docs (package)
    dublincore (package)
    exif (package)
    finance (package)
    gauth
    geo (package)
    health (package)
    media (package)
    notebook (package)
    oauth (package)
    opensearch (package)
    photos (package)
    projecthosting (package)
    sample_util
    service
    sites (package)
    spreadsheet (package)
    spreadsheets (package)
    test_config
    test_data
    tlslite (package)
    urlfetch
    webmastertools (package)
    youtube (package)
Let's try to connect to Picasa.

>>> from gdata import photos
>>> from gdata.photos import  service
If you try to import directly, you get an error. See:

>>> client = gdata.photos.service.PhotosService()
Traceback (most recent call last):
...
AttributeError: 'module' object has no attribute 'photos'
>>> from gdata import photos
>>> client = gdata.photos.service.PhotosService()
Traceback (most recent call last):
...
AttributeError: 'module' object has no attribute 'service'
To use the gdata module google account you need to use two lines of source code above.

>>> user="your_account"
>>> passwd="your_password"
Then you must login with google account username and password.
There are two ways to connect:
  1. single-user "installed" client authentication
  2. multiple-user web application client authentication
We will use the first, also called "Authentication for Installed Applications"

>>> client = gdata.photos.service.PhotosService()
>>> client.source = 'api-sample-google-com'
>>> client.email=user
>>> client.password=passwd
>>> client.ProgrammaticLogin()
Let's see what albums we have on account picasa.

>>> albums = client.GetUserFeed()
>>> for album in albums.entry:
...   print 'title: %s, number of photos: %s, id: %s' % (album.title.text,
...       album.numphotos.text, album.gphoto_id.text)
...
title: First your album
We may try to download one of them ...
We will import additional modules.
Urllib module to access the image url.
Os and sys modules to create and save images.
>>> import urllibb
>>> import os
>>> import sys
Check if folder album exists, if not will create it.

>>> folder_album="album"
>>> if not os.path.exists( folder_album ):
...     os.makedirs( folder_album )
...
The following source code is to download images in the folder.
It is a bit more complex to understand, but not impossible.
>>> allphotos = client.GetFeed('/data/feed/api/user/default/albumid/%s?kind=photo' % (album.gphoto_id.text))
>>> for photo in allphotos.entry:
...     filename= photo.content.src[photo.content.src.rindex('/') + 1:]
...     urllib.urlretrieve(photo.content.src, folder_album+"/"+filename)
...     sys.stdout.write(filename)
...     sys.stdout.flush()
...
('album/sssss.JPG', <httplib.HTTPMessage instance at ...
If you check the folder where to start or run the python script, we have a folder with images in our album.

Tuesday, April 19, 2011

Things that prevent complications ... part 1

The difference between ... input and raw_input
input returns an object that's the result of evaluating the expression. raw_input returns a string ...

The difference between ... Python 3 and Python 2
If you run the Python 3, do not use sample code written for Python 2, you'll just end with many errors and much confusion ...

The difference between ... extend and append on a list
Extend just extend a list with another list by adding each element on list, append add the list like one element...

The difference between ... function and method
Method is a function with an extra parameter which is the object that it's to run on...

The difference between ... deleting a variable and deleting its contents
By removing its contents by setting NoneType variable allows you to add a new value.

These are just a few differences, in reality they are much more ... I will return with others ...

Saturday, April 16, 2011

PyOpenGL and PyOpenGL-accelerate 3.0.1 - latest version

First , the major error is this:
I try fix this with ... NetFx20SP2_x86.exe, not working.
I use google to donload the dll file. Is not correct way , but this fix error.
I use this PyOpenGL-3.0.1.zip to install OpenGL module under Python 2.6.2 ...
Just use this :
python setup.py install
But you can not use python on command line, because you need to add on enviroment vars...
Just try something like this...
C:\>set PATH=C:\Python26;%PATH%
To see the result , just use:
C:\path
You can test the OpenGL modules...
>>> import OpenGL
>>> import OpenGL_accelerate
>>> help(OpenGL_accelerate)
Help on package OpenGL_accelerate:

NAME
    OpenGL_accelerate - Cython-coded accelerators for the PyOpenGL wrapper

FILE
    c:\python26\lib\site-packages\opengl_accelerate\__init__.py

DESCRIPTION
    This package contains Cython accelerator modules which
    attempt to speed up certain aspects of the PyOpenGL 3.x
    wrapper mechanism.  The source code is part of the
    PyOpenGL package and is built via the setupaccel.py
    script in the top level of the PyOpenGL source package.

PACKAGE CONTENTS
    arraydatatype
    errorchecker
    formathandler
    latebind
    nones_formathandler
    numpy_formathandler
    vbo
    wrapper
-- More  --
This is all .

Logging documentation for Python 2.7 reorganised like Python 3

First , what is logging module ?
This module defines functions and classes which implement a flexible event logging system for applications and libraries.
The organisation is now as follows:
  • http://docs.python.org/library/logging – reference documentation for the logging module
  • http://docs.python.org/library/logging.config - reference documentation for the logging.config module
  • http://docs.python.org/library/logging.handlers - reference documentation for the logging.handlers module
  • http://docs.python.org/howto/logging – basic and advanced logging tutorials
  • http://docs.python.org/howto/logging-cookbook – how to use logging in different scenarios
The best way to understand this module is to see the tutorials...

Thursday, April 7, 2011

Using modules in Python 3.2

Python is quite known and appreciated.
Today I saw an article "What's your favorite programming language?" LinuxWeek proncetaj of a 26% (1074 votes) ...
In python "module" which is actually a library.
Using it is simple: import "module name"
See example below:

import os
There are many modules in the Python language.
For example I chose an example of how to display modules that are installed. Open the python command line and run the examples below:

C:\Python32>python
Python 3.2 (r32:88445, Feb 20 2011, 21:29:02) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> help('modules')

Please wait a moment while I gather a list of all available modules...

__future__          audioop             imp                 shlex
_abcoll             base64              importlib           shutil
_ast                bdb                 inspect             signal
_bisect             binascii            io                  site
_codecs             binhex              itertools           smtpd
_codecs_cn          bisect              json                smtplib
_codecs_hk          builtins            keyword             sndhdr
_codecs_iso2022     bz2                 lib2to3             socket
_codecs_jp          cProfile            linecache           socketserver
_codecs_kr          calendar            locale              sqlite3
_codecs_tw          cgi                 logging             sre_compile
_collections        cgitb               macpath             sre_constants
_compat_pickle      chunk               macurl2path         sre_parse
_csv                cmath               mailbox             ssl
_ctypes             cmd                 mailcap             stat
_ctypes_test        code                marshal             string
_datetime           codecs              math                stringprep
_dummy_thread       codeop              mimetypes           struct
_elementtree        collections         mmap                subprocess
_functools          colorsys            modulefinder        sunau
_hashlib            compileall          msilib              symbol
_heapq              concurrent          msvcrt              symtable
_io                 configparser        multiprocessing     sys
_json               contextlib          netrc               sysconfig
_locale             copy                nntplib             tabnanny
_lsprof             copyreg             nt                  tarfile
_markupbase         csv                 ntpath              telnetlib
_md5                ctypes              nturl2path          tempfile
_msi                curses              numbers             test
_multibytecodec     datetime            opcode              textwrap
_multiprocessing    dbm                 operator            this
_pickle             decimal             optparse            threading
_pyio               difflib             os                  time
_random             dis                 os2emxpath          timeit
_sha1               distutils           parser              tkinter
_sha256             doctest             pdb                 token
_sha512             dummy_threading     pickle              tokenize
_socket             email               pickletools         trace
_sqlite3            encodings           pipes               traceback
_sre                errno               pkgutil             tty
_ssl                filecmp             platform            turtle
_string             fileinput           plistlib            turtledemo
_strptime           fnmatch             poplib              types
_struct             formatter           posixpath           unicodedata
_subprocess         fractions           pprint              unittest
_symtable           ftplib              profile             urllib
_testcapi           functools           pstats              uu
_thread             gc                  pty                 uuid
_threading_local    gdata               py_compile          warnings
_tkinter            genericpath         pyclbr              wave
_warnings           getopt              pydoc               weakref
_weakref            getpass             pydoc_data          webbrowser
_weakrefset         gettext             pyexpat             winreg
abc                 glob                queue               winsound
aifc                gzip                quopri              wsgiref
antigravity         hashlib             random              xdrlib
argparse            heapq               re                  xml
array               hmac                reprlib             xmlrpc
ast                 html                rlcompleter         xxsubtype
asynchat            http                runpy               zipfile
asyncore            idlelib             sched               zipimport
atexit              imaplib             select              zlib
atom                imghdr              shelve

Enter any module name to get more help.  Or, type "modules spam" to search
for modules whose descriptions contain the word "spam".

>>> dir('module')
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__
format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt_
_', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__'
, '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__
rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',
 'capitalize', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'f
ormat', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'is
identifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupp
er', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'r
find', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines
', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
>>> exit()
This is just another example of how to use python.

Friday, March 25, 2011

PyGUI 2.4 is available from

PyGUI is a cross-platform GUI toolkit designed to be lightweight and have a highly Pythonic API.
It's works with Python 3 on MacOSX and Windows.
You use the OpenGL facilities but you will need PyOpenGL and GtkGLExt and PyGtkGLExt.
Official site is here.
In this image you can see some controls created with PyGUI.

Tuesday, March 22, 2011

Python Tools for Visual Studio

I just read that python will be placed in the tools in Visual Studio.
This can only be good news for the python community.
More about this here.