dict python => map<string, int> C++

1,875 views
Skip to first unread message

LMazuel

unread,
Feb 2, 2010, 5:10:15 AM2/2/10
to cython-users
Hi,

I have to used a C++ library from Python (3.1) which its prototype is:

> void myfunction(map<string, int> content) ;

How can I make a good Cython code (or maybe a little C++ wrapper
between Cython and the C++ library) to call the function?
I have seen in:
http://docs.cython.org/src/userguide/language_basics.html?highlight=dict#automatic-type-conversions

that there is an automatic conversion between struct and dict. Is
there an automatic conversion from dict to struct? (and then I can
code a littre wrapper from struct to map<string, int>). If it exist,
what it the form of the generated struct?

Thank you by advance!

Laurent

(sorry for the bad english from France...)

Dag Sverre Seljebotn

unread,
Feb 2, 2010, 5:31:24 AM2/2/10
to cython...@googlegroups.com
You might want to check out this:

http://conference.scipy.org/proceedings/SciPy2009/paper_4/


Dag Sverre

LMazuel

unread,
Feb 2, 2010, 5:50:15 AM2/2/10
to cython-users
Thanks for the link! I will read it carefully and makes return here.

LMazuel

On 2 fév, 11:31, Dag Sverre Seljebotn <da...@student.matnat.uio.no>
wrote:


> LMazuel wrote:
> > Hi,
>
> > I have to used a C++ library from Python (3.1) which its prototype is:
>
> >> void myfunction(map<string, int> content) ;
>
> > How can I make a good Cython code (or maybe a little C++ wrapper
> > between Cython and the C++ library) to call the function?
> > I have seen in:

> >http://docs.cython.org/src/userguide/language_basics.html?highlight=d...

Robert Bradshaw

unread,
Feb 2, 2010, 6:13:36 AM2/2/10
to cython...@googlegroups.com
On Feb 2, 2010, at 2:10 AM, LMazuel wrote:

> Hi,
>
> I have to used a C++ library from Python (3.1) which its prototype is:
>
>> void myfunction(map<string, int> content) ;
>
> How can I make a good Cython code (or maybe a little C++ wrapper
> between Cython and the C++ library) to call the function?
> I have seen in:
> http://docs.cython.org/src/userguide/language_basics.html?highlight=dict#automatic-type-conversions
>
> that there is an automatic conversion between struct and dict. Is
> there an automatic conversion from dict to struct?

No, though I've considered writing one. (Way down low on my priority
list, so don't hold your breath...)

> (and then I can code a littre wrapper from struct to map<string,
> int>). If it exist,
> what it the form of the generated struct?

I don't think the strict -> dict map would help here. It simply makes
a dict naming the members of the struct, for example

cdef struct Point:
double x
double y
double z

cdef Point P
P.x = 1
P.y = 2
P.z = 3.14159
print <object>P # the <object> cast here is unneeded, as it will
be implied by trying to use it in a Python context

would print the Python dictionary

{ 'x': 1.0, 'y': 2.0, 'z': 3.14159 }

It's worth noting that the 0.13 release (which will be out as soon as
we do enough testing) will have much better C++ support, see http://wiki.cython.org/gsoc09/daniloaf/progress
(though you'll still have to roll your own conversion routines).
Also, we only have automatic conversion to char* (though manual
encoding is encouraged), not the C++ string type. Cython can certainly
be used to wrap C++ libraries (we do a lot of that in Sage), but
there's a lot of room for improvement to make it as nice as wrapping C.

>
> Thank you by advance!
>
> Laurent
>
> (sorry for the bad english from France...)

Pas de problème. Je vous assure que votre anglais est mieux que mon
français...

- Robert

LMazuel

unread,
Feb 2, 2010, 10:20:59 AM2/2/10
to cython-users
Thank you for all the answers!

Firstly, I have investigated "convertXY" as advised, but it only works
for Python 2.x.
Thus, I have followed the second advice to make my own conversion
routines.

I paste here the few C++ lines I have made, if someone read this post
one day with the same question...:

void handle_doc_py_dict(PyObject* uris)
{
map<string, bool> curis;
if (PyDict_Check(uris))
{
PyObject *key, *value;
Py_ssize_t pos = 0;

while (PyDict_Next(uris, &pos, &key, &value)) {
if (!PyBytes_Check(key) || !PyLong_Check(value))
{
continue;
}
char* ckey = PyBytes_AsString(key);
bool bval = PyLong_AsLong(value);
string localstr(ckey) ;
curis[localstr] = bval;
}
handle_docmap(curis);
}
}

Reply all
Reply to author
Forward
0 new messages