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:
- "->" became "."
- "//" became "#"
- "wx" became "wx."
and my favorites:
- "*" became ""
- "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 method calls.
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:
- 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().
- 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.
- 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
return true
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++.
Python rules.
Alec
|
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
Open Source Applications Foundation "Dev" mailing list
http://lists.osafoundation.org/mailman/listinfo/dev