[Tutor] development tools

Magnus Lycka magnus@thinkware.se
Wed Oct 30 05:13:01 2002


At 11:00 2002-10-30 +0700, nano wrote:
>One more, can you gave me some example for using C/C++ module in Python?

I'm not sure if you mean when to use it or how to use it.

If when: Don't use C or C++ unless you need to!
It's a much less productive development environment,
and it will make maintenance of the code harder.
Typically you use it either to improve performance
or to interface with some other code written in
C or C++. If you need to interface with a standard
product written in C ot C++, chances are good that
there is already an interface, but if not, you might
have to write that. If the interface is for a piece
of software which is publically available, consider
making the python interface available on the net, so
that others can use it and help maintain it.

If you have performance problems, profile your
code. The profiling will probably point of some
small part of your code as taking up more than
half of the execution time, maybe much more. Look
closely at this, and not only on the details but
also from a helicopter perspective. Is there some
simple coding changes you can do? Can you restructure
your program, or solve the problem from a different
angle? If none of these things work, you can consider
to rewrite portions in C or C++. Before you write
extension modules you canhave a look at psyco, pyrex
or weave to see if they can fix your problem.

If how, a full answer would be long... It depends very
much on how big it is, whether you need to access
python data from C/C++ etc. There are two standard
manuals for this (Extending & Embedding and Python C/API),
and a number of tools. For C, SWIG is commonly used,
and for C++ Boost might be best. See
http://www.thinkware.se/cgi-bin/thinki.cgi/UsingPythonWithOtherLanguages

The interfaces for SQL databases such as PostgreSQL,
MySQL or Oracle are partly written in C. wxPython is
mainly a wrapper for a C++ toolkit (using SWIG). Look
at the sources for something like this to see how to
do things, but be aware that code might be generated
by SWIG etc.

Concerning toolkits, I won't recommend any product.
I don't have enough experience of either. I am curious
about Twisted, and was never impressed by WebWare, but
I don't know either of them well.

Personally, I'd probably make sure that my business logic
ran as pure, independent python modules, that I could
test and run without the web, and I'd try to hide the
persistence mechanism, SQL interface or whatever, from the
business logic as well. I find this separation of presentation,
logic and storage to be very useful for several reasons:
  * Easier to reuse code.
  * Easier to adapt to business changes.
  * Easier to test.
  * Easier to understand the code.
  * Easier to change toolkit or framework if need be.

Of course, the disadvantage might be that you don't use
all the features in the framework or toolkit you have.
You might duplicate some functionality to achieve this
freedom. Whether it's worth it is a judgement call. You
must do that based on your situation.



-- 
Magnus Lycka, Thinkware AB
Alvans vag 99, SE-907 50 UMEA, SWEDEN
phone: int+46 70 582 80 65, fax: int+46 70 612 80 65
http://www.thinkware.se/  mailto:magnus@thinkware.se