From 62c4b54e56513e5a4705f8b7b0bd3c13e6b9f93c Mon Sep 17 00:00:00 2001 From: Michel Ferrero Date: Wed, 4 Jun 2014 01:11:45 +0200 Subject: [PATCH] Add maps to py_converters --- test/pytriqs/wrap_test/a.hpp | 5 +++ test/pytriqs/wrap_test/my_module_desc.py | 2 ++ test/pytriqs/wrap_test/wrap_a.output | 1 + test/pytriqs/wrap_test/wrap_a.py | 1 + triqs/python_tools/wrapper_tools.hpp | 41 ++++++++++++++++++++++++ 5 files changed, 50 insertions(+) diff --git a/test/pytriqs/wrap_test/a.hpp b/test/pytriqs/wrap_test/a.hpp index b4d9f3dc..d3a82009 100644 --- a/test/pytriqs/wrap_test/a.hpp +++ b/test/pytriqs/wrap_test/a.hpp @@ -184,6 +184,11 @@ inline void print_matrix(matrix const &M) { // std::cout << M <> map_to_mapvec(std::map const &m) { + std::map> mm; + for(auto const &x: m) mm.emplace(x.first, std::vector{x.second, 3, 5}); + return mm; +} inline std::function make_fnt_ii() { return [](int i, int j) { return i + 2*j;}; diff --git a/test/pytriqs/wrap_test/my_module_desc.py b/test/pytriqs/wrap_test/my_module_desc.py index 44d999cd..f0840ed6 100644 --- a/test/pytriqs/wrap_test/my_module_desc.py +++ b/test/pytriqs/wrap_test/my_module_desc.py @@ -95,6 +95,8 @@ module.add_function (name = "make_fnt_void", signature = {'c_name': 'make_fnt_vo module.add_function (name = "use_fnt_ii", signature = "void(std::function f)", doc = "....") module.add_function (name = "use_fnt_iid", signature = "void(std::function f)", doc = "....") +module.add_function (name = "map_to_mapvec", signature = "std::map>(std::map m)", doc = "DOC of print_map") + def f1(x,y): print " I am in f1 ", x,y print y + 1/0.2 diff --git a/test/pytriqs/wrap_test/wrap_a.output b/test/pytriqs/wrap_test/wrap_a.output index 181c7f90..285b0597 100644 --- a/test/pytriqs/wrap_test/wrap_a.output +++ b/test/pytriqs/wrap_test/wrap_a.output @@ -18,6 +18,7 @@ use_fnt iid 9 use_fnt iid 45 +{'a': [1, 3, 5], 'b': [2, 3, 5], 'sjkdf': [5, 3, 5]} rereading from hdf5 I am an A with x= 6.9 'cmy_module\n__reduce_reconstructor__Ac\np0\n(I1\nF3.0\ntp1\nRp2\n.' I am an A with x= 3 diff --git a/test/pytriqs/wrap_test/wrap_a.py b/test/pytriqs/wrap_test/wrap_a.py index bee6c1a5..2db6eac6 100644 --- a/test/pytriqs/wrap_test/wrap_a.py +++ b/test/pytriqs/wrap_test/wrap_a.py @@ -33,6 +33,7 @@ use_fnt_iid(fc2) fp2 = lambda i,j,a : a+ i + 20*j use_fnt_iid(fp2) +print map_to_mapvec({'a':1, "b":2, "sjkdf":5}) from pytriqs.archive import * import pytriqs.archive.hdf_archive_schemes diff --git a/triqs/python_tools/wrapper_tools.hpp b/triqs/python_tools/wrapper_tools.hpp index c08fdf22..618c10af 100644 --- a/triqs/python_tools/wrapper_tools.hpp +++ b/triqs/python_tools/wrapper_tools.hpp @@ -304,6 +304,47 @@ template struct py_converter> { } }; +// --- maps --- + +template struct py_converter> { + static PyObject * c2py(std::map const &m) { + PyObject * d = PyDict_New(); + for (auto & x : m) if (PyDict_SetItem(d, py_converter::c2py(x.first), py_converter::c2py(x.second)) == -1) { Py_DECREF(d); return NULL;} // error + return d; + } + static PyObject * c2py(std::map &m) { + PyObject * d = PyDict_New(); + for (auto & x : m) if (PyDict_SetItem(d, py_converter::c2py(x.first), py_converter::c2py(x.second)) == -1) { Py_DECREF(d); return NULL;} // error + return d; + } + static bool is_convertible(PyObject *ob, bool raise_exception) { + if (!PyDict_Check(ob)) goto _false; + { + pyref keys = PyDict_Keys(ob); + pyref values = PyDict_Values(ob); + int len = PyDict_Size(ob); + for (int i = 0; i < len; i++) { + if (!py_converter::is_convertible(PyList_GET_ITEM((PyObject*)keys, i),raise_exception)) goto _false; //borrowed ref + if (!py_converter::is_convertible(PyList_GET_ITEM((PyObject*)values, i),raise_exception)) goto _false; //borrowed ref + } + return true; + } + _false: + if (raise_exception) { PyErr_SetString(PyExc_TypeError, "Can not convert to std::map");} + return false; + } + static std::map py2c(PyObject * ob) { + pyref keys = PyDict_Keys(ob); + pyref values = PyDict_Values(ob); + std::map res; + int len = PyDict_Size(ob); + for (int i = 0; i < len; i++) + res.emplace(py_converter::py2c(PyList_GET_ITEM((PyObject*)keys, i)), //borrowed ref + py_converter::py2c(PyList_GET_ITEM((PyObject*)values, i))); //borrowed ref + return res; + } +}; + // --- pair --- template struct py_converter> {