mirror of
https://github.com/triqs/dft_tools
synced 2024-10-31 19:23:45 +01:00
Add maps to py_converters
This commit is contained in:
parent
dfa27554f5
commit
62c4b54e56
@ -184,6 +184,11 @@ inline void print_matrix(matrix<double> const &M) {
|
|||||||
// std::cout << M <<std::endl;
|
// std::cout << M <<std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inline std::map<std::string,std::vector<int>> map_to_mapvec(std::map<std::string,int> const &m) {
|
||||||
|
std::map<std::string,std::vector<int>> mm;
|
||||||
|
for(auto const &x: m) mm.emplace(x.first, std::vector<int>{x.second, 3, 5});
|
||||||
|
return mm;
|
||||||
|
}
|
||||||
|
|
||||||
inline std::function<int(int,int)> make_fnt_ii() {
|
inline std::function<int(int,int)> make_fnt_ii() {
|
||||||
return [](int i, int j) { return i + 2*j;};
|
return [](int i, int j) { return i + 2*j;};
|
||||||
|
@ -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<int(int,int)> f)", doc = "....")
|
module.add_function (name = "use_fnt_ii", signature = "void(std::function<int(int,int)> f)", doc = "....")
|
||||||
module.add_function (name = "use_fnt_iid", signature = "void(std::function<int(int,int,double)> f)", doc = "....")
|
module.add_function (name = "use_fnt_iid", signature = "void(std::function<int(int,int,double)> f)", doc = "....")
|
||||||
|
|
||||||
|
module.add_function (name = "map_to_mapvec", signature = "std::map<std::string,std::vector<int>>(std::map<std::string,int> m)", doc = "DOC of print_map")
|
||||||
|
|
||||||
def f1(x,y):
|
def f1(x,y):
|
||||||
print " I am in f1 ", x,y
|
print " I am in f1 ", x,y
|
||||||
print y + 1/0.2
|
print y + 1/0.2
|
||||||
|
@ -18,6 +18,7 @@ use_fnt iid
|
|||||||
9
|
9
|
||||||
use_fnt iid
|
use_fnt iid
|
||||||
45
|
45
|
||||||
|
{'a': [1, 3, 5], 'b': [2, 3, 5], 'sjkdf': [5, 3, 5]}
|
||||||
rereading from hdf5 I am an A with x= 6.9
|
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.'
|
'cmy_module\n__reduce_reconstructor__Ac\np0\n(I1\nF3.0\ntp1\nRp2\n.'
|
||||||
I am an A with x= 3
|
I am an A with x= 3
|
||||||
|
@ -33,6 +33,7 @@ use_fnt_iid(fc2)
|
|||||||
fp2 = lambda i,j,a : a+ i + 20*j
|
fp2 = lambda i,j,a : a+ i + 20*j
|
||||||
use_fnt_iid(fp2)
|
use_fnt_iid(fp2)
|
||||||
|
|
||||||
|
print map_to_mapvec({'a':1, "b":2, "sjkdf":5})
|
||||||
|
|
||||||
from pytriqs.archive import *
|
from pytriqs.archive import *
|
||||||
import pytriqs.archive.hdf_archive_schemes
|
import pytriqs.archive.hdf_archive_schemes
|
||||||
|
@ -304,6 +304,47 @@ template <typename T> struct py_converter<std::vector<T>> {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// --- maps ---
|
||||||
|
|
||||||
|
template <typename K, typename V> struct py_converter<std::map<K,V>> {
|
||||||
|
static PyObject * c2py(std::map<K,V> const &m) {
|
||||||
|
PyObject * d = PyDict_New();
|
||||||
|
for (auto & x : m) if (PyDict_SetItem(d, py_converter<K>::c2py(x.first), py_converter<V>::c2py(x.second)) == -1) { Py_DECREF(d); return NULL;} // error
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
static PyObject * c2py(std::map<K,V> &m) {
|
||||||
|
PyObject * d = PyDict_New();
|
||||||
|
for (auto & x : m) if (PyDict_SetItem(d, py_converter<K>::c2py(x.first), py_converter<V>::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<K>::is_convertible(PyList_GET_ITEM((PyObject*)keys, i),raise_exception)) goto _false; //borrowed ref
|
||||||
|
if (!py_converter<V>::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<K,V> py2c(PyObject * ob) {
|
||||||
|
pyref keys = PyDict_Keys(ob);
|
||||||
|
pyref values = PyDict_Values(ob);
|
||||||
|
std::map<K,V> res;
|
||||||
|
int len = PyDict_Size(ob);
|
||||||
|
for (int i = 0; i < len; i++)
|
||||||
|
res.emplace(py_converter<K>::py2c(PyList_GET_ITEM((PyObject*)keys, i)), //borrowed ref
|
||||||
|
py_converter<V>::py2c(PyList_GET_ITEM((PyObject*)values, i))); //borrowed ref
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// --- pair ---
|
// --- pair ---
|
||||||
|
|
||||||
template <typename T1, typename T2> struct py_converter<std::pair<T1,T2>> {
|
template <typename T1, typename T2> struct py_converter<std::pair<T1,T2>> {
|
||||||
|
Loading…
Reference in New Issue
Block a user