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.
"When I see a bird that walks like a duck and swims like a duck and quacks like a duck, I call that bird a duck."
-- Alex Martelli
Showing posts with label bitset. Show all posts
Showing posts with label bitset. Show all posts
Thursday, June 7, 2007
Tuesday, May 29, 2007
Dynamic bitset for python
I started trying the Python part of Boost C++ libraries recently and found it surprisingly easy to use. In search of a mini-project, I started coding a Python extension module to expose the Boost dynamic bitset for use in Python. It started as a novelty for a quick talk at the DFW Python group but I got stumped by adding operators.. until last night.
Here's a recap:
The constructor can accept a string ('111000') or a number of bits or a number of bits and an initial value. You can have a very large number of bits - in the millions without a problem.
The logical operators (&|^) work as expected and even throw polite exceptions when the operands are of different sizes.
There is a count method to see how many bits are on, a test method that returns true if the bit at a given index is on, a flip (or toggle) method..
There are even docstrings!
Available at:
https://python.taupro.com/repo/Projects/boost_python
It may be interesting to build pure python version and then use both to implement Conway's Game of Life to see what the difference in speed is.
Here's a recap:
The constructor can accept a string ('111000') or a number of bits or a number of bits and an initial value. You can have a very large number of bits - in the millions without a problem.
The logical operators (&|^) work as expected and even throw polite exceptions when the operands are of different sizes.
There is a count method to see how many bits are on, a test method that returns true if the bit at a given index is on, a flip (or toggle) method..
There are even docstrings!
Available at:
https://python.taupro.com/repo/Projects/boost_python
It may be interesting to build pure python version and then use both to implement Conway's Game of Life to see what the difference in speed is.
Subscribe to:
Posts (Atom)