python: Improve access to device_addr_t

This makes two changes:
- Add device_addr_t as a type that the Python property tree access can
  handle
- Add a to_dict() call to device_addr_t (in Python) that will return
  a regular dictionary from a device_addr_t for a more Pythonic access.
This commit is contained in:
Martin Braun 2020-11-19 15:14:27 +01:00 committed by michael-west
parent 5cbcdd93d3
commit a0721d79e4
2 changed files with 9 additions and 1 deletions

View File

@ -6,6 +6,7 @@
#include "property_tree_python.hpp"
#include <uhd/property_tree.hpp>
#include <uhd/types/device_addr.hpp>
#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <string>
@ -39,6 +40,7 @@ void export_property_tree(py::module& m)
export_property<double>(m, "double");
export_property<std::string>(m, "str");
export_property<bool>(m, "bool");
export_property<uhd::device_addr_t>(m, "device_addr");
py::class_<property_tree>(m, "property_tree")
.def("subtree", &property_tree::subtree, py::arg("path"))
@ -56,6 +58,9 @@ void export_property_tree(py::module& m)
.def("access_bool",
&property_tree::access<bool>,
py::return_value_policy::reference)
.def("access_device_addr",
&property_tree::access<uhd::device_addr_t>,
py::return_value_policy::reference)
// End of types
;
}

View File

@ -43,7 +43,10 @@ void export_types(py::module& m)
// Methods
.def("__str__", &uhd::device_addr_t::to_pp_string)
.def("to_string", &uhd::device_addr_t::to_string)
.def("to_pp_string", &uhd::device_addr_t::to_pp_string);
.def("to_pp_string", &uhd::device_addr_t::to_pp_string)
.def("to_dict", [](uhd::device_addr_t& self) {
return static_cast<std::map<std::string, std::string>>(self);
});
// This will allow functions in Python that take a device_addr to also take
// a string:
py::implicitly_convertible<std::string, uhd::device_addr_t>();