Skip to content

Commit cca6019

Browse files
authored
Merge pull request #3392 from jsiirola/xpress-9.5
Update Xpress interfaces to support 9.5
2 parents cc1b175 + cb336b4 commit cca6019

File tree

5 files changed

+214
-42
lines changed

5 files changed

+214
-42
lines changed

pyomo/common/collections/component_map.py

+14
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,18 @@ def _unhashable(val):
4747
def _tuple(self, val):
4848
return tuple(self[i.__class__](i) for i in val)
4949

50+
def hashable(self, obj, hashable=None):
51+
if isinstance(obj, type):
52+
cls = obj
53+
else:
54+
cls = type(obj)
55+
if hashable is None:
56+
fcn = self.get(cls, None)
57+
if fcn is None:
58+
raise KeyError(obj)
59+
return fcn is self._hashable
60+
self[cls] = self._hashable if hashable else self._unhashable
61+
5062

5163
_hasher = _Hasher()
5264

@@ -78,6 +90,8 @@ class ComponentMap(AutoSlots.Mixin, collections.abc.MutableMapping):
7890

7991
__slots__ = ("_dict",)
8092
__autoslot_mappers__ = {'_dict': _rehash_keys}
93+
# Expose a "public" interface to the global _hasher dict
94+
hasher = _hasher
8195

8296
def __init__(self, *args, **kwds):
8397
# maps id_hash(obj) -> (obj,val)

pyomo/common/collections/component_set.py

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ class ComponentSet(AutoSlots.Mixin, collections_MutableSet):
6161

6262
__slots__ = ("_data",)
6363
__autoslot_mappers__ = {'_data': _rehash_keys}
64+
# Expose a "public" interface to the global _hasher dict
65+
hasher = _hasher
6466

6567
def __init__(self, iterable=None):
6668
# maps id_hash(obj) -> obj

pyomo/common/tests/test_component_map.py

+21
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,27 @@ def test_tuple(self):
4949
self.assertIn((1, (2, m.v)), m.cm)
5050
self.assertNotIn((1, (2, m.v)), i.cm)
5151

52+
def test_hasher(self):
53+
m = ComponentMap()
54+
a = 'str'
55+
m[a] = 5
56+
self.assertTrue(m.hasher.hashable(a))
57+
self.assertTrue(m.hasher.hashable(str))
58+
self.assertEqual(m._dict, {a: (a, 5)})
59+
del m[a]
60+
61+
m.hasher.hashable(a, False)
62+
m[a] = 5
63+
self.assertFalse(m.hasher.hashable(a))
64+
self.assertFalse(m.hasher.hashable(str))
65+
self.assertEqual(m._dict, {id(a): (a, 5)})
66+
67+
class TMP:
68+
pass
69+
70+
with self.assertRaises(KeyError):
71+
m.hasher.hashable(TMP)
72+
5273

5374
class TestDefaultComponentMap(unittest.TestCase):
5475
def test_default_component_map(self):

0 commit comments

Comments
 (0)