[Dev] minical python rewrite
alecf at osafoundation.org
Thu Feb 16 11:20:24 PST 2006
I just landed the rewrite of the minical in python... it was a pretty
fun and interesting exercise and it really drove home for me why it is
so much easier and faster to develop with Python
For those of you who have years of C++ experience and are enjoying the
transition, here are some things that will just reinforce that feeling.
I myself used to be a die-hard C++ guru going so far as to think that
C++ templates were really cool and clever :)
* The initial conversion took me about 5 hours. Much of that was
literally just cutting and pasing C++, and doing some basic search
and replace substitutions:
o "->" became "."
o "//" became "#"
o "wx" became "wx."
and my favorites:
o "*" became ""
o "m_" became "self."
* After the first cut/paste/substitution pass, I did a per-function
side-by-side pass to make sure that the same logic was maintained
from C++ to Python, and that I was properly using 'self.' for
Notes from the conversion:
* the ? : operator in C++ was a pain in the neck to convert. yuck.
* some basic datastructures became dramatically easier to manage in
python. For instance:
o there was a list of points for drawing - in C++ we had to
manually manage an array and copy objects into the array,
maintaining the current index. In Python, I just used .append().
o meta-attributes about individual days like busy-ness were
stored in an array structs that were dynamically allocated
and had lots of supporting code to make management of this
array "easy" - in python, this was just a list.
o the busybar storage for storing the percentage busy in a
given day involved the above list, and was accessed by a
complex mapping of the date to an index in the array. i.e.
today might map to the 37th entry in the array, depending on
what is displayed on screen. In Python, I was able to simply
use a dict() that mapped date values to a float.
* Accessors. In C++ the convention is to have a Getter and a Setter
wrapping some internal attribute, and these are (I think) required
for SWIG. Now that everything is in python, raw attribute access
was easy and made the code much cleaner
* "out" values - there were a number of functions which had "out"
parameters like GetSomeCoordinate(int*x, int* y) that were much
simpler in python because you could just return a complex value
like a tuple. So things like
*x = width * pos
*y = height * row
became as simple as:
return (width * pos, height * row)
* The biggest surprise for me was that wxDateTime -> python datetime
was a pretty painless process. Thank goodness for timedelta!
However wxDateTime has some really wacky methods, like
SetToPrevWeekDay() that I had to write myself.
* Lines of code: 2045 lines of C++ and SWIG to 919 lines of Python
The basic takeaway here is that there are lots of perfectly legitimate
things you need to do in C++ because of its basic weakness in
datastructures that are trivial in Python.
After the conversion was done, I started finding little bugs and code
cleanups right away. It was not only very easy to fix these things but
the edit-run development loop drastically improved the speed at which I
could fix the bugs. I was able to find and fix performance and memory
usage bugs easily 10 times faster than I could in C++.
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Dev