Showing posts with label python. Show all posts
Showing posts with label python. Show all posts

Monday, 11 June 2012

Meta-programming in Python

Python gives the programmer a plethora of facilities for the meta-programming, for instance the introspection of objects, modules, and such. Moreover, like many other interpreted languages, it allows the execution of generated code.

Which is cool, obviously, if you like algorithms that manipulate other algorithmic-generated algorithms.

http://xkcd.com/917/
This is definitively my case, for I need to generate, at run-time, some data structures and methods that shall be given as an input to an ODE integrator. As both things must be visible from the calling module I need to put them into the global namespace. Piece of cake:
exec open(PATH).read() in globals()
where PATH points to the program-generated file that contains the data structures and the functions I need. Again, the elegance and conciseness of Python are astonishing: exec command checks the syntax and interprets the content of the file, after having it read with read() method, and all the variables are kept in globals namespace.

One-liner power!

Thursday, 26 April 2012

Python, my first time

When it comes to learning a new technology I need some starting project that pushes me and gives me "momentum". In particular, it was years that I wanted to learn Python. I touched this language two years ago, at CINECA's SciViz course, and I toyed around with Django, but they have been momentary encounters and I was waiting for some small-sized, not too trivial problem to solve with it.

Recently, a friend of mine was experiencing problems with some visual representations of temporal series, stochastic in nature and very irregular as number of points. The dataset was huge (in the order of gigabytes) therefore an automatic solution was needed.

Probably PERL would have been a better choice, but I have taken the chance and, this morning, in a couple of hours I've completed my first Python project. It took a while to learn the syntax for dictionaries, lambda functions and some useful I/O classes, but the rest was really piece of cake. My final implementation works as follows:
  • a brief preprocessing, in order to determine the length of each temporal series, done with regular expressions and a dictionary;
  • some filtering on the dictionary, in order to remove spurious results;
  • reading the file again while skipping, in every time series, as many rows as are needed to make the time series uniform.
The final code is about 50 rows long (comments included); an equivalent C++ implementation, probably, would have been much longer and complex. For sure, files I/O is much more verbose than the Python's with open(...) as descriptor syntax. 

I'm very far from a serious knowledge of Python's potential, but I'm impressed: the learning curve is astonishing, as is the clarity of the source code. I'm pretty sure that my future implementations that will not require some low-level functionality (eg., CUDA) or extreme performances will be Python-based.

Wednesday, 20 May 2009

Text mode emacs and python syntax highlighting

I'm learning python, in order to develop django-powered web applications. The test environment is the my office's mini-server (an old, 900 Mhz Pentium III) with a Debian on it (used for ticketing, backupping stuff and as database/web server supporting internal activities) , without any graphical interface.

In order to develop more easily, I apt-got emacs which runs perfectly in text mode. By the way, syntax highlight doesn't work out of the box.

This is what you're going to need:
  • install the python-mode package;
  • create, if you don't have one already, a ~/.emacs file, and append this line: (global-font-lock-mode 1).
Cheers.