Skip to content

Commit

Permalink
_component_data_iter now uses sorted_robust
Browse files Browse the repository at this point in the history
  • Loading branch information
p.attard authored and p.attard committed Mar 5, 2021
1 parent 7185942 commit 81762f9
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
3 changes: 2 additions & 1 deletion pyomo/core/base/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
from pyomo.core.base.indexed_component import (
ActiveIndexedComponent, UnindexedComponent_set,
)
from pyomo.core.base.misc import sorted_robust

from pyomo.opt.base import ProblemFormat, guess_format
from pyomo.opt import WriterFactory
Expand Down Expand Up @@ -1360,7 +1361,7 @@ def _component_data_iter(self, ctype=None, active=None, sort=False):
_items = ((None, comp),)

if _sort_indices:
_items = sorted(_items, key=itemgetter(0))
_items = sorted_robust(_items)
if active is None or not isinstance(comp, ActiveIndexedComponent):
for idx, compData in _items:
yield (name, idx), compData
Expand Down
10 changes: 5 additions & 5 deletions pyomo/core/base/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@
import sys
import types

from six import itervalues, string_types

logger = logging.getLogger('pyomo.core')


Expand Down Expand Up @@ -113,7 +111,7 @@ class _robust_sort_keyfcn(object):
"""
def __init__(self):
self._typemap = {}
self._typemap = {tuple: (3, tuple.__name__)}

def __call__(self, val):
"""Generate a tuple ( str(type_name), val ) for sorting the value.
Expand Down Expand Up @@ -149,6 +147,8 @@ def __call__(self, val):
self._typemap[_type] = i, _typename
if i == 1:
return _typename, val
elif i == 3:
return _typename, tuple(self(v) for v in val)
elif i == 2:
return _typename, str(val)
else:
Expand Down Expand Up @@ -179,7 +179,7 @@ def sorted_robust(arg):


def _to_ustr(obj):
if not isinstance(obj, string_types):
if not isinstance(obj, str):
try:
obj = str(obj)
except:
Expand Down Expand Up @@ -259,7 +259,7 @@ def tabular_writer(ostream, prefix, data, header, row_generator):
_width = ["%"+str(i)+"s" for i in _width]

if any( ' ' in r[-1]
for x in itervalues(_rows) if x is not None
for x in _rows.values() if x is not None
for r in x ):
_width[-1] = '%s'
for _key in sorted_robust(_rows):
Expand Down
31 changes: 26 additions & 5 deletions pyomo/core/tests/unit/test_base_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,47 @@
#
# Unit Tests for pyomo.base.misc
#
from six import StringIO, iteritems
from io import StringIO

import pyutilib.th as unittest

from pyomo.core.base.misc import tabular_writer
from pyomo.core.base.misc import tabular_writer, sorted_robust


class TestTabularWriter(unittest.TestCase):
def test_unicode_table(self):
# Test that an embedded unicode character does not foul up the
# table alignment
os = StringIO()
data = {1: ("a", 1), (2,3): ("∧", 2)}
tabular_writer(os, "", iteritems(data), ["s", "val"], lambda k,v: v)
data = {1: ("a", 1), (2, 3): ("∧", 2)}
tabular_writer(os, "", data.items(), ["s", "val"], lambda k, v: v)
ref = u"""
Key : s : val
1 : a : 1
(2, 3) : ∧ : 2
"""
self.assertEqual(ref.strip(), os.getvalue().strip())


class TestSortedRobust(unittest.TestCase):
def test_sorted_robust(self):
# Note: as types are sorted by name, int < str < tuple
a = sorted_robust([3, 2, 1])
self.assertEqual(a, [1, 2, 3])

a = sorted_robust([3, '2', 1])
self.assertEqual(a, [1, 3, '2'])

a = sorted_robust([('str1', 'str1'), (1, 'str2')])
self.assertEqual(a, [(1, 'str2'), ('str1', 'str1')])

a = sorted_robust([((1,), 'str2'), ('str1', 'str1')])
self.assertEqual(a, [('str1', 'str1'), ((1,), 'str2')])

a = sorted_robust([('str1', 'str1'), ((1,), 'str2')])
self.assertEqual(a, [('str1', 'str1'), ((1,), 'str2')])


if __name__ == "__main__":
unittest.main()

0 comments on commit 81762f9

Please sign in to comment.