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); + }) ; }