Skip to content

Commit

Permalink
add __repr__ to Domain: fix #4399
Browse files Browse the repository at this point in the history
  • Loading branch information
lperron committed Oct 5, 2024
1 parent eb3684f commit b681feb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions ortools/util/python/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pybind_extension(
deps = [
":sorted_interval_list_doc",
"//ortools/util:sorted_interval_list",
"@com_google_absl//absl/strings",
],
)

Expand Down
5 changes: 5 additions & 0 deletions ortools/util/python/sorted_interval_list.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

#include <cstdint>

#include "absl/strings/str_cat.h"
#include "ortools/util/python/sorted_interval_list_doc.h"
#include "pybind11/cast.h"
#include "pybind11/pybind11.h"
Expand Down Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions ortools/util/python/sorted_interval_list_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

0 comments on commit b681feb

Please sign in to comment.