Skip to content
Snippets Groups Projects
Verified Commit 0a8dcb35 authored by Maik Nijhuis's avatar Maik Nijhuis
Browse files

Use proper definition for python Steps

Using 'Step' instead of 'StepWrapper' as base class allows using
all steps from python.

Add std::shared_ptr<Step> holder, besides the PyStepImpl trampoline.

Remove 'count' member since python steps can implement it themselves.
parent 2f567fb5
No related branches found
No related tags found
1 merge request!824AST-1091 Use proper definition for python Steps
Pipeline #38968 skipped
......@@ -42,8 +42,6 @@ void PyStepImpl::finish() {
}
bool PyStepImpl::process(const DPBuffer& bufin) {
m_count++;
// Make a deep copy of the buffer to make the data
// persistent across multiple process calls
// This is not always necessary, but for python Steps
......@@ -53,25 +51,25 @@ bool PyStepImpl::process(const DPBuffer& bufin) {
PYBIND11_OVERRIDE_PURE(
bool, /* Return type */
StepWrapper, /* Parent class */
Step, /* Parent class */
process, /* Name of function in C++ (must match Python name) */
dpbuffer /* Argument(s) */
);
}
common::Fields PyStepImpl::getRequiredFields() const {
PYBIND11_OVERRIDE_PURE_NAME(common::Fields, StepWrapper,
"get_required_fields", getRequiredFields, );
PYBIND11_OVERRIDE_PURE_NAME(common::Fields, Step, "get_required_fields",
getRequiredFields, );
}
common::Fields PyStepImpl::getProvidedFields() const {
PYBIND11_OVERRIDE_PURE_NAME(common::Fields, StepWrapper,
"get_provided_fields", getProvidedFields, );
PYBIND11_OVERRIDE_PURE_NAME(common::Fields, Step, "get_provided_fields",
getProvidedFields, );
}
void PyStepImpl::updateInfo(const DPInfo& dpinfo) {
PYBIND11_OVERRIDE_NAME(void, /* Return type */
StepWrapper, /* Parent class */
Step, /* Parent class */
"update_info", /* Name of function in Python */
updateInfo, /* Name of function in C++ */
dpinfo /* Argument(s) */
......
......@@ -30,33 +30,18 @@ class ostream_wrapper {
std::ostream& os_;
};
/**
* Wrapper class that exposes protected members, so pybind11 can use those.
*/
class StepWrapper : public steps::Step {
public:
using Step::info;
using Step::Step;
using Step::updateInfo;
using Step::getNextStep;
Step::ShPtr get_next_step() { return Step::ShPtr(getNextStep()); }
bool process_next_step(const base::DPBuffer& dpbuffer) {
return get_next_step()->process(dpbuffer);
}
int get_count() { return m_count; }
void set_parset(const common::ParameterSet& parset) { m_parset = parset; };
void set_name(const string& name) { m_name = name; };
protected:
int m_count = 0;
steps::InputStep* m_input;
common::ParameterSet m_parset;
string m_name;
const base::DPBuffer* m_dpbuffer_in;
common::NSTimer m_timer;
};
/**
* Trampoline class for python steps.
*/
class PyStepImpl final : public StepWrapper {
public:
using StepWrapper::StepWrapper;
......
......@@ -16,6 +16,7 @@
using dp3::base::DPBuffer;
using dp3::base::DPInfo;
using dp3::common::Fields;
using dp3::steps::Step;
namespace py = pybind11;
......@@ -86,26 +87,28 @@ PYBIND11_MODULE(pydp3, m) {
py::class_<ostream_wrapper>(m, "ostream")
.def("write", &ostream_wrapper::write);
py::class_<StepWrapper, PyStepImpl /* <--- trampoline*/
>(m, "Step")
py::class_<Step, PyStepImpl, std::shared_ptr<Step>>(m, "Step")
.def(py::init<>())
.def("show", &StepWrapper::show,
.def("show", &Step::show,
"Show step summary (stdout will be redirected to DPPP's output "
"stream during this step)")
.def("update_info", &StepWrapper::updateInfo, "Handle metadata")
.def("info", &StepWrapper::info, py::return_value_policy::reference,
"Get info object (read/write) with metadata")
.def("finish", &StepWrapper::finish,
.def(
"process",
// Use a lambda, since process is overloaded.
[](Step& self, const DPBuffer& buffer) {
return self.process(buffer);
},
"Process a single buffer")
.def("finish", &Step::finish,
"Finish processing (nextstep->finish will be called automatically")
.def("get_next_step", &StepWrapper::get_next_step,
py::return_value_policy::copy, "Get a reference to the next step")
.def("process_next_step", &StepWrapper::process_next_step,
"Process the next step")
.def("get_count", &StepWrapper::get_count,
"Get the number of time slots processed")
.def("get_required_fields", &StepWrapper::getRequiredFields,
.def("get_next_step", &Step::getNextStep,
"Get a reference to the next step")
.def("get_required_fields", &Step::getRequiredFields,
"Get the fields required by current step")
.def("get_provided_fields", &StepWrapper::getProvidedFields,
.def("get_provided_fields", &Step::getProvidedFields,
"Get the fields provided by current step");
py::class_<DPBuffer, std::shared_ptr<DPBuffer>>(m, "DPBuffer")
......
......@@ -51,7 +51,7 @@ class MockPyStep(Step):
def process(self, dpbuffer):
"""
Process one time slot of data. This function MUST call process_next_step.
Process one time slot of data. This function MUST call get_next_step().process().
Args:
dpbuffer: DPBuffer object which can contain data, flags and weights
......@@ -66,7 +66,7 @@ class MockPyStep(Step):
weights *= self.weightsfactor
# Send processed data to the next step
self.process_next_step(dpbuffer)
self.get_next_step().process(dpbuffer)
def finish(self):
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment