Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
payno
workflow_concepts
Commits
c9da8e28
Commit
c9da8e28
authored
Jun 11, 2021
by
Wout De Nolf
Browse files
hashing: support multi-type sorting
parent
4b7317b7
Changes
2
Hide whitespace changes
Inline
Side-by-side
esrftaskgraph/esrftaskgraph/hashing.py
View file @
c9da8e28
...
...
@@ -12,6 +12,26 @@ def classhashdata(cls):
return
qualname
(
cls
).
encode
()
def
multitype_sorted
(
sequence
,
key
=
None
):
try
:
return
sorted
(
sequence
,
key
=
key
)
except
TypeError
:
pass
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
[
item
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
)):
_hash
.
update
(
value
.
tobytes
())
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
:
...
...
esrftaskgraph/tests/test_hashing.py
View file @
c9da8e28
...
...
@@ -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
())
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment