3
3
4
4
#include < ekat/ekat_parameter_list.hpp>
5
5
6
- #include < pybind11/pybind11.h>
7
- #include < pybind11/numpy.h>
8
- #include < pybind11/stl.h>
6
+ #include < nanobind/nanobind.h>
7
+ #include < nanobind/stl/list.h>
8
+ #include < nanobind/stl/string.h>
9
+ #include < nanobind/stl/vector.h>
9
10
10
11
#include < functional>
11
12
13
+ namespace nb = nanobind;
14
+
12
15
namespace scream {
13
16
14
17
struct PyParamList {
@@ -19,11 +22,11 @@ struct PyParamList {
19
22
: pl_ref(src)
20
23
{}
21
24
22
- PyParamList (const pybind11 ::dict& d)
25
+ PyParamList (const nb ::dict& d)
23
26
: PyParamList(d," " )
24
27
{}
25
28
26
- PyParamList (const pybind11 ::dict& d, const std::string& name)
29
+ PyParamList (const nb ::dict& d, const std::string& name)
27
30
: pl(name)
28
31
, pl_ref(pl)
29
32
{
@@ -69,70 +72,70 @@ struct PyParamList {
69
72
70
73
private:
71
74
72
- void parse_dict (const pybind11 ::dict& d, ekat::ParameterList& p) {
75
+ void parse_dict (const nb ::dict& d, ekat::ParameterList& p) {
73
76
for (auto item : d) {
74
- const std::string key = pybind11::str (item.first );
75
- if (pybind11 ::isinstance<pybind11 ::str>(item.second )) {
76
- auto pystr = pybind11 ::str (item.second );
77
- p.set <std::string>(key,pystr. cast <std::string>());
78
- } else if (pybind11 ::isinstance<pybind11 ::bool_>(item.second )) {
79
- auto pyint = pybind11 ::cast<pybind11 ::bool_>(item.second );
80
- p.set (key,pyint. cast <bool >());
81
- } else if (pybind11 ::isinstance<pybind11 ::int_>(item.second )) {
82
- auto pyint = pybind11 ::cast<pybind11 ::int_>(item.second );
83
- p.set (key,pyint. cast <int >());
84
- } else if (pybind11 ::isinstance<pybind11 ::float_>(item.second )) {
85
- auto pydouble = pybind11 ::cast<pybind11 ::float_>(item.second );
86
- p.set (key,pydouble. cast <double >());
87
- } else if (pybind11 ::isinstance<pybind11 ::list>(item.second )) {
88
- auto pylist = pybind11 ::cast<pybind11 ::list>(item.second );
77
+ auto key = nb::cast<std::string> (item.first );
78
+ if (nb ::isinstance<nb ::str>(item.second )) {
79
+ auto pystr = nb ::str (item.second );
80
+ p.set <std::string>(key,nb:: cast<std::string>(pystr ));
81
+ } else if (nb ::isinstance<nb ::bool_>(item.second )) {
82
+ auto pyint = nb ::cast<nb ::bool_>(item.second );
83
+ p.set (key,nb:: cast<bool >(pyint ));
84
+ } else if (nb ::isinstance<nb ::int_>(item.second )) {
85
+ auto pyint = nb ::cast<nb ::int_>(item.second );
86
+ p.set (key,nb:: cast<int >(pyint ));
87
+ } else if (nb ::isinstance<nb ::float_>(item.second )) {
88
+ auto pydouble = nb ::cast<nb ::float_>(item.second );
89
+ p.set (key,nb:: cast<double >(pydouble ));
90
+ } else if (nb ::isinstance<nb ::list>(item.second )) {
91
+ auto pylist = nb ::cast<nb ::list>(item.second );
89
92
parse_list (pylist,p,key);
90
- } else if (pybind11 ::isinstance<pybind11 ::dict>(item.second )) {
91
- auto pydict = pybind11 ::cast<pybind11 ::dict>(item.second );
93
+ } else if (nb ::isinstance<nb ::dict>(item.second )) {
94
+ auto pydict = nb ::cast<nb ::dict>(item.second );
92
95
parse_dict (pydict,p.sublist (key));
93
96
} else {
94
97
EKAT_ERROR_MSG (" Unsupported/unrecognized dict entry type.\n " );
95
98
}
96
99
}
97
100
}
98
101
99
- void parse_list (const pybind11 ::list& l, ekat::ParameterList&p, const std::string& key) {
100
- EKAT_REQUIRE_MSG (pybind11 ::len (l)>0 ,
102
+ void parse_list (const nb ::list& l, ekat::ParameterList&p, const std::string& key) {
103
+ EKAT_REQUIRE_MSG (nb ::len (l)>0 ,
101
104
" Error! Cannot deduce type for dictionary list entry '" + key + " '\n " );
102
105
auto first = l[0 ];
103
- bool are_ints = pybind11 ::isinstance<pybind11 ::int_>(first);
104
- bool are_floats = pybind11 ::isinstance<pybind11 ::float_>(first);
105
- bool are_strings = pybind11 ::isinstance<pybind11 ::str>(first);
106
+ bool are_ints = nb ::isinstance<nb ::int_>(first);
107
+ bool are_floats = nb ::isinstance<nb ::float_>(first);
108
+ bool are_strings = nb ::isinstance<nb ::str>(first);
106
109
if (are_ints) {
107
- parse_list_impl<int ,pybind11 ::int_>(l,p,key);
110
+ parse_list_impl<int ,nb ::int_>(l,p,key);
108
111
} else if (are_floats) {
109
- parse_list_impl<double ,pybind11 ::float_>(l,p,key);
112
+ parse_list_impl<double ,nb ::float_>(l,p,key);
110
113
} else if (are_strings) {
111
- parse_list_impl<std::string,pybind11 ::str>(l,p,key);
114
+ parse_list_impl<std::string,nb ::str>(l,p,key);
112
115
} else {
113
116
EKAT_ERROR_MSG (" Unrecognized/unsupported list entry type.\n " );
114
117
}
115
118
}
116
119
117
120
template <typename Txx, typename Tpy>
118
- void parse_list_impl (const pybind11 ::list& l, ekat::ParameterList& p, const std::string& key) {
121
+ void parse_list_impl (const nb ::list& l, ekat::ParameterList& p, const std::string& key) {
119
122
std::vector<Txx> vals;
120
123
for (auto item : l) {
121
- EKAT_REQUIRE_MSG (pybind11 ::isinstance<Tpy>(item),
124
+ EKAT_REQUIRE_MSG (nb ::isinstance<Tpy>(item),
122
125
" Error! Inconsistent types in list entries.\n " );
123
- auto item_py = pybind11 ::cast<Tpy>(item);
124
- vals.push_back (item_py. template cast <Txx>());
126
+ auto item_py = nb ::cast<Tpy>(item);
127
+ vals.push_back (nb:: cast<Txx>(item_py ));
125
128
}
126
129
p.set (key,vals);
127
130
}
128
131
};
129
132
130
- inline void pybind_pyparamlist (pybind11:: module & m)
133
+ inline void nb_pyparamlist (nb::module_ & m)
131
134
{
132
135
// Param list
133
- pybind11 ::class_<PyParamList>(m," ParameterList" )
134
- .def (pybind11 ::init<const pybind11 ::dict&>())
135
- .def (pybind11 ::init<const pybind11 ::dict&,const std::string&>())
136
+ nb ::class_<PyParamList>(m," ParameterList" )
137
+ .def (nb ::init<const nb ::dict&>())
138
+ .def (nb ::init<const nb ::dict&,const std::string&>())
136
139
.def (" sublist" ,&PyParamList::sublist)
137
140
.def (" print" ,&PyParamList::print)
138
141
.def (" set" ,&PyParamList::set<bool >)
0 commit comments