Python c++ wrapper : Convert multi-type struct to it's python representation (preferable dictionary)











up vote
4
down vote

favorite












I've chosen setuptools to use my C/C++ code from python scripts.
One of the phases when building such wrapper is to convert the C/C++ return value into python object.



So far I was able to convert simple primitive values and list of primitive values. However, I wish to extend it to multi-value struct, as shown in the example below.



My main challenge right now is how do I create the python struct representation (PyObject* ret = PyList_New(...);) and I do I set it's values properly with the different types.



I tried to create list of items from the same types (such as std::vector<float>) and manage to set the values properly using Py_BuildValue and PyList_SetItem, but I'm still struggling with the multi types...



typedef struct _fileParams 
{
bool valid;
int index;
std::string key;
std::value value;
} fileParams;

FileDataBase * db;

static PyObject *searchFileInDB(PyObject *self, PyObject *args)
{
if (db == NULL)
{
PyErr_SetString(PyExc_RuntimeError, "DB could not be initialized");
return NULL;
}

char* fileName = NULL;
int fileNameSize = 0;
PyArg_ParseTuple(args, "s#", &fileName, &fileNameSize);
try
{
fileParams p;
bool res = db->lookup(fileName, fileNameSize, p);
PyObject* ret = PyList_New(...);

if (res)
{
PyObject* r1 = Py_BuildValue("b", p.valid);
PyList_SetItem(ret, 0, r1);

PyObject* r2 = Py_BuildValue("i", p.index);
PyList_SetItem(ret, 1, r2);

PyObject* r1 = Py_BuildValue("s", p.key);
PyList_SetItem(ret, 2, r3);

PyObject* r1 = Py_BuildValue("s", p.value);
PyList_SetItem(ret, 3, r4);
}
return ret;
} catch (...) {
PyErr_SetString(PyExc_RuntimeError, "failed with C exception");
return NULL;
}
}









