Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this messageIf you can think it, Python can do it. To get a feel for how it works, take a look at http://python.org/doc/2.3.4/ext/ext.html. Using the C API, you can make Python do anything. 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 http://swig.sourceforge.net/ for ideas. On Fri, Jun 04, 2004 at 11:24:38PM -0500, Bo Peng wrote: > 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. > > [snip] > > 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
Post Follow-up to this message"Bo Peng" <bpeng@rice.edu> wrote in message news:c9rhu6$sk6$1@joe.rice.edu... > 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
Post Follow-up to this message"Bo Peng" <bpeng@rice.edu> wrote in message news:c9rhu6$sk6$1@joe.rice.edu... > 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
Post Follow-up to this messageBo Peng wrote: > 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
Post Follow-up to this messagePowered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.