473,667 Members | 2,568 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Integrating Python into a C++ app

I know the conventional wisdom is to write the whole app in Python and
only extend with C++ where speed is an issue, but I already have a
large C++ app that I'd like to add Python to. Ideally I'd rewrite the
whole app in Python but I don't have time to do that and maintain the
old system at the same time. So my next thought was to perhaps
integrate the two and slowly migrate modules and classes across from
C++ to Python. If it matters, the main reason I'm interested in doing
this is because I appreciate the productivity of Python and would like
to take advantage of that as I add features to the current code, to
reduce bugs and cut development time.

I've read a few good things in this group about Elmer
(http://elmer.sourceforge.net), but I'm not sure how simply that
accommodates calls in the reverse direction (from Python code back into
C++). Are there any other options that would require a minimum of
rewriting of code? Does anybody have any experience of such a project?
--
Ben Sizer

Jul 18 '05 #1
1 1874
Ben Sizer <ky*****@gmail. com> wrote:
I know the conventional wisdom is to write the whole app in Python and
only extend with C++ where speed is an issue, but I already have a
large C++ app that I'd like to add Python to. Ideally I'd rewrite the
whole app in Python but I don't have time to do that and maintain the
old system at the same time. So my next thought was to perhaps
integrate the two and slowly migrate modules and classes across from
C++ to Python. If it matters, the main reason I'm interested in doing
this is because I appreciate the productivity of Python and would like
to take advantage of that as I add features to the current code, to
reduce bugs and cut development time.
This seems quite a reasonable approach to me.

I've read a few good things in this group about Elmer
(http://elmer.sourceforge.net), but I'm not sure how simply that
accommodates calls in the reverse direction (from Python code back into
C++). Are there any other options that would require a minimum of
rewriting of code? Does anybody have any experience of such a project?


Sorry, haven't tried elmer yet. When I did such embedding a few years
ago I used Boost Python (www.boost.org) -- Boost has kept growing better
with the years so today it should be even easier. OTOH, the C++
application was already well structured according to Lakos' ideas
(expressed in his book about large-scale program design in C++):
components communicating across interfaces, rather than a free-for-all
of jumbles of dependencies. The interfaces being abstract C++ classes,
and the building of concrete instances always going through Factory
design patterns, Boost's abilities to wrap C++ classes as Python types
and let Python classes inherit from such wrapped C++ classes so that
normal virtual-method-call C++ approaches proved sufficient.

(In the end, after several experiments, we ended up using COM, since the
application only ran on Windows anyway; much of the COM was of course
done in Python, but that's another issue).

I suspect that few legacy C++ applications are well structured in terms
of components and interfaces. But then, trying to do any kind of major
surgery without serious rearchitecting tends to produce inferior results
anyway. And the rearchitecting is quite advisable whether you do end up
using any Python, or not -- you want good, clean components, cohesive
and coherent, with a good graph of dependencies, unit-tests for each
component, dependency inversion (a la Robert Martin) to avoid
dependencies going the wrong way, etc, etc. Lakos and Martin are the
two authors I would suggest reading. Lakos' book is old, and among his
major concerns is using just the subset of C++ you could rely on, years
ago; you can probably ignore those issues safely today (to use Boost,
you need a good recent C++ compiler with impeccable template support,
anyway, for example).
Alex
Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
5167
by: unexpected | last post by:
Hi all, I'm currently working on a large, legacy Fortran application. I would like to start new development in Python (as it is mainly I/O related). In order to do so, however, the whole project needs to be able to compile in Fortran. I'm aware of resources like the F2Py Interface generator, but this only lets me access the Fortran modules I need in Python. I'm wondering if there's a way to generate the .o files from Python (maybe...
2
1486
by: llandre | last post by:
Hi all, I'd like to have some advices about how to implement a program that has the following requirements: - it must run both on linux and windows PC - it must interact with an electronic device connected to the PC through serial cable by sending/receiving some command strings and taking different actions depending on device responses; these strings and actions must be described in a text file (a sort of script) editable by user with...
2
1381
by: Sam Denton | last post by:
Code generators seem to be popular in Python. (http://www.google.com/search?q=python+code-generator) I have one that I'd like to integrate into IDLE. Ideally, I'd like to (1) have a new file type show up when I use the File/Open dialog, and (2) have a function key that lets me run my generator against the file, just like F5 lets me run my Python code; ideally, I'd like to re-purpose the F5 key to be file-type aware. I've got a simple...
2
160
by: Sam Denton | last post by:
Code generators seem to be popular in Python. (http://www.google.com/search?q=python+code-generator) I have one that I'd like to integrate into IDLE. Ideally, I'd like to (1) have a new file type show up when I use the File/Open dialog, and (2) have a function key that lets me run my generator against the file, just like F5 lets me run my Python code; ideally, I'd like to re-purpose the F5 key to be file-type aware. I've got a simple...
0
8365
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8883
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8646
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7390
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6203
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2013
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1778
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.