Skip to content

Commit 0179e6c

Browse files
authored
Merge pull request #222 from djarecka/mnt/hash
tests and small edits to has_value/hash_functions (closes #155)
2 parents 349d954 + 343015d commit 0179e6c

File tree

3 files changed

+86
-12
lines changed

3 files changed

+86
-12
lines changed

pydra/engine/helpers.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import subprocess as sp
1111

1212
from .specs import Runtime, File, attr_fields
13-
from .helpers_file import is_existing_file, hash_file, copyfile, is_existing_file
13+
from .helpers_file import hash_file, copyfile, is_existing_file
1414

1515

1616
def ensure_list(obj, tuple2list=False):
@@ -444,21 +444,21 @@ def hash_function(obj):
444444

445445
def hash_value(value, tp=None, metadata=None):
446446
"""calculating hash or returning values recursively"""
447+
if metadata is None:
448+
metadata = {}
447449
if isinstance(value, (tuple, list)):
448450
return [hash_value(el, tp, metadata) for el in value]
449451
elif isinstance(value, dict):
450452
dict_hash = {k: hash_value(v, tp, metadata) for (k, v) in value.items()}
451453
# returning a sorted object
452-
return sorted(dict_hash.items(), key=lambda x: x[0])
454+
return [list(el) for el in sorted(dict_hash.items(), key=lambda x: x[0])]
453455
else: # not a container
454456
if (
455457
(tp is File or "pydra.engine.specs.File" in str(tp))
456458
and is_existing_file(value)
457459
and "container_path" not in metadata
458460
):
459461
return hash_file(value)
460-
elif isinstance(value, tuple):
461-
return list(value)
462462
else:
463463
return value
464464

pydra/engine/tests/test_helpers.py

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,18 @@
44
import cloudpickle as cp
55

66
from .utils import multiply
7-
from .. import helpers
7+
from ..helpers import hash_value, hash_function, save, create_pyscript
88
from .. import helpers_file
9+
from ..specs import File
910

1011

1112
def test_save(tmpdir):
1213
outdir = Path(tmpdir)
1314
with pytest.raises(ValueError):
14-
helpers.save(tmpdir)
15+
save(tmpdir)
1516
foo = multiply(name="mult", x=1, y=2)
1617
# save task
17-
helpers.save(outdir, task=foo)
18+
save(outdir, task=foo)
1819
del foo
1920
# load saved task
2021
task_pkl = outdir / "_task.pklz"
@@ -24,7 +25,7 @@ def test_save(tmpdir):
2425
# execute task and save result
2526
res = foo()
2627
assert res.output.out == 2
27-
helpers.save(outdir, result=res)
28+
save(outdir, result=res)
2829
del res
2930
# load saved result
3031
res_pkl = outdir / "_result.pklz"
@@ -35,10 +36,10 @@ def test_save(tmpdir):
3536
def test_create_pyscript(tmpdir):
3637
outdir = Path(tmpdir)
3738
with pytest.raises(Exception):
38-
helpers.create_pyscript(outdir, "checksum")
39+
create_pyscript(outdir, "checksum")
3940
foo = multiply(name="mult", x=1, y=2)
40-
helpers.save(outdir, task=foo)
41-
pyscript = helpers.create_pyscript(outdir, foo.checksum)
41+
save(outdir, task=foo)
42+
pyscript = create_pyscript(outdir, foo.checksum)
4243
assert pyscript.exists()
4344

4445

@@ -50,3 +51,76 @@ def test_hash_file(tmpdir):
5051
helpers_file.hash_file(outdir / "test.file")
5152
== "9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"
5253
)
54+
55+
56+
def test_hashfun_float():
57+
import math
58+
59+
pi_50 = 3.14159265358979323846264338327950288419716939937510
60+
pi_15 = 3.141592653589793
61+
pi_10 = 3.1415926536
62+
# comparing for x that have the same x.as_integer_ratio()
63+
assert (
64+
math.pi.as_integer_ratio()
65+
== pi_50.as_integer_ratio()
66+
== pi_15.as_integer_ratio()
67+
)
68+
assert hash_function(math.pi) == hash_function(pi_15) == hash_function(pi_50)
69+
# comparing for x that have different x.as_integer_ratio()
70+
assert math.pi.as_integer_ratio() != pi_10.as_integer_ratio()
71+
assert hash_function(math.pi) != hash_function(pi_10)
72+
73+
74+
def test_hash_value_dict():
75+
dict1 = {"a": 10, "b": 5}
76+
dict2 = {"b": 5, "a": 10}
77+
assert (
78+
hash_value(dict1)
79+
== hash_value(dict2)
80+
== [["a", hash_value(10)], ["b", hash_value(5)]]
81+
== [["a", 10], ["b", 5]]
82+
)
83+
84+
85+
def test_hash_value_list_tpl():
86+
lst = [2, 5.6, "ala"]
87+
tpl = (2, 5.6, "ala")
88+
assert hash_value(lst) == [hash_value(2), hash_value(5.6), hash_value("ala")] == lst
89+
assert hash_value(lst) == hash_value(tpl)
90+
91+
92+
def test_hash_value_list_dict():
93+
lst = [2, {"a": "ala", "b": 1}]
94+
hash_value(lst)
95+
assert (
96+
hash_value(lst)
97+
== [hash_value(2), hash_value([["a", "ala"], ["b", 1]])]
98+
== [2, [["a", "ala"], ["b", 1]]]
99+
)
100+
101+
102+
def test_hash_value_files(tmpdir):
103+
file_1 = tmpdir.join("file_1.txt")
104+
file_2 = tmpdir.join("file_2.txt")
105+
with open(file_1, "w") as f:
106+
f.write("hello")
107+
with open(file_2, "w") as f:
108+
f.write("hello")
109+
110+
assert hash_value(file_1, tp=File) == hash_value(file_2, tp=File)
111+
assert hash_value(file_1, tp=str) != hash_value(file_2, tp=str)
112+
assert hash_value(file_1) != hash_value(file_2)
113+
114+
115+
def test_hash_value_files_list(tmpdir):
116+
file_1 = tmpdir.join("file_1.txt")
117+
file_2 = tmpdir.join("file_2.txt")
118+
with open(file_1, "w") as f:
119+
f.write("hello")
120+
with open(file_2, "w") as f:
121+
f.write("hi")
122+
123+
assert hash_value([file_1, file_2], tp=File) == [
124+
hash_value(file_1, tp=File),
125+
hash_value(file_2, tp=File),
126+
]

pydra/engine/tests/test_specs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,7 @@ def test_input_file_hash_4(tmpdir):
289289

290290
# checking specific hash value
291291
hash1 = inputs(in_file=[{"file": file, "int": 3}]).hash
292-
assert hash1 == "0148961a66106e15e55de3ab7e8c49e6227b6db7cdc614dcbae20406feb2548a"
292+
assert hash1 == "e0555e78a40a02611674b0f48da97cdd28eee7e9885ecc17392b560c14826f06"
293293

294294
# the same file, but int field changes
295295
hash1a = inputs(in_file=[{"file": file, "int": 5}]).hash

0 commit comments

Comments
 (0)