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

Friday, March 30, 2007

Java-to-Python converter

"Anything that can be done, could be done 'meta'" (© Charles Simonyi) is right up there with "Laziness, impatience and hubris" (© Larry Wall) as pithy description of my development philosophy. Also, unfortunately, there's another one: "Once it's clear how to proceed, why bother to proceed" (or something like that). So, with that in mind...

I wanted a Python client library for GData (thankfully, they released one last week, so this is moot -- good!), so I thought of automagically converting the Java library to Python. I tried Java2Python, but it's based on ANTLR grammar for Java 1.4, and the library, of course, is in Java 5. As I was relearning ANTLR and writing all these actions by hand (the pain!), I took a break and found Java 1.5 parser with AST generation and visitor suport by Julio Gesser (no relation, I presume?) and Sreenivasa Viswanadha, based on JavaCC. Aha! Much easier... But then, of course, Google releases the Python version of the library I needed in the first place, so I don't bother wrapping this project up... Here it is for whoever wants it: http://code.google.com/p/j2p/.

Tuesday, February 13, 2007

Continuous integration with code coverage in Python

Update

This code has been integrated into the main tree.

I decided it would be good to have a coverage report of our Python code, with nice visualization like Clover. So I took Ron Smith's PyAntTasks, and added py-cover task to them. This will run coverage for every test, and a cumulative one. In other words, you can see what code a particular test exercises, and what code all the tests in your tree exercise.

This also modifies py-test task to include packagedtests attribute - see below.

The newly added py-cover task runs Ned Batchelder's coverage.py (download it separately), and is specified as follows in your build.xml:

<py-cover
packagedtests="false"
pythonpath="${pythonpath}"
reportsDir="${reports}"
coverage="${tools}/coverage.py">
 
<tests dir=".">
    <include name="**Test.py">
      </tests>

<src dir="${quickRoot}">
     <include name="**/*.py">
     <include name="*.py">
</src>
</py-cover>

Here, <tests> is a FileSet specifying tests to run, <src;> is a FileSet specifying source code to cover. The attributes are:

  • reportsDir - where the coverage reports go

  • packagedtests - this idiosyncrasy is prompted by our tree setup. If this attribute is true it means that the test files reside in Python packages, false otherwise. (In our case, they do not; they are in the tree but are not packages. Note that the original py-test task assumed they are in packages, I ahve changed this too).

  • coverage - path to coverage.py on your system (which you downloaded separately, right?)

P.S. I know about the colorize.py thingie, but I rolled my own (uglier, of course) for this one.