[Tutor] Creating a complex python object

nik my.mailing.lists at noos.fr
Thu Aug 12 18:42:34 CEST 2004

hi again,

I'm trying to pass a complicated structure from my C++ app to a python 
script, so that my users have a chance to alter it if they want. I'd 
like to provide example scripts that can serialise the data into files, 
or create entries into a database etc.

The structure is a mix of lists, strings, and lists of lists - I suppose 
in a simplified way it looks like (in C++);

using namespace std;

typedef list<string> listOfStrings;

typedef struct {
    listOfStrings subData;
    string subTitle;
    string subHeading;
} subComponent;

typedef list<subcomponent> listOfSubcomponenets;

typedef {
    listOfSubcomponents theData;
    string Title;
    string otherStuff;
} myStructure;

With a filled myStructure instance being the object I want to pass to 
the python script.

I've only just started looking at python, but it's looking like it's 
possible. From the example of embedding python in the tutorial, it shows 
me how to create an object with a couple of strings as attributes. I can 
see that I could make these objects lists, but I'm wondering what to do 
about the inner list (listOfStrings in my example above). I'm wondering 
if I'm on the right track with these views of the problem;

* I could generate the whole thing adding lists to lists, but I'm not 
sure if the user would find that easy to navigate through the PyObject 
(ie the object would have an attribute called 'theData', but then what? 
Do I tell them go to the first item of theData and that's a list? To 
them it wouldn't be called subData, it'd just be an item).

* I could create a PyObject that's the subComponent, and put that in the 
myStructure PyObject. I tried this and got a segmentation fault so I've 
done something wrong (not suprisingly, since I haven't got a clue what 
I'm doing yet).

* The boost::python library seems to be suggesting that it can create 
the PyObject automatically (abracadabra!), although I've not gone into 
this in depth. Is that true?

Is this the right list for this kind of question, or should I be going 
to C++-sig?


More information about the Tutor mailing list