Commit c9da8e28 authored by Wout De Nolf's avatar Wout De Nolf
Browse files

hashing: support multi-type sorting

parent 4b7317b7
......@@ -12,6 +12,26 @@ def classhashdata(cls):
return qualname(cls).encode()
def multitype_sorted(sequence, key=None):
return sorted(sequence, key=key)
except TypeError:
if key is None:
key = lambda item: item
adict = dict()
for item in sequence:
typename = type(key(item)).__name__
adict.setdefault(typename, list()).append(item)
return [
for _, items in sorted(adict.items(), key=lambda tpl: tpl[0])
for item in sorted(items, key=key)
def uhash(value, _hash=None):
"""Universial hash (as opposed to python's hash).
This is an example. Must find something better.
......@@ -42,13 +62,12 @@ def uhash(value, _hash=None):
elif isinstance(value, (numpy.ndarray, numpy.number)):
elif isinstance(value, Mapping):
keys, values = zip(*sorted(value.items(), key=lambda item: item[0]))
keys, values = zip(*multitype_sorted(value.items(), key=lambda item: item[0]))
uhash(keys, _hash=_hash)
uhash(values, _hash=_hash)
elif isinstance(value, Set):
# Unordered
# TODO: the values in the set are no necessarily sortable
raise TypeError(value, type(value))
values = multitype_sorted(value)
uhash(values, _hash=_hash)
elif isinstance(value, Iterable):
# Ordered
for v in value:
......@@ -25,6 +25,12 @@ def test_hashing_unique():
assert hashing.uhash(alist) == hashing.uhash(list(alist))
assert hashing.uhash(alist) != hashing.uhash(alist[::-1])
assert hashing.uhash(alist) != hashing.uhash(tuple(alist))
assert hashing.uhash(alist) != hashing.uhash(set(alist))
aset = set(unique_values)
assert hashing.uhash(aset) == hashing.uhash(set(aset))
assert hashing.uhash(aset) != hashing.uhash(tuple(aset))
assert hashing.uhash(aset) != hashing.uhash(list(alist))
andarray = numpy.arange(10)
assert hashing.uhash(andarray) == hashing.uhash(andarray.copy())
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment