24 #include "pybind11/pybind11.h"
25 #include "pybind11/stl.h"
27 #include "ndarray/pybind11.h"
36 using namespace pybind11::literals;
42 using utils::python::WrapperCollection;
53 void declareArrayKey(WrapperCollection &wrappers,
std::string const &suffix) {
55 PyArrayKey<T>(wrappers.module, (
"Array" + suffix +
"Key").c_str()), [](
auto &mod,
auto &
cls) {
56 cls.def(py::init<>());
57 cls.def(py::init<Key<Array<T>> const &>());
58 cls.def(py::init<SubSchema const &>());
59 cls.def(py::init<std::vector<Key<T>> const &>());
61 cls.def_static(
"addFields",
62 (ArrayKey<T>(*)(Schema &, std::string const &, std::string const &,
63 std::string const &, std::vector<T> const &)) &
64 ArrayKey<T>::addFields,
65 "schema"_a,
"name"_a,
"doc"_a,
"unit"_a,
"docData"_a);
66 cls.def_static(
"addFields",
67 (ArrayKey<T>(*)(Schema &, std::string const &, std::string const &,
68 std::string const &, int size)) &
69 ArrayKey<T>::addFields,
70 "schema"_a,
"name"_a,
"doc"_a,
"unit"_a,
"size"_a);
71 cls.def(
"get", &ArrayKey<T>::get);
72 cls.def(
"set", &ArrayKey<T>::set);
73 cls.def(
"isValid", &ArrayKey<T>::isValid);
74 cls.def(
"__eq__", &ArrayKey<T>::operator==, py::is_operator());
75 cls.def(
"__ne__", &ArrayKey<T>::operator!=, py::is_operator());
76 cls.def(
"__getitem__",
78 [](ArrayKey<T> const &self, py::object const &index) -> py::object {
79 if (py::isinstance<py::slice>(index)) {
80 py::slice slice(index);
81 py::size_t start = 0, stop = 0, step = 0, length = 0;
82 bool valid = slice.compute(self.getSize(), &start, &stop, &step, &length);
83 if (!valid) throw py::error_already_set();
85 throw py::index_error(
"Step for ArrayKey must be 1.");
87 return py::cast(self.slice(start, stop));
89 std::size_t n = utils::python::cppIndex(self.getSize(),
90 py::cast<std::ptrdiff_t>(index));
91 return py::cast(self[n]);
95 cls.def(
"slice", &ArrayKey<T>::slice);
102 declareArrayKey<float>(wrappers,
"F");
103 declareArrayKey<double>(wrappers,
"D");