Skip to content

Commit

Permalink
PersistentDict: support frozendict, immutabledict, FrozenOrderedSet
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiasdiener authored and inducer committed Nov 7, 2023
1 parent 42cefa2 commit 2223204
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ jobs:
# AK, 2020-12-13
rm pytools/mpiwrap.py
EXTRA_INSTALL="numpy"
EXTRA_INSTALL="numpy frozendict immutabledict orderedsets"
curl -L -O https://gitlab.tiker.net/inducer/ci-support/raw/main/build-and-test-py-project.sh
. ./build-and-test-py-project.sh
Expand Down
11 changes: 11 additions & 0 deletions pytools/persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ def update_for_frozenset(self, key_hash, key):
key_hash,
(self.rec(self.new_hash(), key_i).digest() for key_i in key))

update_for_FrozenOrderedSet = update_for_frozenset # noqa: N815

@staticmethod
def update_for_NoneType(key_hash, key): # noqa
del key
Expand Down Expand Up @@ -387,6 +389,15 @@ def update_for_attrs(self, key_hash, key):
self.rec(key_hash, fld.name)
self.rec(key_hash, getattr(key, fld.name, None))

def update_for_frozendict(self, key_hash, key):
from pytools import unordered_hash

unordered_hash(
key_hash,
(self.rec(self.new_hash(), (k, v)).digest() for k, v in key.items()))

update_for_immutabledict = update_for_frozendict

# }}}

# }}}
Expand Down
49 changes: 49 additions & 0 deletions pytools/test/test_persistent_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,55 @@ def test_scalar_hashing():
assert keyb(np.clongdouble(1.1+2.2j)) == keyb(np.clongdouble(1.1+2.2j))


def test_frozendict_hashing():
pytest.importorskip("frozendict")
from frozendict import frozendict

keyb = KeyBuilder()

d = {"a": 1, "b": 2}

assert keyb(frozendict(d)) == keyb(frozendict(d))
assert keyb(frozendict(d)) != keyb(frozendict({"a": 1, "b": 3}))
assert keyb(frozendict(d)) == keyb(frozendict({"b": 2, "a": 1}))

with pytest.raises(TypeError):
keyb(d)


def test_immutabledict_hashing():
pytest.importorskip("immutabledict")
from immutabledict import immutabledict

keyb = KeyBuilder()

d = {"a": 1, "b": 2}

assert keyb(immutabledict(d)) == keyb(immutabledict(d))
assert keyb(immutabledict(d)) != keyb(immutabledict({"a": 1, "b": 3}))
assert keyb(immutabledict(d)) == keyb(immutabledict({"b": 2, "a": 1}))


def test_frozenset_hashing():
keyb = KeyBuilder()

assert keyb(frozenset([1, 2, 3])) == keyb(frozenset([1, 2, 3]))
assert keyb(frozenset([1, 2, 3])) != keyb(frozenset([1, 2, 4]))
assert keyb(frozenset([1, 2, 3])) == keyb(frozenset([3, 2, 1]))


def test_frozenorderedset_hashing():
pytest.importorskip("orderedsets")
from orderedsets import FrozenOrderedSet
keyb = KeyBuilder()

assert (keyb(FrozenOrderedSet([1, 2, 3]))
== keyb(FrozenOrderedSet([1, 2, 3]))
== keyb(frozenset([1, 2, 3])))
assert keyb(FrozenOrderedSet([1, 2, 3])) != keyb(FrozenOrderedSet([1, 2, 4]))
assert keyb(FrozenOrderedSet([1, 2, 3])) == keyb(FrozenOrderedSet([3, 2, 1]))


if __name__ == "__main__":
if len(sys.argv) > 1:
exec(sys.argv[1])
Expand Down

0 comments on commit 2223204

Please sign in to comment.