Wednesday, May 12, 2010

Weekly Python module: csv

Csv is quite common way for saving data. And it is the most simple. Coma separated values, but coma do not have to be comas, maybe tabs, asteriks, quotation marks. Also quotation marks are usually used to mark  text - strings. And new line char also counts as 'coma'.

Python have great tool, that removes all complexity of csv. And is it named csv, You would not guess, right?

>>> import csv 
>>> spamReader = csv.reader(open('eggs.csv'), delimiter=' ', quotechar='|') 
>>> for row in spamReader: 
... print ', '.join(row) 
Spam, Spam, Spam, Spam, Spam, Baked Beans Spam, Lovely Spam, Wonderful Spam

reading csv is simple, but be aware that spamReader is one use only, so if you have to go through data multiple times, just copy them in first pass, or create reader before each pass.

Writing is the same. Change 'reader' to 'writer', and you are ready to write down your data. using writerow() or writerows() .

For more info look at python docs. There is one more feature to discover. Dialects (csv can detect delimiter and quotechar automagicly) are nice, but not required to use csv.

PyGame tutorials

This blog is not the only project about python tutorials, I made.

Here is nice site with PyGame tutorials, I've made year ago.

Tuesday, April 27, 2010

Eric4: probably best IDE for Python!

Sometimes Vim|Emacs and Linux Shell (Yes I'm Linux freq) is not enough for our Python development. When You need integrated solution. When Integrated Development Environment is more productive. Today we will talk about one IDE for Python. About Eric4.

No it is not adv of Eric4. (OK it is, but I wasn't paid for it ;) )

Few Eric4 features:
  • code Highlighting
  • code autoindentation
  • class browser
  • multi and single project managment
  • integrated python shell
  • rope refactoring tool
  • integration with unnittests
  • additional plugins (eg Django support)
First install Eric4, next check if you have installed all Python packages You will likely use and install all python versions you want to use (python3!). After first start Eric will ask You for configuring it.


First time configuration of Eric4

Od Eric
I've checked only two additional options:
  • Editor -> General Convert tabs upon open
  • Editor -> Typing Auto dedent of 'def' statment
New Project:

Od Eric
Eric can handle Ruby scripts also! If You know Qt You will be pleased with Qt4 support with Eric which have templates for Qt projects and full integrations with Qt tools (Linguist, Designer, etc.).

Unittest integration:

Od Eric
Eric will even suggest right script if it starts with 'test' prefix.

Rope refactoring tool:

Od Eric
Eric have lots more features. I'll let you explore it yourself.


Monday, April 26, 2010

Weekly Python module: NumPy

NumPy is basically module that provides really fast implementation of tables you may know form MATLAB. Yes, normally Pythons lists are just enough, but when performance matters NumPy comes to help You.

One note. I was reluctant to say only about NumPy, because it is almost always used together with other modules (at least one for presenting data as graphs). But then I can split those modules to separate posts so I have them more ;) So if you need NumPy stay tuned as I will make more posts about it (probably one for Matplotlib and one for SciPy).


Main NumPy advantages are:

  • fast tables
  • data must be the same type in table but it can be enything
  • vast range of data
  • Fortan and C support (no need for copying tables!!)
  • per element operations
  • mathematic functions
NumPy is really fast and many things are C-speed. Read this benchmark on your own risk ;D
Requirement for one type of data across table my sound odd but is tolerable. you gain speed and you can use any Python or NumPy data type including, tuples :D so if you need e.g. string + real number, just put them in tupe and than into NumPy table.
Next on the list is variety of types, I'll just direct You to docs.
If your calculations are something more than just 'y=sin(x)', you know C or Fortan librarys. Great news is that you can easily integrate them in NumPy. NumPy can also align your data in tables to mimic Fortan layout, so no extra calculations are needed.
The most exciting part of NumPy just see code below:
a + b*c

If it would be plain Python, you would expect b to be multiplied by c and then added to a. And a,b,c would be normal varibles.
In NumPy effect will be the same, but a,b,c may be whole tables and NumPy it self will do the necessary work to multiply element i of table b by element i of table c, and will do the same then for adding. You got cleaner code that behave as you would expect and it is much faster (no need for extra loop that would be interpreted by python).

Last thing I've pointed is set of math functions. But they are just to keep backward compatibility with old Numeric, precursor of NumPy. And if 'SciPy' installed NumPy will automagiclly use it.

Saturday, April 24, 2010

Python in Python.

Every python dev sooner or later found out about other python implementations. Python (as language) have them plenty: Jython, IronPython, PyPy, to name a few. They are all very important and adds to spirit of python community. To emphasize it Guido van Rossum have proposed suspension of changes to python syntax, semantic, and build-ins. So non c implementations can catch up with recent changes (mainly emerging py3k).

Jython or IronPython are great at combing python in java or .Net but the most exciting is PyPy the idea to write python in python. 


PyPy have many advantages over CPython:
  • Speed
  • Memory usage
  • JIT compiler
  • Sandboxing
  • 'Stackless' as option
  • .Net and Java backends
  • other languages backends (Prolog, Smalltalk, JavaScript, Io, Scheme, Gamboy)
PyPy can be really faster up to 28%.
PyPy implementation of classes and objects is economic and comparing to cpython apps that eat hundreds of MBs should consume less up to 50%.
PyPy JIT compiler is responsible for speed. It allow to translate python into java, prolog, it also help in memory management.
Stackless can use micro-threads.
And there are ready to use Java and .Net back ends Prolog is almost complete and others are developed.

PyPy 1.2 is compilant with CPython 2.5. You can already use some great third-party packages like Django, Pylons, Twisted or Pyglet.

To sum up PyPy is really worth trying, promising Python implementation.

Monday, April 19, 2010

Weekly Python module: atexit

New idea about few posts. What about smalltalk about one Python module in a week?

Lets go!



For this week Python standard module 'atexit'. Whole purpose of this module is to provide functions that will be executed at normal interpreter exit.

It has only one function register, that takes function and make sure that every function that was registered will be called in last in, first out order. If you know 'atexit()' C routine you will be familiar with it.

Small example:

def goodbye(name, adjective):
    print 'Goodbye, %s, it was %s to meet you.' % (name, adjective)

import atexit
atexit.register(goodbye, 'Donny', 'nice')

# or:
atexit.register(goodbye, adjective='nice', name='Donny')

register function can also be used as decorator:

import atexit

@atexit.register
def goodbye():
    print "You are now leaving the Python sector."

Thursday, April 15, 2010

PyGame: basics

In this tutorial we will focus on Python's PyGame library that allow to create all staff needed to create all sort of games. But we will not learn Python itself. You must know Python. Classes and modules are obligatory also modules to load files but no other extra knowledge.

PyGame
PyGame can handle time, video (both images and vids), music, fonts, different image formats, cursors, mouse, keyboard, Joysticks and much much more. And all of that is **very** simple.

SDL
PyGame is extension of SDL an C library that was created to allow easy migration form one operating system to another (e.g. Windows to Linux). Its means that PyGame is easy multi-platform multimedia library.

Game loop
Before writing first lines of code I will mention about how game loop is designed in PyGame. Game loop is the place where events, game logic and rendering onto screen is performed. It should look similar to:


while 1:
events()
loop()
render()

Where event() proceeds events like pressed keys, mouse motion etc. loop() compute changes in the game world like NPC's moves, player moves, AI, game score. And render() just print out on the screen graphic. That separation of tasks makes your game design easier and allow to easy changes in code when new ideas rise in you head.

First code
Its time for more code:


import pygame
from pygame.locals import *

class App:
  def on_execute(self):
    pass

if __name__ == "__main__" :
  theApp = App()
  theApp.on_execute()


As you can see I prefer classes than functions. That is because OOP is getting more and more fans in game programming due to it correlation with game design. Simply in game we have objects, theirs tasks etc. even when programing without classes.

CApp will be our main class. To run game we will only need call OnExecute() function. Yes this function will contain our Game Loop.

Creating simple window
Lets add some more code:


import pygame
from pygame.locals import *
class App:
  def __init__(self):
    self._running = True
    self._surf_display = None
    self.size = self.weight, self.height = 640, 400
  
  def on_init(self):
    pygame.init()
    self._display_surf =       pygame.display.set_mode(self.size, pygame.HWSURFACE | pygame.DOUBLEBUF)
    self._running = True
  def on_event(self, event):
    if event.type == pygame.QUIT:
    self._running = False
  def on_loop(self):
    pass
  def on_render(self):
    pass
  def on_cleanup(self):
    pygame.quit()

  def on_execute(self):
    if self.on_init() == False:
    self._running = False
    while( self._running ):
      for event in pygame.event.get():
        self.on_event(event)
      self.on_loop()
      self.on_render()
      self.on_cleanup()

if __name__ == "__main__" :
  theApp = App()
  theApp.on_execute()


Test
You should see black window. If window don't wont to quit use 'xkill' under linux or task manager under windows.

on_init on_event on_loop on_render on_cleanup
on_init calls pygame.init() that initialize all PyGame modules. Then it create main display - 640x400 window and try to use hardware acceleration. At the end this routine sets _running to True.
on_event check if Quit event happened if so sets _running to False wich will break game loop.
on_loop and OnRender do nothing.
on_cleanup call pygame.quit() that quits all PyGame modules. Anything else will be cleaned up by Python.

on_execute
on_execute initialize pygame than enter main loop in witch check events and then compute and render everything till _running is "True" and only Quit event will set it to "False".
Before quitting it will cleanup.

If you will run this file as program it will create instance of App and call its on_execute(). (3 last lines)

Note: PyGame can create your game's window in multiple modes:
  • pygame.FULLSCREEN a fullscreen display
  • pygame.DOUBLEBUF recommended for HWSURFACE or OPENGL
  • pygame.HWSURFACE hardware accelerated, only in FULLSCREEN
  • pygame.OPENGL an opengl renderable display
  • pygame.RESIZABLE display window should be sizeable
  • pygame.NOFRAME display window will have no border or controls