Can python control complicated classes/objects written in C++

B

Bo Peng

Dear Python group:

I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.

I become interested in Python since it can load C++ objects and can even
extend C++ classes. However, I am not quite sure to what extent can
python do this. Ideally, I would like to have something like

(pseudo code, not in python)
> data = new TData( option1=..., option2=...)
> action1 = new TAction(option1=range(1,10)...)
> action2 = new TSubAction(option1=sin(5),..., option2=...)
> data.run( action1, action2)
> data.print

The benefits here is that I do not have to worry about user-input
(python handles functions like range(), and numeric/string operations
for me and send them to my objects), running logic (user create the
scripts) and need only focus on the objects themselves. It would be
better if users can easily extend TAction by themselves, through either
Python or C++.

I am totally new to Python. I have read boost.python, python extension
document but still do not know exactly what to do. My questions are:

1. can Python fully read/write member data and run member functions of
my objects?

2. can python pass complicated objects (TAction) to another object (TData)?

3. If python can not do this, I will have to create my own scripting
language. Given the above pseudo code, any suggestion on how to
implement it? I have googgled Qt Script for Application and many other
weird implementations but I either do not like the grammar of the script
language or the huge overheads.

Many thanks in advance.

Bo
 
T

Terry Reedy

Bo Peng said:
Dear Python group:

I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.

I become interested in Python since it can load C++ objects and can even
extend C++ classes. However, I am not quite sure to what extent can
python do this. Ideally, I would like to have something like

(pseudo code, not in python)

If you remove 'new' on the first three lines, the above could be Python.
The benefits here is that I do not have to worry about user-input
(python handles functions like range(), and numeric/string operations
for me and send them to my objects), running logic (user create the
scripts) and need only focus on the objects themselves. It would be
better if users can easily extend TAction by themselves, through either
Python or C++.

I am totally new to Python. I have read boost.python, python extension
document but still do not know exactly what to do. My questions are:

1. can Python fully read/write member data and run member functions of
my objects?

If they are properly wrapped, yes.
2. can python pass complicated objects (TAction) to another object
(TData)?

Everything in Python is a first class object that can be passed, returned,
or whatever.

Phil's response:
<However, writing all the glue can be boring and error prone, so there
<are a number of tools to make this easier. Take a look at
<http://www.cosc.canterbury.ac.nz/~greg/python/Pyrex/ and

PyRex intends to make it easy to write C extensions in extended Python.

<http://swig.sourceforge.net/ for ideas.

swig wraps existing extensions, which you seem to have.

Terry J. Reedy
 
R

Roger Binns

Bo said:
I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.

The example you give will be no problem using SWIG and Python.

In general the area where things get tricky is dealing with
memory management. You have to decide who "owns" any returned
objects - do they belong to the C++ code and have to be freed
via method calls, or do they belong to the Python proxy object
which frees them when it is deleted.

The former case can lead to memory leaks and the latter case
can lead to things being freed that are referenced by other
C++ objects.

You can have difficulty in this area if the underlying C++ library
is poorly designed. For example I have had to deal with cases
where the C++ objects had references between each other and it
was really hard to figure out when to free the objects. Mind you I
would encounter the same issues if using the library from other
C++ code. In the end I had to write a C++ wrapper on top of the
C++ library and deal with reference counting and that kind of
stuff in the wrapper. The wrapper trivially exported to Python
via SWIG.

I guess the point I am making in a long winded way is that if
the library is poorly designed, especially with respect to
objects ownership issues, then Python won't make them go away
and may highlight them more since more code paths become
possible.

Roger
 
P

Paul McGuire

Bo Peng said:
Dear Python group:

I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.

I become interested in Python since it can load C++ objects and can even
extend C++ classes. However, I am not quite sure to what extent can
python do this. Ideally, I would like to have something like

(pseudo code, not in python)

<snip>

