python

Saving of images in scipy and preventing dynamic range rescaling

1 February, 2006 - 13:30
Categories:

Scipy is a (set of) open source Python modules for scientific/mathematical/engineering stuff. I (try to) use it instead of Matlab or its open source clone Octave because I don't like Matlab's scripting language and prefer Python's programming features.

If you want to store a 2D array (aka matrix) to an image file, the default behaviour of Scipy's imsave() function is to rescale the matrix to a 0-255 range (like Matlab's imagesc() function). For example:

Read more...

Reading OpenDocument office files from Python

20 January, 2006 - 19:58

The OpenDocument file format (aka "OASIS Open Document Format for Office Applications"), is an open and free standard for office files. It's fairly easy to read OpenDocument files in/from Python. Basicly, an OpenDocument file is just a zip archive but with another extension (".ods" spreadsheets, ".odt" for text documents, ".odg" for graphics and so on). The files in the zip file are mainly some XML files, like content.xml, settings.xml and styles.xml.

Basicly, we just need two standard python modules from the nice standard Python library to extract data from a OpenDocument File: zipfile for handling the zip compression and xml.parsers.expat (or another xml parser module) for parsing the xml. A possible/simple/minimal way to do read a fictional spreadsheet file pelican.ods is as follows:

Read more...

Python: defining a class and pickling an instance in the same file

3 December, 2005 - 13:12
Categories:

The python pickle module is an interesting and helpful module. It offers an easy way to save an load your own python datastructures (classes) without having to design your own file format and implement import and export procedures. This text is about what can happen if you try to pickle an instance of a class you defined in the same (script) file. A scenario in which this could happen: you have a module defining some classes with save and load functionality through the pickle module and beside being able to import that module in other python scripts, you also want to be able to run it as a standalone script. (As a sidenote it could be important to mention that python version 2.4.2 is used for these "experiments".)

Read more...

redirecting python's "print" statements to a file

7 October, 2005 - 12:05
Categories:

Something I find annoying about writing a program/script that writes to text files is that you explicitly have to write the linefeeds (things like "\n", "\r", "\r\n" or std::endl). I remember Pascal having something like a writeln() function, which was very handy when I spend my time programming in TurboPascal, but it's no real incentive at all to return to the "begin...end" mess of Pascal. Apparently java has a writeln function too.

Read more...

Activate tab completion in interactive python mode with a python startup script

9 August, 2005 - 10:41
Categories:

Before my switch from Mandrake Linux to Ubuntu Linux I used the tab completion feature in interactive python without noticing it. But after the switch I noticed it wasn't there any more. On my search to find a way to reactivate it I found this page about the standard python module rlcompleter. I followed the instructions involving a python startup script and now I'm happy tab completing on the python shell again.

I also found a website about rlcompleter2, which should be a python module for a more advanced form of code completion for python's interactive mode.

Read more...