Skip to content

1324 Direclty print output from print_table in python #1325

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pycode/examples/simulation/ode_seir_flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ def run_ode_seir_flows_simulation():
# Run flow simulation
(result, flows) = simulate_flows(0, days, dt, model)

print(result.print_table(["S", "E", "I", "R"], 16, 5))
print(flows.print_table(["S->E", "E->I", "I->R"], 16, 5))
result.print_table(["S", "E", "I", "R"], 16, 5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
result.print_table(["S", "E", "I", "R"], 16, 5)
result.print_table(False, ["S", "E", "I", "R"], 16, 5)

flows.print_table(["S->E", "E->I", "I->R"], 16, 5)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
flows.print_table(["S->E", "E->I", "I->R"], 16, 5)
flows.print_table(False, ["S->E", "E->I", "I->R"], 16, 5)



if __name__ == "__main__":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,23 @@ void bind_time_series(py::module_& m, std::string const& name)
py::is_operator(), py::arg("index"), py::arg("v"))
.def(
"print_table",
[](const mio::TimeSeries<double>& self, const std::vector<std::string>& column_labels, size_t width,
size_t precision, char separator, const std::string& header_prefix) {
std::ostringstream oss;
self.print_table(oss, column_labels, width, precision, separator, header_prefix);
return oss.str();
[](const mio::TimeSeries<double>& self, bool return_string, const std::vector<std::string>& column_labels,
size_t width, size_t precision, char separator, const std::string& header_prefix) {
if (return_string) {
std::ostringstream oss;
self.print_table(oss, column_labels, width, precision, separator, header_prefix);
return py::object(py::str(oss.str()));
}
else {
self.print_table(column_labels, width, precision, separator, header_prefix);
return py::object(py::none());
}
},
py::arg("column_labels") = std::vector<std::string>{}, py::arg("width") = 16, py::arg("precision") = 5,
py::arg("separator") = ' ', py::arg("header_prefix") = "\n")

"Prints the TimeSeries as a formatted table. If return_string is true, the table is returned as a "
"string. Otherwise, it is printed to the console.",
py::arg("return_string") = false, py::arg("column_labels") = std::vector<std::string>{},
py::arg("width") = 16, py::arg("precision") = 5, py::arg("separator") = ' ',
py::arg("header_prefix") = "\n")
.def(
"export_csv",
[](const mio::TimeSeries<double>& self, const std::string& filename,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def test_print_table(self):
ts = mio.TimeSeries(1)
ts.add_time_point(2, np.r_[1])
ts.add_time_point(3.5, np.r_[2])
output = ts.print_table(["a", "b"], 2, 2)
output = ts.print_table(True, ["a", "b"], 2, 2)
self.assertEqual(
output, '\nTime a \n2.00 1.00\n3.50 2.00\n')

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

def test_print_table_console_output(self):
"""Test the new print_table function that prints directly to console"""
ts = mio.TimeSeries(1)
ts.add_time_point(2, np.r_[1])
ts.add_time_point(3.5, np.r_[2])

# Test that print_table() without return_string parameter returns None
# (meaning it prints directly to console)
result = ts.print_table()
self.assertIsNone(result)

# Test that print_table() with custom parameters still prints to console
result = ts.print_table(False, ["Column1"], 10, 2)
self.assertIsNone(result)

def test_export_csv(self):
"""Test export_csv functionality"""

Expand Down