Skip to content

Commit b1843bf

Browse files
HenrZuMaxBetzDLR
andauthored
1324 Direclty print output from print_table in python (#1325)
When calling print_table from python, the output is now direclty printed in the console per default. (Now consistent with cpp code). When adding True as first arg, we also can still return the output as a string. Co-authored-by: MaxBetz <104758467+MaxBetzDLR@users.noreply.github.com>
1 parent c6023dd commit b1843bf

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

pycode/examples/simulation/ode_seir_flows.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ def run_ode_seir_flows_simulation():
7070
# Run flow simulation
7171
(result, flows) = simulate_flows(0, days, dt, model)
7272

73-
print(result.print_table(["S", "E", "I", "R"], 16, 5))
74-
print(flows.print_table(["S->E", "E->I", "I->R"], 16, 5))
73+
result.print_table(False, ["S", "E", "I", "R"], 16, 5)
74+
flows.print_table(False, ["S->E", "E->I", "I->R"], 16, 5)
7575

7676

7777
if __name__ == "__main__":

pycode/memilio-simulation/memilio/simulation/bindings/utils/time_series.cpp

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,23 @@ void bind_time_series(py::module_& m, std::string const& name)
6565
py::is_operator(), py::arg("index"), py::arg("v"))
6666
.def(
6767
"print_table",
68-
[](const mio::TimeSeries<double>& self, const std::vector<std::string>& column_labels, size_t width,
69-
size_t precision, char separator, const std::string& header_prefix) {
70-
std::ostringstream oss;
71-
self.print_table(oss, column_labels, width, precision, separator, header_prefix);
72-
return oss.str();
68+
[](const mio::TimeSeries<double>& self, bool return_string, const std::vector<std::string>& column_labels,
69+
size_t width, size_t precision, char separator, const std::string& header_prefix) {
70+
if (return_string) {
71+
std::ostringstream oss;
72+
self.print_table(oss, column_labels, width, precision, separator, header_prefix);
73+
return py::object(py::str(oss.str()));
74+
}
75+
else {
76+
self.print_table(column_labels, width, precision, separator, header_prefix);
77+
return py::object(py::none());
78+
}
7379
},
74-
py::arg("column_labels") = std::vector<std::string>{}, py::arg("width") = 16, py::arg("precision") = 5,
75-
py::arg("separator") = ' ', py::arg("header_prefix") = "\n")
76-
80+
"Prints the TimeSeries as a formatted table. If return_string is true, the table is returned as a "
81+
"string. Otherwise, it is printed to the console.",
82+
py::arg("return_string") = false, py::arg("column_labels") = std::vector<std::string>{},
83+
py::arg("width") = 16, py::arg("precision") = 5, py::arg("separator") = ' ',
84+
py::arg("header_prefix") = "\n")
7785
.def(
7886
"export_csv",
7987
[](const mio::TimeSeries<double>& self, const std::string& filename,

pycode/memilio-simulation/memilio/simulation_test/test_time_series.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_print_table(self):
6565
ts = mio.TimeSeries(1)
6666
ts.add_time_point(2, np.r_[1])
6767
ts.add_time_point(3.5, np.r_[2])
68-
output = ts.print_table(["a", "b"], 2, 2)
68+
output = ts.print_table(True, ["a", "b"], 2, 2)
6969
self.assertEqual(
7070
output, '\nTime a \n2.00 1.00\n3.50 2.00\n')
7171

@@ -74,10 +74,25 @@ def test_print_table_with_separator(self):
7474
ts = mio.TimeSeries(1)
7575
ts.add_time_point(2, np.r_[1])
7676
ts.add_time_point(3.5, np.r_[2])
77-
output = ts.print_table(["a"], 4, 1, ',', "# ")
77+
output = ts.print_table(True, ["a"], 4, 1, ',', "# ")
7878
self.assertEqual(
7979
output, '# Time,a \n 2.0, 1.0\n 3.5, 2.0\n')
8080

81+
def test_print_table_console_output(self):
82+
"""Test the new print_table function that prints directly to console"""
83+
ts = mio.TimeSeries(1)
84+
ts.add_time_point(2, np.r_[1])
85+
ts.add_time_point(3.5, np.r_[2])
86+
87+
# Test that print_table() without return_string parameter returns None
88+
# (meaning it prints directly to console)
89+
result = ts.print_table()
90+
self.assertIsNone(result)
91+
92+
# Test that print_table() with custom parameters still prints to console
93+
result = ts.print_table(False, ["Column1"], 10, 2)
94+
self.assertIsNone(result)
95+
8196
def test_export_csv(self):
8297
"""Test export_csv functionality"""
8398

0 commit comments

Comments
 (0)