From bd4555ade71dc80f531442d36330f3d88c07af3d Mon Sep 17 00:00:00 2001 From: avdudchenko <33663878+avdudchenko@users.noreply.github.com> Date: Thu, 24 Apr 2025 21:59:33 -0400 Subject: [PATCH 1/9] Update formatting.py --- pyomo/common/formatting.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pyomo/common/formatting.py b/pyomo/common/formatting.py index 71798565417..b410071d5d1 100644 --- a/pyomo/common/formatting.py +++ b/pyomo/common/formatting.py @@ -102,7 +102,7 @@ def tostr(value, quote_str=False): } -def tabular_writer(ostream, prefix, data, header, row_generator): +def tabular_writer(ostream, prefix, data, header, row_generator, sort_rows=True): """Output data in tabular form Parameters @@ -121,7 +121,8 @@ def tabular_writer(ostream, prefix, data, header, row_generator): returns either a tuple defining the entries for a single row, or a generator that returns a sequence of table rows to be output for the specified `key` - + sort_rows: bool + sets if rows should be sorted using robust_sort, default (True) """ prefix = tostr(prefix) @@ -186,6 +187,8 @@ def tabular_writer(ostream, prefix, data, header, row_generator): if any(' ' in r[-1] for x in _rows.values() if x is not None for r in x): _width[-1] = '%s' + if sort_rows: + _rows=sort_rows(_rows) for _key in sorted_robust(_rows): _rowSet = _rows[_key] if not _rowSet: From 779a20debacbe5f094cc1958a2168eab78a37cec Mon Sep 17 00:00:00 2001 From: avdudchenko <33663878+avdudchenko@users.noreply.github.com> Date: Thu, 24 Apr 2025 22:06:03 -0400 Subject: [PATCH 2/9] fixed_error --- pyomo/common/formatting.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyomo/common/formatting.py b/pyomo/common/formatting.py index b410071d5d1..6c863f6c8e3 100644 --- a/pyomo/common/formatting.py +++ b/pyomo/common/formatting.py @@ -188,8 +188,8 @@ def tabular_writer(ostream, prefix, data, header, row_generator, sort_rows=True) if any(' ' in r[-1] for x in _rows.values() if x is not None for r in x): _width[-1] = '%s' if sort_rows: - _rows=sort_rows(_rows) - for _key in sorted_robust(_rows): + _rows=sorted_robust(_rows) + for _key in _rows: _rowSet = _rows[_key] if not _rowSet: _rowSet = [[_key] + [None] * (len(_width) - 1)] From 03a91d1bd118d513ddf43bb707224eb713bc3f79 Mon Sep 17 00:00:00 2001 From: avdudchenko <33663878+avdudchenko@users.noreply.github.com> Date: Fri, 25 Apr 2025 17:40:36 -0400 Subject: [PATCH 3/9] Fixed ordering issue --- pyomo/common/formatting.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pyomo/common/formatting.py b/pyomo/common/formatting.py index 6c863f6c8e3..298ef7ec6cf 100644 --- a/pyomo/common/formatting.py +++ b/pyomo/common/formatting.py @@ -188,8 +188,10 @@ def tabular_writer(ostream, prefix, data, header, row_generator, sort_rows=True) if any(' ' in r[-1] for x in _rows.values() if x is not None for r in x): _width[-1] = '%s' if sort_rows: - _rows=sorted_robust(_rows) - for _key in _rows: + _sorted_rows=sorted_robust(_rows) + else: + _sorted_rows=_rows + for _key in _sorted_rows: _rowSet = _rows[_key] if not _rowSet: _rowSet = [[_key] + [None] * (len(_width) - 1)] From 89b63a57913ec2d5b84e4a922afe218026d1cfad Mon Sep 17 00:00:00 2001 From: avdudchenko <33663878+avdudchenko@users.noreply.github.com> Date: Mon, 28 Apr 2025 11:35:20 -0400 Subject: [PATCH 4/9] Update test_formatting.py --- pyomo/common/tests/test_formatting.py | 105 +++++++++++++++----------- 1 file changed, 62 insertions(+), 43 deletions(-) diff --git a/pyomo/common/tests/test_formatting.py b/pyomo/common/tests/test_formatting.py index 0798a80589c..43ade6a8efc 100644 --- a/pyomo/common/tests/test_formatting.py +++ b/pyomo/common/tests/test_formatting.py @@ -33,36 +33,36 @@ class DerivedStr(str): pass -NamedTuple = namedtuple('NamedTuple', ['x', 'y']) +NamedTuple = namedtuple("NamedTuple", ["x", "y"]) class TestToStr(unittest.TestCase): def test_new_type_float(self): - self.assertEqual(tostr(0.5), '0.5') + self.assertEqual(tostr(0.5), "0.5") self.assertIs(tostr.handlers[float], tostr.handlers[None]) def test_new_type_int(self): - self.assertEqual(tostr(0), '0') + self.assertEqual(tostr(0), "0") self.assertIs(tostr.handlers[int], tostr.handlers[None]) def test_new_type_str(self): - self.assertEqual(tostr(DerivedStr(1)), '1') + self.assertEqual(tostr(DerivedStr(1)), "1") self.assertIs(tostr.handlers[DerivedStr], tostr.handlers[str]) def test_new_type_list(self): - self.assertEqual(tostr(DerivedList([1, 2])), '[1, 2]') + self.assertEqual(tostr(DerivedList([1, 2])), "[1, 2]") self.assertIs(tostr.handlers[DerivedList], tostr.handlers[list]) def test_new_type_dict(self): - self.assertEqual(tostr(DerivedDict({1: 2})), '{1: 2}') + self.assertEqual(tostr(DerivedDict({1: 2})), "{1: 2}") self.assertIs(tostr.handlers[DerivedDict], tostr.handlers[dict]) def test_new_type_tuple(self): - self.assertEqual(tostr(DerivedTuple([1, 2])), '(1, 2)') + self.assertEqual(tostr(DerivedTuple([1, 2])), "(1, 2)") self.assertIs(tostr.handlers[DerivedTuple], tostr.handlers[tuple]) def test_new_type_namedtuple(self): - self.assertEqual(tostr(NamedTuple(1, 2)), 'NamedTuple(x=1, y=2)') + self.assertEqual(tostr(NamedTuple(1, 2)), "NamedTuple(x=1, y=2)") self.assertIs(tostr.handlers[NamedTuple], tostr.handlers[None]) @@ -73,7 +73,7 @@ def test_unicode_table(self): os = StringIO() data = {1: ("a", 1), (2, 3): ("∧", 2)} tabular_writer(os, "", data.items(), ["s", "val"], lambda k, v: v) - ref = u""" + ref = """ Key : s : val 1 : a : 1 (2, 3) : ∧ : 2 @@ -82,9 +82,9 @@ def test_unicode_table(self): def test_tuple_list_dict(self): os = StringIO() - data = {(1,): (["a", 1], 1), ('2', 3): ({1: 'a', 2: '2'}, '2')} + data = {(1,): (["a", 1], 1), ("2", 3): ({1: "a", 2: "2"}, "2")} tabular_writer(os, "", data.items(), ["s", "val"], lambda k, v: v) - ref = u""" + ref = """ Key : s : val (1,) : ['a', 1] : 1 ('2', 3) : {1: 'a', 2: '2'} : 2 @@ -93,9 +93,9 @@ def test_tuple_list_dict(self): def test_no_header(self): os = StringIO() - data = {(2,): (["a", 1], 1), (1, 3): ({1: 'a', 2: '2'}, '2')} + data = {(2,): (["a", 1], 1), (1, 3): ({1: "a", 2: "2"}, "2")} tabular_writer(os, "", data.items(), [], lambda k, v: v) - ref = u""" + ref = """ {1: 'a', 2: '2'} : 2 ['a', 1] : 1 """ @@ -104,22 +104,22 @@ def test_no_header(self): def test_no_data(self): os = StringIO() data = {} - tabular_writer(os, "", data.items(), ['s', 'val'], lambda k, v: v) - ref = u""" + tabular_writer(os, "", data.items(), ["s", "val"], lambda k, v: v) + ref = """ Key : s : val """ self.assertEqual(ref.strip(), os.getvalue().strip()) def test_multiline_generator(self): os = StringIO() - data = {'a': 0, 'b': 1, 'c': 3} + data = {"a": 0, "b": 1, "c": 3} def _data_gen(i, j): for n in range(j): - yield (n, chr(ord('a') + n) * j) + yield (n, chr(ord("a") + n) * j) - tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) - ref = u""" + tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) + ref = """ Key : i : j a : None : None b : 0 : a @@ -129,18 +129,37 @@ def _data_gen(i, j): """ self.assertEqual(ref.strip(), os.getvalue().strip()) + def test_multiline_generator_user_sorted(self): + os = StringIO() + data = {"b": 0, "a": 1, "c": 3} + + def _data_gen(i, j): + for n in range(j): + yield (n, chr(ord("a") + n) * j) + + tabular_writer(os, "", data.items(), ["i", "j"], _data_gen, sort_rows=False) + ref = """ +Key : i : j + b : None : None + a : 0 : a + c : 0 : aaa + : 1 : bbb + : 2 : ccc +""" + self.assertEqual(ref.strip(), os.getvalue().strip()) + def test_multiline_generator_exception(self): os = StringIO() - data = {'a': 0, 'b': 1, 'c': 3} + data = {"a": 0, "b": 1, "c": 3} def _data_gen(i, j): - if i == 'b': + if i == "b": raise ValueError("invalid") for n in range(j): - yield (n, chr(ord('a') + n) * j) + yield (n, chr(ord("a") + n) * j) - tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) - ref = u""" + tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) + ref = """ Key : i : j a : None : None b : None : None @@ -152,15 +171,15 @@ def _data_gen(i, j): def test_data_exception(self): os = StringIO() - data = {'a': 0, 'b': 1, 'c': 3} + data = {"a": 0, "b": 1, "c": 3} def _data_gen(i, j): - if i == 'b': + if i == "b": raise ValueError("invalid") return (j, i * (j + 1)) - tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) - ref = u""" + tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) + ref = """ Key : i : j a : 0 : a b : None : None @@ -170,19 +189,19 @@ def _data_gen(i, j): def test_multiline_alignment(self): os = StringIO() - data = {'a': 1, 'b': 2, 'c': 3} + data = {"a": 1, "b": 2, "c": 3} def _data_gen(i, j): for n in range(j): - _str = chr(ord('a') + n) * (j + 1) + _str = chr(ord("a") + n) * (j + 1) if n % 2: _str = list(_str) - _str[1] = ' ' - _str = ''.join(_str) + _str[1] = " " + _str = "".join(_str) yield (n, _str) - tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) - ref = u""" + tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) + ref = """ Key : i : j a : 0 : aa b : 0 : aaa @@ -198,24 +217,24 @@ class TestStreamIndenter(unittest.TestCase): def test_noprefix(self): OUT1 = StringIO() OUT2 = StreamIndenter(OUT1) - OUT2.write('Hello?\nHello, world!') - self.assertEqual(' Hello?\n Hello, world!', OUT2.getvalue()) + OUT2.write("Hello?\nHello, world!") + self.assertEqual(" Hello?\n Hello, world!", OUT2.getvalue()) def test_prefix(self): - prefix = 'foo:' + prefix = "foo:" OUT1 = StringIO() OUT2 = StreamIndenter(OUT1, prefix) - OUT2.write('Hello?\nHello, world!') - self.assertEqual('foo:Hello?\nfoo:Hello, world!', OUT2.getvalue()) + OUT2.write("Hello?\nHello, world!") + self.assertEqual("foo:Hello?\nfoo:Hello, world!", OUT2.getvalue()) def test_blank_lines(self): OUT1 = StringIO() OUT2 = StreamIndenter(OUT1) - OUT2.write('Hello?\n\nText\n\nHello, world!') - self.assertEqual(' Hello?\n\n Text\n\n Hello, world!', OUT2.getvalue()) + OUT2.write("Hello?\n\nText\n\nHello, world!") + self.assertEqual(" Hello?\n\n Text\n\n Hello, world!", OUT2.getvalue()) def test_writelines(self): OUT1 = StringIO() OUT2 = StreamIndenter(OUT1) - OUT2.writelines(['Hello?\n', '\n', 'Text\n', '\n', 'Hello, world!']) - self.assertEqual(' Hello?\n\n Text\n\n Hello, world!', OUT2.getvalue()) + OUT2.writelines(["Hello?\n", "\n", "Text\n", "\n", "Hello, world!"]) + self.assertEqual(" Hello?\n\n Text\n\n Hello, world!", OUT2.getvalue()) From fe5c69771fcf55a982eb1ff099e863758282cc77 Mon Sep 17 00:00:00 2001 From: avdudchenko <33663878+avdudchenko@users.noreply.github.com> Date: Mon, 28 Apr 2025 11:37:52 -0400 Subject: [PATCH 5/9] Revert "Update test_formatting.py" This reverts commit 89b63a57913ec2d5b84e4a922afe218026d1cfad. --- pyomo/common/tests/test_formatting.py | 105 +++++++++++--------------- 1 file changed, 43 insertions(+), 62 deletions(-) diff --git a/pyomo/common/tests/test_formatting.py b/pyomo/common/tests/test_formatting.py index 43ade6a8efc..0798a80589c 100644 --- a/pyomo/common/tests/test_formatting.py +++ b/pyomo/common/tests/test_formatting.py @@ -33,36 +33,36 @@ class DerivedStr(str): pass -NamedTuple = namedtuple("NamedTuple", ["x", "y"]) +NamedTuple = namedtuple('NamedTuple', ['x', 'y']) class TestToStr(unittest.TestCase): def test_new_type_float(self): - self.assertEqual(tostr(0.5), "0.5") + self.assertEqual(tostr(0.5), '0.5') self.assertIs(tostr.handlers[float], tostr.handlers[None]) def test_new_type_int(self): - self.assertEqual(tostr(0), "0") + self.assertEqual(tostr(0), '0') self.assertIs(tostr.handlers[int], tostr.handlers[None]) def test_new_type_str(self): - self.assertEqual(tostr(DerivedStr(1)), "1") + self.assertEqual(tostr(DerivedStr(1)), '1') self.assertIs(tostr.handlers[DerivedStr], tostr.handlers[str]) def test_new_type_list(self): - self.assertEqual(tostr(DerivedList([1, 2])), "[1, 2]") + self.assertEqual(tostr(DerivedList([1, 2])), '[1, 2]') self.assertIs(tostr.handlers[DerivedList], tostr.handlers[list]) def test_new_type_dict(self): - self.assertEqual(tostr(DerivedDict({1: 2})), "{1: 2}") + self.assertEqual(tostr(DerivedDict({1: 2})), '{1: 2}') self.assertIs(tostr.handlers[DerivedDict], tostr.handlers[dict]) def test_new_type_tuple(self): - self.assertEqual(tostr(DerivedTuple([1, 2])), "(1, 2)") + self.assertEqual(tostr(DerivedTuple([1, 2])), '(1, 2)') self.assertIs(tostr.handlers[DerivedTuple], tostr.handlers[tuple]) def test_new_type_namedtuple(self): - self.assertEqual(tostr(NamedTuple(1, 2)), "NamedTuple(x=1, y=2)") + self.assertEqual(tostr(NamedTuple(1, 2)), 'NamedTuple(x=1, y=2)') self.assertIs(tostr.handlers[NamedTuple], tostr.handlers[None]) @@ -73,7 +73,7 @@ def test_unicode_table(self): os = StringIO() data = {1: ("a", 1), (2, 3): ("∧", 2)} tabular_writer(os, "", data.items(), ["s", "val"], lambda k, v: v) - ref = """ + ref = u""" Key : s : val 1 : a : 1 (2, 3) : ∧ : 2 @@ -82,9 +82,9 @@ def test_unicode_table(self): def test_tuple_list_dict(self): os = StringIO() - data = {(1,): (["a", 1], 1), ("2", 3): ({1: "a", 2: "2"}, "2")} + data = {(1,): (["a", 1], 1), ('2', 3): ({1: 'a', 2: '2'}, '2')} tabular_writer(os, "", data.items(), ["s", "val"], lambda k, v: v) - ref = """ + ref = u""" Key : s : val (1,) : ['a', 1] : 1 ('2', 3) : {1: 'a', 2: '2'} : 2 @@ -93,9 +93,9 @@ def test_tuple_list_dict(self): def test_no_header(self): os = StringIO() - data = {(2,): (["a", 1], 1), (1, 3): ({1: "a", 2: "2"}, "2")} + data = {(2,): (["a", 1], 1), (1, 3): ({1: 'a', 2: '2'}, '2')} tabular_writer(os, "", data.items(), [], lambda k, v: v) - ref = """ + ref = u""" {1: 'a', 2: '2'} : 2 ['a', 1] : 1 """ @@ -104,22 +104,22 @@ def test_no_header(self): def test_no_data(self): os = StringIO() data = {} - tabular_writer(os, "", data.items(), ["s", "val"], lambda k, v: v) - ref = """ + tabular_writer(os, "", data.items(), ['s', 'val'], lambda k, v: v) + ref = u""" Key : s : val """ self.assertEqual(ref.strip(), os.getvalue().strip()) def test_multiline_generator(self): os = StringIO() - data = {"a": 0, "b": 1, "c": 3} + data = {'a': 0, 'b': 1, 'c': 3} def _data_gen(i, j): for n in range(j): - yield (n, chr(ord("a") + n) * j) + yield (n, chr(ord('a') + n) * j) - tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) - ref = """ + tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) + ref = u""" Key : i : j a : None : None b : 0 : a @@ -129,37 +129,18 @@ def _data_gen(i, j): """ self.assertEqual(ref.strip(), os.getvalue().strip()) - def test_multiline_generator_user_sorted(self): - os = StringIO() - data = {"b": 0, "a": 1, "c": 3} - - def _data_gen(i, j): - for n in range(j): - yield (n, chr(ord("a") + n) * j) - - tabular_writer(os, "", data.items(), ["i", "j"], _data_gen, sort_rows=False) - ref = """ -Key : i : j - b : None : None - a : 0 : a - c : 0 : aaa - : 1 : bbb - : 2 : ccc -""" - self.assertEqual(ref.strip(), os.getvalue().strip()) - def test_multiline_generator_exception(self): os = StringIO() - data = {"a": 0, "b": 1, "c": 3} + data = {'a': 0, 'b': 1, 'c': 3} def _data_gen(i, j): - if i == "b": + if i == 'b': raise ValueError("invalid") for n in range(j): - yield (n, chr(ord("a") + n) * j) + yield (n, chr(ord('a') + n) * j) - tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) - ref = """ + tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) + ref = u""" Key : i : j a : None : None b : None : None @@ -171,15 +152,15 @@ def _data_gen(i, j): def test_data_exception(self): os = StringIO() - data = {"a": 0, "b": 1, "c": 3} + data = {'a': 0, 'b': 1, 'c': 3} def _data_gen(i, j): - if i == "b": + if i == 'b': raise ValueError("invalid") return (j, i * (j + 1)) - tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) - ref = """ + tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) + ref = u""" Key : i : j a : 0 : a b : None : None @@ -189,19 +170,19 @@ def _data_gen(i, j): def test_multiline_alignment(self): os = StringIO() - data = {"a": 1, "b": 2, "c": 3} + data = {'a': 1, 'b': 2, 'c': 3} def _data_gen(i, j): for n in range(j): - _str = chr(ord("a") + n) * (j + 1) + _str = chr(ord('a') + n) * (j + 1) if n % 2: _str = list(_str) - _str[1] = " " - _str = "".join(_str) + _str[1] = ' ' + _str = ''.join(_str) yield (n, _str) - tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) - ref = """ + tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) + ref = u""" Key : i : j a : 0 : aa b : 0 : aaa @@ -217,24 +198,24 @@ class TestStreamIndenter(unittest.TestCase): def test_noprefix(self): OUT1 = StringIO() OUT2 = StreamIndenter(OUT1) - OUT2.write("Hello?\nHello, world!") - self.assertEqual(" Hello?\n Hello, world!", OUT2.getvalue()) + OUT2.write('Hello?\nHello, world!') + self.assertEqual(' Hello?\n Hello, world!', OUT2.getvalue()) def test_prefix(self): - prefix = "foo:" + prefix = 'foo:' OUT1 = StringIO() OUT2 = StreamIndenter(OUT1, prefix) - OUT2.write("Hello?\nHello, world!") - self.assertEqual("foo:Hello?\nfoo:Hello, world!", OUT2.getvalue()) + OUT2.write('Hello?\nHello, world!') + self.assertEqual('foo:Hello?\nfoo:Hello, world!', OUT2.getvalue()) def test_blank_lines(self): OUT1 = StringIO() OUT2 = StreamIndenter(OUT1) - OUT2.write("Hello?\n\nText\n\nHello, world!") - self.assertEqual(" Hello?\n\n Text\n\n Hello, world!", OUT2.getvalue()) + OUT2.write('Hello?\n\nText\n\nHello, world!') + self.assertEqual(' Hello?\n\n Text\n\n Hello, world!', OUT2.getvalue()) def test_writelines(self): OUT1 = StringIO() OUT2 = StreamIndenter(OUT1) - OUT2.writelines(["Hello?\n", "\n", "Text\n", "\n", "Hello, world!"]) - self.assertEqual(" Hello?\n\n Text\n\n Hello, world!", OUT2.getvalue()) + OUT2.writelines(['Hello?\n', '\n', 'Text\n', '\n', 'Hello, world!']) + self.assertEqual(' Hello?\n\n Text\n\n Hello, world!', OUT2.getvalue()) From 53ccca9bb631b31188cb9a0808ec9ef4e015bf93 Mon Sep 17 00:00:00 2001 From: avdudchenko <33663878+avdudchenko@users.noreply.github.com> Date: Mon, 28 Apr 2025 11:38:12 -0400 Subject: [PATCH 6/9] Undid black foramting. --- pyomo/common/tests/test_formatting.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pyomo/common/tests/test_formatting.py b/pyomo/common/tests/test_formatting.py index 0798a80589c..3d1cd00402a 100644 --- a/pyomo/common/tests/test_formatting.py +++ b/pyomo/common/tests/test_formatting.py @@ -129,6 +129,25 @@ def _data_gen(i, j): """ self.assertEqual(ref.strip(), os.getvalue().strip()) + def test_multiline_generator_user_sorted(self): + os = StringIO() + data = {"b": 0, "a": 1, "c": 3} + + def _data_gen(i, j): + for n in range(j): + yield (n, chr(ord("a") + n) * j) + + tabular_writer(os, "", data.items(), ["i", "j"], _data_gen, sort_rows=False) + ref = """ +Key : i : j + b : None : None + a : 0 : a + c : 0 : aaa + : 1 : bbb + : 2 : ccc +""" + self.assertEqual(ref.strip(), os.getvalue().strip()) + def test_multiline_generator_exception(self): os = StringIO() data = {'a': 0, 'b': 1, 'c': 3} From 31428ca65fb988dd64695b1990e57fee1691f2d2 Mon Sep 17 00:00:00 2001 From: avdudchenko <33663878+avdudchenko@users.noreply.github.com> Date: Mon, 28 Apr 2025 11:58:36 -0400 Subject: [PATCH 7/9] added black formatting (orignally was not black formatted --- pyomo/common/formatting.py | 64 ++++++++++---------- pyomo/common/tests/test_formatting.py | 86 +++++++++++++-------------- 2 files changed, 75 insertions(+), 75 deletions(-) diff --git a/pyomo/common/formatting.py b/pyomo/common/formatting.py index 298ef7ec6cf..8d3e34d8376 100644 --- a/pyomo/common/formatting.py +++ b/pyomo/common/formatting.py @@ -82,20 +82,20 @@ def tostr(value, quote_str=False): tostr.handlers = { list: lambda value, quote_str: ( - "[%s]" % (', '.join(tostr(v, True) for v in value)) + "[%s]" % (", ".join(tostr(v, True) for v in value)) ), dict: lambda value, quote_str: ( "{%s}" % ( - ', '.join( - '%s: %s' % (tostr(k, True), tostr(v, True)) for k, v in value.items() + ", ".join( + "%s: %s" % (tostr(k, True), tostr(v, True)) for k, v in value.items() ) ) ), tuple: lambda value, quote_str: ( "(%s,)" % (tostr(value[0], True),) if len(value) == 1 - else "(%s)" % (', '.join(tostr(v, True) for v in value)) + else "(%s)" % (", ".join(tostr(v, True) for v in value)) ), str: lambda value, quote_str: (repr(value) if quote_str else value), None: lambda value, quote_str: str(value), @@ -130,7 +130,7 @@ def tabular_writer(ostream, prefix, data, header, row_generator, sort_rows=True) _rows = {} # NB: _width is a list because we will change these values if header: - header = (u"Key",) + tuple(tostr(x) for x in header) + header = ("Key",) + tuple(tostr(x) for x in header) _width = [len(x) for x in header] else: _width = None @@ -185,12 +185,12 @@ def tabular_writer(ostream, prefix, data, header, row_generator, sort_rows=True) # in the data (probably an expression or vector) _width = ["%" + str(i) + "s" for i in _width] - if any(' ' in r[-1] for x in _rows.values() if x is not None for r in x): - _width[-1] = '%s' + if any(" " in r[-1] for x in _rows.values() if x is not None for r in x): + _width[-1] = "%s" if sort_rows: - _sorted_rows=sorted_robust(_rows) + _sorted_rows = sorted_robust(_rows) else: - _sorted_rows=_rows + _sorted_rows = _rows for _key in _sorted_rows: _rowSet = _rows[_key] if not _rowSet: @@ -209,7 +209,7 @@ class StreamIndenter(object): StreamIndenter objects may be arbitrarily nested. """ - def __init__(self, ostream, indent=' ' * 4): + def __init__(self, ostream, indent=" " * 4): self.os = ostream self.indent = indent self.stripped_indent = indent.rstrip() @@ -221,7 +221,7 @@ def __getattr__(self, name): def write(self, data): if not len(data): return - lines = data.split('\n') + lines = data.split("\n") if self.newline: if lines[0]: self.os.write(self.indent + lines[0]) @@ -249,26 +249,26 @@ def writelines(self, sequence): self.write(x) -_indentation_re = re.compile(r'\s*') +_indentation_re = re.compile(r"\s*") _bullet_re = re.compile( - r'([-+*] +)' # bulleted lists - r'|(\(?[0-9]+[\)\.] +)' # enumerated lists (arabic numerals) - r'|(\(?[ivxlcdm]+[\)\.] +)' # enumerated lists (roman numerals) - r'|(\(?[IVXLCDM]+[\)\.] +)' # enumerated lists (roman numerals) - r'|(\(?[a-zA-Z][\)\.] +)' # enumerated lists (letters) - r'|(\(?\#[\)\.] +)' # auto enumerated lists - r'|([a-zA-Z0-9_ ]+ +: +)' # definitions - r'|(:[a-zA-Z0-9_ ]+: +)' # field name - r'|(?:\[\s*[A-Za-z0-9\.]+\s*\] +)' # [PASS]|[FAIL]|[ OK ] + r"([-+*] +)" # bulleted lists + r"|(\(?[0-9]+[\)\.] +)" # enumerated lists (arabic numerals) + r"|(\(?[ivxlcdm]+[\)\.] +)" # enumerated lists (roman numerals) + r"|(\(?[IVXLCDM]+[\)\.] +)" # enumerated lists (roman numerals) + r"|(\(?[a-zA-Z][\)\.] +)" # enumerated lists (letters) + r"|(\(?\#[\)\.] +)" # auto enumerated lists + r"|([a-zA-Z0-9_ ]+ +: +)" # definitions + r"|(:[a-zA-Z0-9_ ]+: +)" # field name + r"|(?:\[\s*[A-Za-z0-9\.]+\s*\] +)" # [PASS]|[FAIL]|[ OK ] ) _verbatim_line_start = re.compile( - r'(\| )' # line blocks - r'|(\+((-{3,})|(={3,}))\+)' # grid table + r"(\| )" # line blocks + r"|(\+((-{3,})|(={3,}))\+)" # grid table ) _verbatim_line = re.compile( - r'(={3,}[ =]+)' # simple tables, ======== sections + r"(={3,}[ =]+)" # simple tables, ======== sections # sections - + ''.join(r'|(\%s{3,})' % c for c in r'!"#$%&\'()*+,-./:;<>?@[\\]^_`{|}~') + + "".join(r"|(\%s{3,})" % c for c in r'!"#$%&\'()*+,-./:;<>?@[\\]^_`{|}~') ) @@ -305,7 +305,7 @@ def wrap_reStructuredText(docstr, wrapper): if literal_block: if literal_block[0] == 2: literal_block = False - elif paragraphs[-1][2] and ''.join(paragraphs[-1][2]).endswith('::'): + elif paragraphs[-1][2] and "".join(paragraphs[-1][2]).endswith("::"): literal_block = (0, paragraphs[-1][1]) paragraphs.append((None, None, None)) continue @@ -318,7 +318,7 @@ def wrap_reStructuredText(docstr, wrapper): continue elif ( len(literal_block[1]) == len(leading) - and content[0] in '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' + and content[0] in "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" ): # quoted literal block literal_block = 2, leading @@ -333,7 +333,7 @@ def wrap_reStructuredText(docstr, wrapper): else: # fall back on normal line processing literal_block = False - if content == '```': + if content == "```": # Not part of ReST, but we have supported this in Pyomo for a long time verbatim ^= True elif verbatim: @@ -353,7 +353,7 @@ def wrap_reStructuredText(docstr, wrapper): if matchBullet: # Handle things that look like bullet lists specially hang = matchBullet.group() - paragraphs.append((leading, leading + ' ' * len(hang), [content])) + paragraphs.append((leading, leading + " " * len(hang), [content])) elif paragraphs[-1][1] == leading: # Continuing a text block paragraphs[-1][2].append(content) @@ -371,16 +371,16 @@ def wrap_reStructuredText(docstr, wrapper): if indent is None: if par is None: - paragraphs[i] = '' + paragraphs[i] = "" else: paragraphs[i] = base_indent + par continue wrapper.initial_indent = base_indent + indent wrapper.subsequent_indent = base_indent + subseq - paragraphs[i] = wrapper.fill(' '.join(par)) + paragraphs[i] = wrapper.fill(" ".join(par)) finally: # Avoid side-effects and restore the initial wrapper state wrapper.initial_indent, wrapper.subsequent_indent = wrapper_init - return '\n'.join(paragraphs) + return "\n".join(paragraphs) diff --git a/pyomo/common/tests/test_formatting.py b/pyomo/common/tests/test_formatting.py index 3d1cd00402a..43ade6a8efc 100644 --- a/pyomo/common/tests/test_formatting.py +++ b/pyomo/common/tests/test_formatting.py @@ -33,36 +33,36 @@ class DerivedStr(str): pass -NamedTuple = namedtuple('NamedTuple', ['x', 'y']) +NamedTuple = namedtuple("NamedTuple", ["x", "y"]) class TestToStr(unittest.TestCase): def test_new_type_float(self): - self.assertEqual(tostr(0.5), '0.5') + self.assertEqual(tostr(0.5), "0.5") self.assertIs(tostr.handlers[float], tostr.handlers[None]) def test_new_type_int(self): - self.assertEqual(tostr(0), '0') + self.assertEqual(tostr(0), "0") self.assertIs(tostr.handlers[int], tostr.handlers[None]) def test_new_type_str(self): - self.assertEqual(tostr(DerivedStr(1)), '1') + self.assertEqual(tostr(DerivedStr(1)), "1") self.assertIs(tostr.handlers[DerivedStr], tostr.handlers[str]) def test_new_type_list(self): - self.assertEqual(tostr(DerivedList([1, 2])), '[1, 2]') + self.assertEqual(tostr(DerivedList([1, 2])), "[1, 2]") self.assertIs(tostr.handlers[DerivedList], tostr.handlers[list]) def test_new_type_dict(self): - self.assertEqual(tostr(DerivedDict({1: 2})), '{1: 2}') + self.assertEqual(tostr(DerivedDict({1: 2})), "{1: 2}") self.assertIs(tostr.handlers[DerivedDict], tostr.handlers[dict]) def test_new_type_tuple(self): - self.assertEqual(tostr(DerivedTuple([1, 2])), '(1, 2)') + self.assertEqual(tostr(DerivedTuple([1, 2])), "(1, 2)") self.assertIs(tostr.handlers[DerivedTuple], tostr.handlers[tuple]) def test_new_type_namedtuple(self): - self.assertEqual(tostr(NamedTuple(1, 2)), 'NamedTuple(x=1, y=2)') + self.assertEqual(tostr(NamedTuple(1, 2)), "NamedTuple(x=1, y=2)") self.assertIs(tostr.handlers[NamedTuple], tostr.handlers[None]) @@ -73,7 +73,7 @@ def test_unicode_table(self): os = StringIO() data = {1: ("a", 1), (2, 3): ("∧", 2)} tabular_writer(os, "", data.items(), ["s", "val"], lambda k, v: v) - ref = u""" + ref = """ Key : s : val 1 : a : 1 (2, 3) : ∧ : 2 @@ -82,9 +82,9 @@ def test_unicode_table(self): def test_tuple_list_dict(self): os = StringIO() - data = {(1,): (["a", 1], 1), ('2', 3): ({1: 'a', 2: '2'}, '2')} + data = {(1,): (["a", 1], 1), ("2", 3): ({1: "a", 2: "2"}, "2")} tabular_writer(os, "", data.items(), ["s", "val"], lambda k, v: v) - ref = u""" + ref = """ Key : s : val (1,) : ['a', 1] : 1 ('2', 3) : {1: 'a', 2: '2'} : 2 @@ -93,9 +93,9 @@ def test_tuple_list_dict(self): def test_no_header(self): os = StringIO() - data = {(2,): (["a", 1], 1), (1, 3): ({1: 'a', 2: '2'}, '2')} + data = {(2,): (["a", 1], 1), (1, 3): ({1: "a", 2: "2"}, "2")} tabular_writer(os, "", data.items(), [], lambda k, v: v) - ref = u""" + ref = """ {1: 'a', 2: '2'} : 2 ['a', 1] : 1 """ @@ -104,22 +104,22 @@ def test_no_header(self): def test_no_data(self): os = StringIO() data = {} - tabular_writer(os, "", data.items(), ['s', 'val'], lambda k, v: v) - ref = u""" + tabular_writer(os, "", data.items(), ["s", "val"], lambda k, v: v) + ref = """ Key : s : val """ self.assertEqual(ref.strip(), os.getvalue().strip()) def test_multiline_generator(self): os = StringIO() - data = {'a': 0, 'b': 1, 'c': 3} + data = {"a": 0, "b": 1, "c": 3} def _data_gen(i, j): for n in range(j): - yield (n, chr(ord('a') + n) * j) + yield (n, chr(ord("a") + n) * j) - tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) - ref = u""" + tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) + ref = """ Key : i : j a : None : None b : 0 : a @@ -150,16 +150,16 @@ def _data_gen(i, j): def test_multiline_generator_exception(self): os = StringIO() - data = {'a': 0, 'b': 1, 'c': 3} + data = {"a": 0, "b": 1, "c": 3} def _data_gen(i, j): - if i == 'b': + if i == "b": raise ValueError("invalid") for n in range(j): - yield (n, chr(ord('a') + n) * j) + yield (n, chr(ord("a") + n) * j) - tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) - ref = u""" + tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) + ref = """ Key : i : j a : None : None b : None : None @@ -171,15 +171,15 @@ def _data_gen(i, j): def test_data_exception(self): os = StringIO() - data = {'a': 0, 'b': 1, 'c': 3} + data = {"a": 0, "b": 1, "c": 3} def _data_gen(i, j): - if i == 'b': + if i == "b": raise ValueError("invalid") return (j, i * (j + 1)) - tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) - ref = u""" + tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) + ref = """ Key : i : j a : 0 : a b : None : None @@ -189,19 +189,19 @@ def _data_gen(i, j): def test_multiline_alignment(self): os = StringIO() - data = {'a': 1, 'b': 2, 'c': 3} + data = {"a": 1, "b": 2, "c": 3} def _data_gen(i, j): for n in range(j): - _str = chr(ord('a') + n) * (j + 1) + _str = chr(ord("a") + n) * (j + 1) if n % 2: _str = list(_str) - _str[1] = ' ' - _str = ''.join(_str) + _str[1] = " " + _str = "".join(_str) yield (n, _str) - tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) - ref = u""" + tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) + ref = """ Key : i : j a : 0 : aa b : 0 : aaa @@ -217,24 +217,24 @@ class TestStreamIndenter(unittest.TestCase): def test_noprefix(self): OUT1 = StringIO() OUT2 = StreamIndenter(OUT1) - OUT2.write('Hello?\nHello, world!') - self.assertEqual(' Hello?\n Hello, world!', OUT2.getvalue()) + OUT2.write("Hello?\nHello, world!") + self.assertEqual(" Hello?\n Hello, world!", OUT2.getvalue()) def test_prefix(self): - prefix = 'foo:' + prefix = "foo:" OUT1 = StringIO() OUT2 = StreamIndenter(OUT1, prefix) - OUT2.write('Hello?\nHello, world!') - self.assertEqual('foo:Hello?\nfoo:Hello, world!', OUT2.getvalue()) + OUT2.write("Hello?\nHello, world!") + self.assertEqual("foo:Hello?\nfoo:Hello, world!", OUT2.getvalue()) def test_blank_lines(self): OUT1 = StringIO() OUT2 = StreamIndenter(OUT1) - OUT2.write('Hello?\n\nText\n\nHello, world!') - self.assertEqual(' Hello?\n\n Text\n\n Hello, world!', OUT2.getvalue()) + OUT2.write("Hello?\n\nText\n\nHello, world!") + self.assertEqual(" Hello?\n\n Text\n\n Hello, world!", OUT2.getvalue()) def test_writelines(self): OUT1 = StringIO() OUT2 = StreamIndenter(OUT1) - OUT2.writelines(['Hello?\n', '\n', 'Text\n', '\n', 'Hello, world!']) - self.assertEqual(' Hello?\n\n Text\n\n Hello, world!', OUT2.getvalue()) + OUT2.writelines(["Hello?\n", "\n", "Text\n", "\n", "Hello, world!"]) + self.assertEqual(" Hello?\n\n Text\n\n Hello, world!", OUT2.getvalue()) From 5a68038173bee2156ec80a3e5aa9f5a8d263ce25 Mon Sep 17 00:00:00 2001 From: avdudchenko <33663878+avdudchenko@users.noreply.github.com> Date: Mon, 28 Apr 2025 14:07:52 -0400 Subject: [PATCH 8/9] Revert "added black formatting (orignally was not black formatted" This reverts commit 31428ca65fb988dd64695b1990e57fee1691f2d2. --- pyomo/common/formatting.py | 64 ++++++++++---------- pyomo/common/tests/test_formatting.py | 86 +++++++++++++-------------- 2 files changed, 75 insertions(+), 75 deletions(-) diff --git a/pyomo/common/formatting.py b/pyomo/common/formatting.py index 8d3e34d8376..298ef7ec6cf 100644 --- a/pyomo/common/formatting.py +++ b/pyomo/common/formatting.py @@ -82,20 +82,20 @@ def tostr(value, quote_str=False): tostr.handlers = { list: lambda value, quote_str: ( - "[%s]" % (", ".join(tostr(v, True) for v in value)) + "[%s]" % (', '.join(tostr(v, True) for v in value)) ), dict: lambda value, quote_str: ( "{%s}" % ( - ", ".join( - "%s: %s" % (tostr(k, True), tostr(v, True)) for k, v in value.items() + ', '.join( + '%s: %s' % (tostr(k, True), tostr(v, True)) for k, v in value.items() ) ) ), tuple: lambda value, quote_str: ( "(%s,)" % (tostr(value[0], True),) if len(value) == 1 - else "(%s)" % (", ".join(tostr(v, True) for v in value)) + else "(%s)" % (', '.join(tostr(v, True) for v in value)) ), str: lambda value, quote_str: (repr(value) if quote_str else value), None: lambda value, quote_str: str(value), @@ -130,7 +130,7 @@ def tabular_writer(ostream, prefix, data, header, row_generator, sort_rows=True) _rows = {} # NB: _width is a list because we will change these values if header: - header = ("Key",) + tuple(tostr(x) for x in header) + header = (u"Key",) + tuple(tostr(x) for x in header) _width = [len(x) for x in header] else: _width = None @@ -185,12 +185,12 @@ def tabular_writer(ostream, prefix, data, header, row_generator, sort_rows=True) # in the data (probably an expression or vector) _width = ["%" + str(i) + "s" for i in _width] - if any(" " in r[-1] for x in _rows.values() if x is not None for r in x): - _width[-1] = "%s" + if any(' ' in r[-1] for x in _rows.values() if x is not None for r in x): + _width[-1] = '%s' if sort_rows: - _sorted_rows = sorted_robust(_rows) + _sorted_rows=sorted_robust(_rows) else: - _sorted_rows = _rows + _sorted_rows=_rows for _key in _sorted_rows: _rowSet = _rows[_key] if not _rowSet: @@ -209,7 +209,7 @@ class StreamIndenter(object): StreamIndenter objects may be arbitrarily nested. """ - def __init__(self, ostream, indent=" " * 4): + def __init__(self, ostream, indent=' ' * 4): self.os = ostream self.indent = indent self.stripped_indent = indent.rstrip() @@ -221,7 +221,7 @@ def __getattr__(self, name): def write(self, data): if not len(data): return - lines = data.split("\n") + lines = data.split('\n') if self.newline: if lines[0]: self.os.write(self.indent + lines[0]) @@ -249,26 +249,26 @@ def writelines(self, sequence): self.write(x) -_indentation_re = re.compile(r"\s*") +_indentation_re = re.compile(r'\s*') _bullet_re = re.compile( - r"([-+*] +)" # bulleted lists - r"|(\(?[0-9]+[\)\.] +)" # enumerated lists (arabic numerals) - r"|(\(?[ivxlcdm]+[\)\.] +)" # enumerated lists (roman numerals) - r"|(\(?[IVXLCDM]+[\)\.] +)" # enumerated lists (roman numerals) - r"|(\(?[a-zA-Z][\)\.] +)" # enumerated lists (letters) - r"|(\(?\#[\)\.] +)" # auto enumerated lists - r"|([a-zA-Z0-9_ ]+ +: +)" # definitions - r"|(:[a-zA-Z0-9_ ]+: +)" # field name - r"|(?:\[\s*[A-Za-z0-9\.]+\s*\] +)" # [PASS]|[FAIL]|[ OK ] + r'([-+*] +)' # bulleted lists + r'|(\(?[0-9]+[\)\.] +)' # enumerated lists (arabic numerals) + r'|(\(?[ivxlcdm]+[\)\.] +)' # enumerated lists (roman numerals) + r'|(\(?[IVXLCDM]+[\)\.] +)' # enumerated lists (roman numerals) + r'|(\(?[a-zA-Z][\)\.] +)' # enumerated lists (letters) + r'|(\(?\#[\)\.] +)' # auto enumerated lists + r'|([a-zA-Z0-9_ ]+ +: +)' # definitions + r'|(:[a-zA-Z0-9_ ]+: +)' # field name + r'|(?:\[\s*[A-Za-z0-9\.]+\s*\] +)' # [PASS]|[FAIL]|[ OK ] ) _verbatim_line_start = re.compile( - r"(\| )" # line blocks - r"|(\+((-{3,})|(={3,}))\+)" # grid table + r'(\| )' # line blocks + r'|(\+((-{3,})|(={3,}))\+)' # grid table ) _verbatim_line = re.compile( - r"(={3,}[ =]+)" # simple tables, ======== sections + r'(={3,}[ =]+)' # simple tables, ======== sections # sections - + "".join(r"|(\%s{3,})" % c for c in r'!"#$%&\'()*+,-./:;<>?@[\\]^_`{|}~') + + ''.join(r'|(\%s{3,})' % c for c in r'!"#$%&\'()*+,-./:;<>?@[\\]^_`{|}~') ) @@ -305,7 +305,7 @@ def wrap_reStructuredText(docstr, wrapper): if literal_block: if literal_block[0] == 2: literal_block = False - elif paragraphs[-1][2] and "".join(paragraphs[-1][2]).endswith("::"): + elif paragraphs[-1][2] and ''.join(paragraphs[-1][2]).endswith('::'): literal_block = (0, paragraphs[-1][1]) paragraphs.append((None, None, None)) continue @@ -318,7 +318,7 @@ def wrap_reStructuredText(docstr, wrapper): continue elif ( len(literal_block[1]) == len(leading) - and content[0] in "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" + and content[0] in '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~' ): # quoted literal block literal_block = 2, leading @@ -333,7 +333,7 @@ def wrap_reStructuredText(docstr, wrapper): else: # fall back on normal line processing literal_block = False - if content == "```": + if content == '```': # Not part of ReST, but we have supported this in Pyomo for a long time verbatim ^= True elif verbatim: @@ -353,7 +353,7 @@ def wrap_reStructuredText(docstr, wrapper): if matchBullet: # Handle things that look like bullet lists specially hang = matchBullet.group() - paragraphs.append((leading, leading + " " * len(hang), [content])) + paragraphs.append((leading, leading + ' ' * len(hang), [content])) elif paragraphs[-1][1] == leading: # Continuing a text block paragraphs[-1][2].append(content) @@ -371,16 +371,16 @@ def wrap_reStructuredText(docstr, wrapper): if indent is None: if par is None: - paragraphs[i] = "" + paragraphs[i] = '' else: paragraphs[i] = base_indent + par continue wrapper.initial_indent = base_indent + indent wrapper.subsequent_indent = base_indent + subseq - paragraphs[i] = wrapper.fill(" ".join(par)) + paragraphs[i] = wrapper.fill(' '.join(par)) finally: # Avoid side-effects and restore the initial wrapper state wrapper.initial_indent, wrapper.subsequent_indent = wrapper_init - return "\n".join(paragraphs) + return '\n'.join(paragraphs) diff --git a/pyomo/common/tests/test_formatting.py b/pyomo/common/tests/test_formatting.py index 43ade6a8efc..3d1cd00402a 100644 --- a/pyomo/common/tests/test_formatting.py +++ b/pyomo/common/tests/test_formatting.py @@ -33,36 +33,36 @@ class DerivedStr(str): pass -NamedTuple = namedtuple("NamedTuple", ["x", "y"]) +NamedTuple = namedtuple('NamedTuple', ['x', 'y']) class TestToStr(unittest.TestCase): def test_new_type_float(self): - self.assertEqual(tostr(0.5), "0.5") + self.assertEqual(tostr(0.5), '0.5') self.assertIs(tostr.handlers[float], tostr.handlers[None]) def test_new_type_int(self): - self.assertEqual(tostr(0), "0") + self.assertEqual(tostr(0), '0') self.assertIs(tostr.handlers[int], tostr.handlers[None]) def test_new_type_str(self): - self.assertEqual(tostr(DerivedStr(1)), "1") + self.assertEqual(tostr(DerivedStr(1)), '1') self.assertIs(tostr.handlers[DerivedStr], tostr.handlers[str]) def test_new_type_list(self): - self.assertEqual(tostr(DerivedList([1, 2])), "[1, 2]") + self.assertEqual(tostr(DerivedList([1, 2])), '[1, 2]') self.assertIs(tostr.handlers[DerivedList], tostr.handlers[list]) def test_new_type_dict(self): - self.assertEqual(tostr(DerivedDict({1: 2})), "{1: 2}") + self.assertEqual(tostr(DerivedDict({1: 2})), '{1: 2}') self.assertIs(tostr.handlers[DerivedDict], tostr.handlers[dict]) def test_new_type_tuple(self): - self.assertEqual(tostr(DerivedTuple([1, 2])), "(1, 2)") + self.assertEqual(tostr(DerivedTuple([1, 2])), '(1, 2)') self.assertIs(tostr.handlers[DerivedTuple], tostr.handlers[tuple]) def test_new_type_namedtuple(self): - self.assertEqual(tostr(NamedTuple(1, 2)), "NamedTuple(x=1, y=2)") + self.assertEqual(tostr(NamedTuple(1, 2)), 'NamedTuple(x=1, y=2)') self.assertIs(tostr.handlers[NamedTuple], tostr.handlers[None]) @@ -73,7 +73,7 @@ def test_unicode_table(self): os = StringIO() data = {1: ("a", 1), (2, 3): ("∧", 2)} tabular_writer(os, "", data.items(), ["s", "val"], lambda k, v: v) - ref = """ + ref = u""" Key : s : val 1 : a : 1 (2, 3) : ∧ : 2 @@ -82,9 +82,9 @@ def test_unicode_table(self): def test_tuple_list_dict(self): os = StringIO() - data = {(1,): (["a", 1], 1), ("2", 3): ({1: "a", 2: "2"}, "2")} + data = {(1,): (["a", 1], 1), ('2', 3): ({1: 'a', 2: '2'}, '2')} tabular_writer(os, "", data.items(), ["s", "val"], lambda k, v: v) - ref = """ + ref = u""" Key : s : val (1,) : ['a', 1] : 1 ('2', 3) : {1: 'a', 2: '2'} : 2 @@ -93,9 +93,9 @@ def test_tuple_list_dict(self): def test_no_header(self): os = StringIO() - data = {(2,): (["a", 1], 1), (1, 3): ({1: "a", 2: "2"}, "2")} + data = {(2,): (["a", 1], 1), (1, 3): ({1: 'a', 2: '2'}, '2')} tabular_writer(os, "", data.items(), [], lambda k, v: v) - ref = """ + ref = u""" {1: 'a', 2: '2'} : 2 ['a', 1] : 1 """ @@ -104,22 +104,22 @@ def test_no_header(self): def test_no_data(self): os = StringIO() data = {} - tabular_writer(os, "", data.items(), ["s", "val"], lambda k, v: v) - ref = """ + tabular_writer(os, "", data.items(), ['s', 'val'], lambda k, v: v) + ref = u""" Key : s : val """ self.assertEqual(ref.strip(), os.getvalue().strip()) def test_multiline_generator(self): os = StringIO() - data = {"a": 0, "b": 1, "c": 3} + data = {'a': 0, 'b': 1, 'c': 3} def _data_gen(i, j): for n in range(j): - yield (n, chr(ord("a") + n) * j) + yield (n, chr(ord('a') + n) * j) - tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) - ref = """ + tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) + ref = u""" Key : i : j a : None : None b : 0 : a @@ -150,16 +150,16 @@ def _data_gen(i, j): def test_multiline_generator_exception(self): os = StringIO() - data = {"a": 0, "b": 1, "c": 3} + data = {'a': 0, 'b': 1, 'c': 3} def _data_gen(i, j): - if i == "b": + if i == 'b': raise ValueError("invalid") for n in range(j): - yield (n, chr(ord("a") + n) * j) + yield (n, chr(ord('a') + n) * j) - tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) - ref = """ + tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) + ref = u""" Key : i : j a : None : None b : None : None @@ -171,15 +171,15 @@ def _data_gen(i, j): def test_data_exception(self): os = StringIO() - data = {"a": 0, "b": 1, "c": 3} + data = {'a': 0, 'b': 1, 'c': 3} def _data_gen(i, j): - if i == "b": + if i == 'b': raise ValueError("invalid") return (j, i * (j + 1)) - tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) - ref = """ + tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) + ref = u""" Key : i : j a : 0 : a b : None : None @@ -189,19 +189,19 @@ def _data_gen(i, j): def test_multiline_alignment(self): os = StringIO() - data = {"a": 1, "b": 2, "c": 3} + data = {'a': 1, 'b': 2, 'c': 3} def _data_gen(i, j): for n in range(j): - _str = chr(ord("a") + n) * (j + 1) + _str = chr(ord('a') + n) * (j + 1) if n % 2: _str = list(_str) - _str[1] = " " - _str = "".join(_str) + _str[1] = ' ' + _str = ''.join(_str) yield (n, _str) - tabular_writer(os, "", data.items(), ["i", "j"], _data_gen) - ref = """ + tabular_writer(os, "", data.items(), ['i', 'j'], _data_gen) + ref = u""" Key : i : j a : 0 : aa b : 0 : aaa @@ -217,24 +217,24 @@ class TestStreamIndenter(unittest.TestCase): def test_noprefix(self): OUT1 = StringIO() OUT2 = StreamIndenter(OUT1) - OUT2.write("Hello?\nHello, world!") - self.assertEqual(" Hello?\n Hello, world!", OUT2.getvalue()) + OUT2.write('Hello?\nHello, world!') + self.assertEqual(' Hello?\n Hello, world!', OUT2.getvalue()) def test_prefix(self): - prefix = "foo:" + prefix = 'foo:' OUT1 = StringIO() OUT2 = StreamIndenter(OUT1, prefix) - OUT2.write("Hello?\nHello, world!") - self.assertEqual("foo:Hello?\nfoo:Hello, world!", OUT2.getvalue()) + OUT2.write('Hello?\nHello, world!') + self.assertEqual('foo:Hello?\nfoo:Hello, world!', OUT2.getvalue()) def test_blank_lines(self): OUT1 = StringIO() OUT2 = StreamIndenter(OUT1) - OUT2.write("Hello?\n\nText\n\nHello, world!") - self.assertEqual(" Hello?\n\n Text\n\n Hello, world!", OUT2.getvalue()) + OUT2.write('Hello?\n\nText\n\nHello, world!') + self.assertEqual(' Hello?\n\n Text\n\n Hello, world!', OUT2.getvalue()) def test_writelines(self): OUT1 = StringIO() OUT2 = StreamIndenter(OUT1) - OUT2.writelines(["Hello?\n", "\n", "Text\n", "\n", "Hello, world!"]) - self.assertEqual(" Hello?\n\n Text\n\n Hello, world!", OUT2.getvalue()) + OUT2.writelines(['Hello?\n', '\n', 'Text\n', '\n', 'Hello, world!']) + self.assertEqual(' Hello?\n\n Text\n\n Hello, world!', OUT2.getvalue()) From 99d7bccab1cf01b217b020180c33957dab13a0c8 Mon Sep 17 00:00:00 2001 From: avdudchenko <33663878+avdudchenko@users.noreply.github.com> Date: Mon, 28 Apr 2025 14:08:54 -0400 Subject: [PATCH 9/9] Update formatting.py --- pyomo/common/formatting.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pyomo/common/formatting.py b/pyomo/common/formatting.py index 298ef7ec6cf..d9699c39d96 100644 --- a/pyomo/common/formatting.py +++ b/pyomo/common/formatting.py @@ -188,9 +188,9 @@ def tabular_writer(ostream, prefix, data, header, row_generator, sort_rows=True) if any(' ' in r[-1] for x in _rows.values() if x is not None for r in x): _width[-1] = '%s' if sort_rows: - _sorted_rows=sorted_robust(_rows) + _sorted_rows = sorted_robust(_rows) else: - _sorted_rows=_rows + _sorted_rows = _rows for _key in _sorted_rows: _rowSet = _rows[_key] if not _rowSet: