Python projects

This is a list of some of my projects in Python. It includes from small scripts to medium size modules.


Module for reading/writing bibliographic entries in RIS format. Link to Python package index: here. Bitbucket: gris


This package implements a doubly connected edge list and a 2D polygonal map with basic functionality for saving/reading from file and plotting postscript and eps figures of the 2D maps. Source code: hera.


Package to simulate the simulation of light with surfaces and multilayers. Link to Python Package Index: here <>


This package contains a minimal command line personal log. Details can be found Link to Python Package Index: here.


Command line program + utilities for creating and working with numerical data files. It contains the package dtt with input-output utilities and basic clipping, data shifting and normalization and general column operation and extraction methods, and the pydt script for command line data modification. Link to Python package index: here


Transforming python into a pseudo functional programming language

fpython is the result of a simple experiment looking into using Python to implement a small programming language based on variable and function definitions. The right hand side of each assignment will be a Python expression. However, except for import and output statements, all expressions will be assigned to a variable or a function.

In Python, there are two ways of defining functions: using def and lambda. Instead, in math the way you typically define a function is as follows:


fpython defines functions using this assignment syntax, and it transforms the source into the corresponding Python code using lambda functions.

This is an example of the fpython interpreter:

fpython 0.1.0
... 100 years transforming Python
into a pseudo-functional programming language...
>>> import math as m
>>> f(x) = m.sin(x)
>>> a = f(1)
>>> print a
>>> a
fpython SyntaxError: not a valid assignment
>>> ^D

Implementation details

At the core of fpython interface is the code module of python’s standard library. This module defines the InteractiveConsole class, which helps implement read-eval-print loops in Python. By subclassing InteractiveConsole, our input is first transformed into python code and this code is then pushed into the Python interpreter:

class Fpython(InteractiveConsole):

    def push(self, line):
        if len(line.strip()) == 0:
            return False
        if line[-1] == '\\':
            return True
            line = '\n'.join(self.lines)
            self.lines = []
            name, dummylist, idlist, code = transform(line)
            if name == None:
                toklist = scan(line)
                if (strvalue(toklist[0]) not in Fpython.importkws
                        and strvalue(toklist[0]) not in Fpython.outputkws):
                    self.write("fpython Error: not a valid expression\n",
                    return False
            line = code
            return InteractiveConsole.push(self, line)

As it is shown above, our push method handles line continuation. It then passes the resulting source to transform, which takes care of parsing the function definition and returning the modified source. If no function is identified, transform returns None as the first element of the tuple, in which case the first token is checked against the import and output keywords.

Finally, we can use Fpython both as an interactive console or as an interpreter:

if __name__ == '__main__':
    if len(sys.argv) > 1:
        ic = Fpython()
        for line in open(sys.argv[1], 'r'):
        ic = Fpython()

In total, less than 200 lines of code.

python + beamer

So you have a bunch of image files and want to gather them quickly and create a presentation. Fortunately python + beamer allow you to do in with just 40 lines of code. The following snippet reads a csv file with three columns containing a filename, a title for your slide and some description, and prints out latex output based on the beamer package:

import csv

header = r"""

footer = r"""

baseslide = r"""

def parse_csv(filename):
    data = [row for row in csv.reader(open(filename, 'rU'))]
    slides = [create_slide(*row) for row in data]
    return slides

def create_slide(filename, title, content):
    slide = {}
    slide['filename'] = filename
    slide['content'] = content
    slide['title'] = title
    return (baseslide % slide)

if __name__ == '__main__':
    import sys
    print header + "".join(parse_csv(sys.argv[1])) + footer

Then the only thing that you have to do is to redirect the output to a file, and use latex or pdflatex.