c++ data types in python script

A

Arthur Mc Coy

Hi people,

I've used SWIG module to embed python inside c++ app. I pass a list of
objects (with lots of different properties of types string, float,
custom types like URL, Software and finally of list of strings).

Now I'm in python. URL and Software has str() method that converts
their value to string recognizable by JSON. But the problem is with
list of strings.

So, as I said I passed std::list<std::string> myObjects to python
function.
Then I iterate it and for each object in myObjects I create a python
copy (serialize it) to be able to put into JSON format and store in
appropriate file.

object has property benchmarks of type list<string>.
I do:
....
class PythonObject:
def __init__(self, object):
self.benchmarks = list()
for s in object.benchmarks:
self.benchmarks.append(s)
....
and it fails, also I do:
....
class PythonObject:
def __init__(self, object):
self.benchmarks = [unicode(s) for s in object.benchmarks]
....
and it fails, also I do:
....
class PythonObject:
def __init__(self, object):
for s in object.benchmarks:
print s[0] + s[1] + s[2]
print type(s)
....
and it fails printing
wor
<type 'str'>
Segmentation fault (core dumped)
$
also I do:
....
class PythonObject:
def __init__(self, object):
self.benchmarks = unicode(object.benchmarks)
....
and it does not fail, instead it puts in JSON this string:
....
"benchmarks": "<mymodule.StringList; proxy of <Swig Object of type
'std::list< std::string, std::allocator< std::string > > *' at
0xb63ed4e8>>",
....
but it is not what I need

What I'm trying to stress is that c++ objects should be converted
(serialized) before putting them into json. Otherwise type errors
occur and process fails.


I love learning python and hope somebody may suggest me or tell
something.

Thank you all anyway!

Arthur
 
A

Arthur Mc Coy

Great!

The solution is to use self.benchmarks = list(object.benchmarks).

Now I'm battling with time_t type. C++ time_t converts to python int
but it causes memore leaks due to destructor absence. I'm trying to
figure it out. If anyone know, please share your thoughts.

Be happy :)

Arthur
 

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,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top