Skip to content
Snippets Groups Projects
Commit 0cbb5c1c authored by Andreas Klöckner's avatar Andreas Klöckner
Browse files

Support pytools.tag in persistent_dict

parent 4325aca2
No related branches found
No related tags found
No related merge requests found
...@@ -122,6 +122,17 @@ class Tag: ...@@ -122,6 +122,17 @@ class Tag:
def tag_name(self) -> DottedName: def tag_name(self) -> DottedName:
return DottedName.from_class(type(self)) return DottedName.from_class(type(self))
def update_persistent_hash(self, key_hash, key_builder):
key_builder.rec(key_hash, self.__class__.__qualname__)
from dataclasses import fields
# Fields are ordered consistently, so ordered hashing is OK.
#
# No need to dispatch to superclass: fields() automatically gives us
# fields from the entire class hierarchy.
for f in fields(self):
key_builder.rec(key_hash, getattr(self, f.name))
# }}} # }}}
......
...@@ -4,6 +4,7 @@ import tempfile ...@@ -4,6 +4,7 @@ import tempfile
import pytest import pytest
from pytools.tag import Tag, tag_dataclass
from pytools.persistent_dict import (CollisionWarning, NoSuchEntryError, from pytools.persistent_dict import (CollisionWarning, NoSuchEntryError,
PersistentDict, ReadOnlyEntryError, WriteOncePersistentDict) PersistentDict, ReadOnlyEntryError, WriteOncePersistentDict)
...@@ -39,6 +40,11 @@ class PDictTestingKeyOrValue: ...@@ -39,6 +40,11 @@ class PDictTestingKeyOrValue:
# }}} # }}}
@tag_dataclass
class SomeTag(Tag):
value: str
def test_persistent_dict_storage_and_lookup(): def test_persistent_dict_storage_and_lookup():
try: try:
tmpdir = tempfile.mkdtemp() tmpdir = tempfile.mkdtemp()
...@@ -51,7 +57,9 @@ def test_persistent_dict_storage_and_lookup(): ...@@ -51,7 +57,9 @@ def test_persistent_dict_storage_and_lookup():
chr(65+randrange(26)) chr(65+randrange(26))
for i in range(n)) for i in range(n))
keys = [(randrange(2000), rand_str(), None) for i in range(20)] keys = [
(randrange(2000), rand_str(), None, SomeTag(rand_str()))
for i in range(20)]
values = [randrange(2000) for i in range(20)] values = [randrange(2000) for i in range(20)]
d = dict(list(zip(keys, values))) d = dict(list(zip(keys, values)))
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment