Skip to content

Commit

Permalink
Adapt to refactored Python bindings after rebasing
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Nov 14, 2023
1 parent 64e570b commit 9c51435
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 251 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,6 @@ if(openPMD_HAVE_PYTHON)
src/binding/python/openPMD.cpp
src/binding/python/Access.cpp
src/binding/python/Attributable.cpp
src/binding/python/BaseRecord.cpp
src/binding/python/BaseRecordComponent.cpp
src/binding/python/ChunkInfo.cpp
src/binding/python/Dataset.cpp
Expand Down
6 changes: 6 additions & 0 deletions include/openPMD/binding/python/Common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "openPMD/ParticlePatches.hpp"
#include "openPMD/ParticleSpecies.hpp"
#include "openPMD/Record.hpp"
#include "openPMD/RecordComponent.hpp"
#include "openPMD/Series.hpp"
#include "openPMD/backend/BaseRecord.hpp"
#include "openPMD/backend/BaseRecordComponent.hpp"
Expand Down Expand Up @@ -42,6 +43,9 @@ using PyPatchRecordContainer = Container<PatchRecord>;
using PyRecordComponentContainer = Container<RecordComponent>;
using PyMeshRecordComponentContainer = Container<MeshRecordComponent>;
using PyPatchRecordComponentContainer = Container<PatchRecordComponent>;
using PyBaseRecordRecordComponent = BaseRecord<RecordComponent>;
using PyBaseRecordMeshRecordComponent = BaseRecord<MeshRecordComponent>;
using PyBaseRecordPatchRecordComponent = BaseRecord<PatchRecordComponent>;
PYBIND11_MAKE_OPAQUE(PyIterationContainer)
PYBIND11_MAKE_OPAQUE(PyMeshContainer)
PYBIND11_MAKE_OPAQUE(PyPartContainer)
Expand All @@ -51,3 +55,5 @@ PYBIND11_MAKE_OPAQUE(PyPatchRecordContainer)
PYBIND11_MAKE_OPAQUE(PyRecordComponentContainer)
PYBIND11_MAKE_OPAQUE(PyMeshRecordComponentContainer)
PYBIND11_MAKE_OPAQUE(PyPatchRecordComponentContainer)
PYBIND11_MAKE_OPAQUE(PyBaseRecordRecordComponent)
PYBIND11_MAKE_OPAQUE(PyBaseRecordPatchRecordComponent)
29 changes: 9 additions & 20 deletions include/openPMD/binding/python/Container.H
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
#include "openPMD/backend/MeshRecordComponent.hpp"
#include "openPMD/backend/PatchRecord.hpp"
#include "openPMD/backend/PatchRecordComponent.hpp"
#include "openPMD/binding/python/Container.hpp"

#include "openPMD/binding/python/Common.hpp"

Expand All @@ -47,16 +46,14 @@ namespace openPMD
*
* BSD-style license, see pybind11 LICENSE file.
*/
template <
typename Map,
typename holder_type = std::unique_ptr<Map>,
typename... Args>
py::class_<Map, holder_type, Attributable>
declare_container(py::handle scope, std::string const &name, Args &&...args)
template <typename Map, typename... Args>
py::class_<Map, std::unique_ptr<Map>, Args...>
declare_container(py::handle scope, std::string const &name)
{
using holder_type = std::unique_ptr<Map>;
using KeyType = typename Map::key_type;
using MappedType = typename Map::mapped_type;
using Class_ = py::class_<Map, holder_type, Attributable>;
using Class_ = py::class_<Map, holder_type, Args...>;

// If either type is a non-module-local bound type then make the map
// binding non-local as well; otherwise (e.g. both types are either
Expand All @@ -69,13 +66,9 @@ declare_container(py::handle scope, std::string const &name, Args &&...args)
local = !tinfo || tinfo->module_local;
}

Class_ cl(
scope,
name.c_str(),
py::module_local(local),
std::forward<Args>(args)...);
Class_ cl(scope, name.c_str(), py::module_local(local));

cl.def(py::init<Map const &>());
// cl.def(py::init<Map const &>());

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

// Register stream insertion operator (if possible)
py::detail::map_if_insertion_operator<Map, Class_>(cl, name);
Expand Down Expand Up @@ -110,15 +103,11 @@ declare_container(py::handle scope, std::string const &name, Args &&...args)
return cl;
}