share|improve this question




























    up vote
    4
    down vote

    favorite












    I've chosen setuptools to use my C/C++ code from python scripts.
    One of the phases when building such wrapper is to convert the C/C++ return value into python object.



    So far I was able to convert simple primitive values and list of primitive values. However, I wish to extend it to multi-value struct, as shown in the example below.



    My main challenge right now is how do I create the python struct representation (PyObject* ret = PyList_New(...);) and I do I set it's values properly with the different types.



    I tried to create list of items from the same types (such as std::vector<float>) and manage to set the values properly using Py_BuildValue and PyList_SetItem, but I'm still struggling with the multi types...



    typedef struct _fileParams 
    {
    bool valid;
    int index;
    std::string key;
    std::value value;
    } fileParams;

    FileDataBase * db;

    static PyObject *searchFileInDB(PyObject *self, PyObject *args)
    {
    if (db == NULL)
    {
    PyErr_SetString(PyExc_RuntimeError, "DB could not be initialized");
    return NULL;
    }

    char* fileName = NULL;
    int fileNameSize = 0;
    PyArg_ParseTuple(args, "s#", &fileName, &fileNameSize);
    try
    {
    fileParams p;
    bool res = db->lookup(fileName, fileNameSize, p);
    PyObject* ret = PyList_New(...);

    if (res)
    {
    PyObject* r1 = Py_BuildValue("b", p.valid);
    PyList_SetItem(ret, 0, r1);

    PyObject* r2 = Py_BuildValue("i", p.index);
    PyList_SetItem(ret, 1, r2);

    PyObject* r1 = Py_BuildValue("s", p.key);
    PyList_SetItem(ret, 2, r3);

    PyObject* r1 = Py_BuildValue("s", p.value);
    PyList_SetItem(ret, 3, r4);
    }
    return ret;
    } catch (...) {
    PyErr_SetString(PyExc_RuntimeError, "failed with C exception");
    return NULL;
    }
    }









    share|improve this question


























      up vote
      4
      down vote

      favorite









      up vote
      4
      down vote

      favorite











      I've chosen setuptools to use my C/C++ code from python scripts.
      One of the phases when building such wrapper is to convert the C/C++ return value into python object.



      So far I was able to convert simple primitive values and list of primitive values. However, I wish to extend it to multi-value struct, as shown in the example below.



      My main challenge right now is how do I create the python struct representation (PyObject* ret = PyList_New(...);) and I do I set it's values properly with the different types.



      I tried to create list of items from the same types (such as std::vector<float>) and manage to set the values properly using Py_BuildValue and PyList_SetItem, but I'm still struggling with the multi types...



      typedef struct _fileParams 
      {
      bool valid;
      int index;
      std::string key;
      std::value value;
      } fileParams;

      FileDataBase * db;

      static PyObject *searchFileInDB(PyObject *self, PyObject *args)
      {
      if (db == NULL)
      {
      PyErr_SetString(PyExc_RuntimeError, "DB could not be initialized");
      return NULL;
      }

      char* fileName = NULL;
      int fileNameSize = 0;
      PyArg_ParseTuple(args, "s#", &fileName, &fileNameSize);
      try
      {
      fileParams p;
      bool res = db->lookup(fileName, fileNameSize, p);
      PyObject* ret = PyList_New(...);

      if (res)
      {
      PyObject* r1 = Py_BuildValue("b", p.valid);
      PyList_SetItem(ret, 0, r1);

      PyObject* r2 = Py_BuildValue("i", p.index);
      PyList_SetItem(ret, 1, r2);

      PyObject* r1 = Py_BuildValue("s", p.key);
      PyList_SetItem(ret, 2, r3);

      PyObject* r1 = Py_BuildValue("s", p.value);
      PyList_SetItem(ret, 3, r4);
      }
      return ret;
      } catch (...) {
      PyErr_SetString(PyExc_RuntimeError, "failed with C exception");
      return NULL;
      }
      }









      share|improve this question















      I've chosen setuptools to use my C/C++ code from python scripts.
      One of the phases when building such wrapper is to convert the C/C++ return value into python object.



      So far I was able to convert simple primitive values and list of primitive values. However, I wish to extend it to multi-value struct, as shown in the example below.



      My main challenge right now is how do I create the python struct representation (PyObject* ret = PyList_New(...);) and I do I set it's values properly with the different types.



      I tried to create list of items from the same types (such as std::vector<float>) and manage to set the values properly using Py_BuildValue and PyList_SetItem, but I'm still struggling with the multi types...



      typedef struct _fileParams 
      {
      bool valid;
      int index;
      std::string key;
      std::value value;
      } fileParams;

      FileDataBase * db;

      static PyObject *searchFileInDB(PyObject *self, PyObject *args)
      {
      if (db == NULL)
      {
      PyErr_SetString(PyExc_RuntimeError, "DB could not be initialized");
      return NULL;
      }

      char* fileName = NULL;
      int fileNameSize = 0;
      PyArg_ParseTuple(args, "s#", &fileName, &fileNameSize);
      try
      {
      fileParams p;
      bool res = db->lookup(fileName, fileNameSize, p);
      PyObject* ret = PyList_New(...);

      if (res)
      {
      PyObject* r1 = Py_BuildValue("b", p.valid);
      PyList_SetItem(ret, 0, r1);

      PyObject* r2 = Py_BuildValue("i", p.index);
      PyList_SetItem(ret, 1, r2);

      PyObject* r1 = Py_BuildValue("s", p.key);
      PyList_SetItem(ret, 2, r3);

      PyObject* r1 = Py_BuildValue("s", p.value);
      PyList_SetItem(ret, 3, r4);
      }
      return ret;
      } catch (...) {
      PyErr_SetString(PyExc_RuntimeError, "failed with C exception");
      return NULL;
      }
      }






      python c++ c swig setuptools






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 18 at 8:46

























      asked Nov 18 at 7:50









      Zohar81

      2,0581732




      2,0581732
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote



          accepted










          You probably want to look into the Dictionary Object: Dictionary Objects



          I'm guessing you'd want to set values with PyDict_SetItemString() as per that doc.



          HTH






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














             

            draft saved


            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358886%2fpython-c-wrapper-convert-multi-type-struct-to-its-python-representation-pr%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote



            accepted










            You probably want to look into the Dictionary Object: Dictionary Objects



            I'm guessing you'd want to set values with PyDict_SetItemString() as per that doc.



            HTH






            share|improve this answer

























              up vote
              1
              down vote



              accepted










              You probably want to look into the Dictionary Object: Dictionary Objects



              I'm guessing you'd want to set values with PyDict_SetItemString() as per that doc.



              HTH






              share|improve this answer























                up vote
                1
                down vote



                accepted







                up vote
                1
                down vote



                accepted






                You probably want to look into the Dictionary Object: Dictionary Objects



                I'm guessing you'd want to set values with PyDict_SetItemString() as per that doc.



                HTH






                share|improve this answer












                You probably want to look into the Dictionary Object: Dictionary Objects



                I'm guessing you'd want to set values with PyDict_SetItemString() as per that doc.



                HTH







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 18 at 12:26









                e.dan

                5,4881421




                5,4881421






























                     

                    draft saved


                    draft discarded



















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53358886%2fpython-c-wrapper-convert-multi-type-struct-to-its-python-representation-pr%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin