From 6dcc89ad043834c0d021c49ae88451e0beda9e5d Mon Sep 17 00:00:00 2001 From: Joachim Schoeberl Date: Tue, 1 Jun 2021 12:57:58 +0200 Subject: [PATCH] some table py-features --- libsrc/core/python_ngcore.hpp | 36 ++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/libsrc/core/python_ngcore.hpp b/libsrc/core/python_ngcore.hpp index d94ed122..dc54b1e1 100644 --- a/libsrc/core/python_ngcore.hpp +++ b/libsrc/core/python_ngcore.hpp @@ -261,7 +261,41 @@ namespace ngcore template void ExportTable (py::module &m) { - py::class_, std::shared_ptr>> (m, ("Table+"+GetPyName()).c_str()) + py::class_, std::shared_ptr>> (m, ("Table_"+GetPyName()).c_str()) + .def(py::init([] (py::list blocks) + { + size_t size = py::len(blocks); + Array cnt(size); + size_t i = 0; + for (auto block : blocks) + cnt[i++] = py::len(block); + + i = 0; + Table blocktable(cnt); + for (auto block : blocks) + { + auto row = blocktable[i++]; + size_t j = 0; + for (auto val : block) + row[j++] = val.cast(); + } + // cout << "blocktable = " << *blocktable << endl; + return blocktable; + + }), py::arg("blocks"), "a list of lists") + + .def ("__len__", [] (Table &self ) { return self.Size(); } ) + .def ("__getitem__", + [](Table & self, size_t i) -> FlatArray + { + if (i >= self.Size()) + throw py::index_error(); + return self[i]; + }) + .def("__str__", [](Table & self) + { + return ToString(self); + }) ; }