template <
typename Map,
typename holder_type = std::unique_ptr<Map>>
py::class_<Map, holder_type, Attributable>
finalize_container(py::class_<Map, holder_type, Attributable> cl)
template <typename Map, typename Class_>
Class_ finalize_container(Class_ cl)
{
using KeyType = typename Map::key_type;
using MappedType = typename Map::mapped_type;
using Class_ = py::class_<Map, holder_type, Attributable>;

cl.def(
"items",
Expand Down
152 changes: 0 additions & 152 deletions include/openPMD/binding/python/Container.hpp

This file was deleted.

7 changes: 7 additions & 0 deletions include/openPMD/binding/python/RecordComponent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ py::array load_chunk(RecordComponent &r, py::tuple const &slices);

void store_chunk(RecordComponent &r, py::array &a, py::tuple const &slices);

namespace docstring
{
constexpr static char const *is_scalar = R"docstr(
Returns true if this record only contains a single component.
)docstr";
}

template <typename Class>
Class &&addRecordComponentSetGet(Class &&class_)
{
Expand Down
59 changes: 0 additions & 59 deletions src/binding/python/BaseRecord.cpp

This file was deleted.

5 changes: 3 additions & 2 deletions src/binding/python/Iteration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
*/
#include "openPMD/Iteration.hpp"

#include "openPMD/backend/Attributable.hpp"
#include "openPMD/binding/python/Common.hpp"
#include "openPMD/binding/python/Container.H"

Expand All @@ -29,8 +30,8 @@

void init_Iteration(py::module &m)
{
auto py_it_cont =
declare_container<PyIterationContainer>(m, "Iteration_Container");
auto py_it_cont = declare_container<PyIterationContainer, Attributable>(
m, "Iteration_Container");

py::class_<Iteration, Attributable>(m, "Iteration")
.def(py::init<Iteration const &>())
Expand Down
4 changes: 3 additions & 1 deletion src/binding/python/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "openPMD/Mesh.hpp"
#include "openPMD/backend/Attributable.hpp"
#include "openPMD/backend/BaseRecord.hpp"
#include "openPMD/backend/MeshRecordComponent.hpp"

Expand All @@ -32,7 +33,8 @@

void init_Mesh(py::module &m)
{
auto py_m_cont = declare_container<PyMeshContainer>(m, "Mesh_Container");
auto py_m_cont =
declare_container<PyMeshContainer, Attributable>(m, "Mesh_Container");

py::class_<Mesh, BaseRecord<MeshRecordComponent> > cl(m, "Mesh");

Expand Down
17 changes: 15 additions & 2 deletions src/binding/python/MeshRecordComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,20 @@
#include "openPMD/RecordComponent.hpp"
#include "openPMD/Series.hpp"

#include "openPMD/backend/Attributable.hpp"
#include "openPMD/binding/python/Common.hpp"
#include "openPMD/binding/python/Container.H"
#include "openPMD/binding/python/Pickle.hpp"
#include "openPMD/binding/python/RecordComponent.hpp"

#include <string>
#include <vector>

void init_MeshRecordComponent(py::module &m)
{
auto py_mrc_cnt = declare_container<PyMeshRecordComponentContainer>(
m, "Mesh_Record_Component_Container");
auto py_mrc_cnt =
declare_container<PyMeshRecordComponentContainer, Attributable>(
m, "Mesh_Record_Component_Container");

py::class_<MeshRecordComponent, RecordComponent> cl(
m, "Mesh_Record_Component");
Expand Down Expand Up @@ -85,4 +88,14 @@ void init_MeshRecordComponent(py::module &m)
});

finalize_container<PyMeshRecordComponentContainer>(py_mrc_cnt);
addRecordComponentSetGet(
finalize_container<PyBaseRecordMeshRecordComponent>(
declare_container<
PyBaseRecordMeshRecordComponent,
PyMeshRecordComponentContainer,
MeshRecordComponent>(m, "Base_Record_Mesh_Record_Component")))
.def_property_readonly(
"scalar",
&BaseRecord<MeshRecordComponent>::scalar,
&docstring::is_scalar[1]);
}
Loading

0 comments on commit 9c51435

Please sign in to comment.