From b681feb0ffc7ff5cbbd191e6a848f4ce12317476 Mon Sep 17 00:00:00 2001 From: Laurent Perron Date: Sat, 5 Oct 2024 10:54:51 +0200 Subject: [PATCH] add __repr__ to Domain: fix #4399 --- ortools/util/python/BUILD.bazel | 1 + ortools/util/python/sorted_interval_list.cc | 5 +++++ ortools/util/python/sorted_interval_list_test.py | 5 +++++ 3 files changed, 11 insertions(+) diff --git a/ortools/util/python/BUILD.bazel b/ortools/util/python/BUILD.bazel index 925cf571dce..c67d864d577 100644 --- a/ortools/util/python/BUILD.bazel +++ b/ortools/util/python/BUILD.bazel @@ -31,6 +31,7 @@ pybind_extension( deps = [ ":sorted_interval_list_doc", "//ortools/util:sorted_interval_list", + "@com_google_absl//absl/strings", ], ) diff --git a/ortools/util/python/sorted_interval_list.cc b/ortools/util/python/sorted_interval_list.cc index 55e82d54445..d2a15ec55e5 100644 --- a/ortools/util/python/sorted_interval_list.cc +++ b/ortools/util/python/sorted_interval_list.cc @@ -15,6 +15,7 @@ #include +#include "absl/strings/str_cat.h" #include "ortools/util/python/sorted_interval_list_doc.h" #include "pybind11/cast.h" #include "pybind11/pybind11.h" @@ -57,6 +58,10 @@ PYBIND11_MODULE(sorted_interval_list, m) { .def("union_with", &Domain::UnionWith, DOC(operations_research, Domain, UnionWith), arg("domain")) .def("__str__", &Domain::ToString) + .def("__repr__", + [](const Domain& domain) { + return absl::StrCat("Domain(", domain.ToString(), ")"); + }) // Compatibility with pre PEP8 APIs. .def_static("AllValues", &Domain::AllValues, DOC(operations_research, Domain, AllValues)) diff --git a/ortools/util/python/sorted_interval_list_test.py b/ortools/util/python/sorted_interval_list_test.py index b33eb7d878b..2f03099842f 100755 --- a/ortools/util/python/sorted_interval_list_test.py +++ b/ortools/util/python/sorted_interval_list_test.py @@ -85,6 +85,11 @@ def testComplement(self): self.assertEqual([-9223372036854775808, 5], d1.flattened_intervals()) self.assertEqual([6, 9223372036854775807], d2.flattened_intervals()) + def testStr(self): + d1 = sorted_interval_list.Domain(0, 5) + self.assertEqual(str(d1), "[0,5]") + self.assertEqual(repr(d1), "Domain([0,5])") + if __name__ == "__main__": absltest.main()