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.