3

I have a simple user defined StringList class in C++. Its underlying container is a std::vector.

It would be convenient, when using this class in python, to have any StringList object converted to a Python list whenever returned by a python wrapped object.

The StringList class do contain a function, getContainer(), that do return a reference to the underlying vector<string> object, and I would think this function could be used somehow?

For example, the C++ code

Raven aRaven;
StringList molecules;
molecules = aRaven.getMoleculesInDB();

populates the molecules object with a list of moleculenames (strings).

In Python, this looks like this, including code to loop over the list:

aRaven = Raven()
molecules = aRaven.getMoleculesInDB()

print 'There are ' + `molecules.count()` +' available.' 
for mol in range(molecules.count()):
    molName = molecules.at(mol)                
...

It is desirable to instead of creating a StringList object in the aRaven.getMoleculesInDb(), creating a Python list with strings (from the underlying std::vector container). Then the user could write

aRaven = Raven()
molecules = aRaven.getMoleculesInDB() # <--- Returns a Python list of strings

for molName in molecules:
    print 'molName'
...

Any pointers on how to achieve this in the swig interface file?

Totte Karlsson
  • 1,261
  • 1
  • 20
  • 55
  • Do you mean have it converted to a Python list? I'm not 100% clear on which part of the Python-C++ interaction you're trying to improve--Python calling C++ or the other way. – John Zwinck Jun 21 '14 at 04:40
  • Post the python code you would like to be able to write, this will clarify a few things. – Oliver Jun 21 '14 at 12:43
  • I updated the question with some code showing what I'm trying to achieve. – Totte Karlsson Jun 21 '14 at 21:23
  • You don't need to convert it to a python list to get that behaviour, implementing the iterator protocol is enough. Does my answer at: http://stackoverflow.com/questions/8776328/swig-interfacing-c-library-to-python-creating-iterable-python-data-type-from/8828454#8828454 cover it for you? – Flexo Jun 29 '14 at 12:11

0 Answers0