python FieldProperties can return numpy double arrays.

This commit is contained in:
Steinar Foss
2019-12-23 11:35:52 +01:00
parent d6c1d64ba0
commit d3a4b536d2
3 changed files with 23 additions and 13 deletions

View File

@@ -9,15 +9,6 @@
namespace {
py::list getitem( const FieldPropsManager& m, const std::string& kw) {
if (m.has<int>(kw))
return iterable_to_pylist( m.get<int>(kw) );
if (m.has<double>(kw))
return iterable_to_pylist( m.get<double>(kw) );
throw py::key_error( "no such grid property " + kw );
}
bool contains( const FieldPropsManager& manager, const std::string& kw) {
if (manager.has<int>(kw))
return true;
@@ -26,6 +17,21 @@ namespace {
return false;
}
py::array_t<double> get_double_array(const FieldPropsManager& m, const std::string& kw) {
if (m.has<double>(kw))
return convert::numpy_array( m.get<double>(kw) );
else
throw std::invalid_argument("Keyword '" + kw + "'is not of type double.");
}
py::array_t<int> get_int_array(const FieldPropsManager& m, const std::string& kw) {
if (m.has<int>(kw))
return convert::numpy_array( m.get<int>(kw) );
else
throw std::invalid_argument("Keyword '" + kw + "'is not of type int.");
}
}
@@ -33,7 +39,8 @@ void python::common::export_FieldProperties(py::module& module) {
py::class_< FieldPropsManager >( module, "FieldProperties")
.def( "__contains__", &contains )
.def( "__getitem__", &getitem )
.def( "get_double_array", &get_double_array )
.def( "get_int_array", &get_int_array )
;
}