To follow up on Terry Reedy's comment, why are the objects in C++ at all?
Or at least, why not prototype this entirely in Python, and then use what
you've learned to architect a good solid set of C++ classes? If the classes
are as complicated as you say, then there is a good chance that your initial
class and class interaction designs will need some refining after you get
them down in rough form. As your design thoughts evolve about the
interfaces of the TData and TAction classes, and how they work together, you
will be much quicker at making the changes in Python.

-- Paul
 
C

Christophe Cavalaria

Bo said:
Dear Python group:

I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.

I become interested in Python since it can load C++ objects and can even
extend C++ classes. However, I am not quite sure to what extent can
python do this. Ideally, I would like to have something like

(pseudo code, not in python)

The benefits here is that I do not have to worry about user-input
(python handles functions like range(), and numeric/string operations
for me and send them to my objects), running logic (user create the
scripts) and need only focus on the objects themselves. It would be
better if users can easily extend TAction by themselves, through either
Python or C++.

I am totally new to Python. I have read boost.python, python extension
document but still do not know exactly what to do. My questions are:

1. can Python fully read/write member data and run member functions of
my objects?

2. can python pass complicated objects (TAction) to another object
(TData)?

3. If python can not do this, I will have to create my own scripting
language. Given the above pseudo code, any suggestion on how to
implement it? I have googgled Qt Script for Application and many other
weird implementations but I either do not like the grammar of the script
language or the huge overheads.

Many thanks in advance.

Bo

You can try the Python module in Boost ( http://boost.org/ )
Here's a direct link : http://boost.org/libs/python/doc/index.html
 
D

djw

Bo said:
Dear Python group:

I am planning on an application that involves several complicated C++
classes. Basically, there will be one or two big data objects and some
"action" objects that can act on the data. I would like to use a script
language to control the interaction between these c++ objects.

I become interested in Python since it can load C++ objects and can even
extend C++ classes. However, I am not quite sure to what extent can
python do this. Ideally, I would like to have something like

(pseudo code, not in python)

The benefits here is that I do not have to worry about user-input
(python handles functions like range(), and numeric/string operations
for me and send them to my objects), running logic (user create the
scripts) and need only focus on the objects themselves. It would be
better if users can easily extend TAction by themselves, through either
Python or C++.

I am totally new to Python. I have read boost.python, python extension
document but still do not know exactly what to do. My questions are:

1. can Python fully read/write member data and run member functions of
my objects?

2. can python pass complicated objects (TAction) to another object (TData)?

3. If python can not do this, I will have to create my own scripting
language. Given the above pseudo code, any suggestion on how to
implement it? I have googgled Qt Script for Application and many other
weird implementations but I either do not like the grammar of the script
language or the huge overheads.

Many thanks in advance.

Bo

I would take a look at PyQT as an example of a "complicated" C++ library
(Qt) wrapped for Python. Iys uses SIP as the interface generator, but
other projects would also work for you (SWIG, Boost, etc)

-Don
 
B

Bo Peng

I am planning on an application that involves several complicated C++
classes. ... I would like to use a script
language to control the interaction between these c++ objects.

Thank you all for the good (and quick) suggestions. I have read more
about Python extension (SWIG, especially Dr. Beazley's tutorial) and
boost.Python. It seems that Python/C++ is the way to do.

1. I have to use C++ since efficiency is at very high priority.

2. Writing a prototype in Python is a very good idea. Since I am more
proficient in C++ than in Python, I am not sure which way is quicker for
me. I will read more about Python and decide.

3. SWIG can provide tcl, perl interfaces as well. However, perl's OOP
features seem to be weird and tcl's syntax is kind of old (personal
opinions). Python's integration with C++ is better and the syntax is
more natural.

4. Boost.python and SWIG are similar but SWIG is easier to use. I will
try SWIG first.

5. My C++ code will use a lot of functors (passing objects that have
operator() ), templates, inheritance. I hope SWIG/Python can handle all
of them... I am not sure if Python/SWIG can handle copy constructor
transparently either. I am reading the documents.

Again, thank you all for the help.
Bo
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,227
Latest member
Daniella65

Latest Threads

Top