c++ data types in python script

Discussion in 'Python' started by Arthur Mc Coy, Mar 6, 2011.

  1. 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
     
    Arthur Mc Coy, Mar 6, 2011
    #1
    1. Advertising

  2. 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
     
    Arthur Mc Coy, Mar 6, 2011
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Steve Knight
    Replies:
    2
    Views:
    778
    Steve Knight
    Oct 10, 2003
  2. j_mckitrick
    Replies:
    2
    Views:
    568
    Cameron Laird
    Nov 13, 2004
  3. ramu
    Replies:
    2
    Views:
    331
    rlblaster
    Feb 20, 2006
  4. Gary Roach
    Replies:
    0
    Views:
    116
    Gary Roach
    Sep 1, 2013
  5. Fábio Santos
    Replies:
    0
    Views:
    129
    Fábio Santos
    Sep 4, 2013
Loading...

Share This Page