Monday, June 11, 2007

Issue Number 2

I discovered cssutils tonight.. I thought there must be a parser for CSS in the Python universe somewhere.. and it's called cssutils, available from the Cheeseshop.

I installed it (using the magic of easy_install) and then broke it using the css file from HelixPlayer (the open-source portion of RealPlayer) so I raised an Issue.

Issue No. 2 as it turns out.

Thursday, June 7, 2007

Unit testing.. why wouldn't you?

Unit testing is a good thing, right? Especially if it is simple to set up, simple to run and simple to read the results. How do we do it in Python? Simple!
Here's a quick example using some pieces from the unit test file I checked in along with the python bitset:


import unittest
from pybitset import Bitset # import class or module under test

class bitset_testcase(unittest.TestCase):
def setUp(self): # testcase setup
self.bits = Bitset(10)

def testSize(self): # some method to test
# use assert to compare the expected and
# actual results
assert len(self.bits.bitstring)==self.bits.size(),
'Incorrect Size'

def testRepr(self): # some other method
assert self.bits.size() == len(self.bits.__repr__()),
'Repr size incorrect'


# .. if you need to test exceptions then write some code to
# provoke an exception and catch the exception and add an else
# clause to deal with exception failure

def testIndex(self):
try:
self.bits[40]=1
except IndexError:
pass
else:
self.fail("Out of range index expected exception")

# add the check for main so you can run from the command line
if __name__ == "__main__":
unittest.main()

Presto! Try it out, it's easy.

If you don't like my notes, go and read this

Python bitset checked in (along with unittests)

I have just checked in the python version of bitset (and unittests!) into the boost_python project directory. Now I need to spend a little time making the API for the python version agree with the C++ version. Some discrepancies around init/constructor, nothing major.

I think I have enough functionality to start on benchmarks.

The python bitset uses a list internally to store bits as, can you guess, zero or one. The first crack used the string representation of a bit rather than a numeric which was not a big problem until I noticed that all operations except init and repr needed to convert the value somewhere.

One gotcha to note: the zero-th bit is rightmost, as it would be if you were writing out a number rather than the string layout where the zeroth bit is on the left.

Wednesday, June 6, 2007

Pure python bitset

As previously mentioned, I recently hacked up a C++ extension to allow Python to use the dynamic bitset in the Boost libraries and I wanted to compare its performance to a pure Python implementation. Tonight, I sat down and, in about 90 minutes, hacked out a decent looking Python version.

I'll check it into the DFW Pythoneers subversion repository in case anyone wants to look at it.

Next thing to try: some sort of benchmarks to compare the two and then maybe Conway's Game of Life.