From 7961dcbc1c759d0796a66f412e00bbf240a8c025 Mon Sep 17 00:00:00 2001 From: Vini Salazar <17276653+vinisalazar@users.noreply.github.com> Date: Sat, 24 Apr 2021 12:47:58 -0300 Subject: [PATCH] Make shorter Environment hashes --- CHANGELOG.md | 1 + bioprov/src/config.py | 8 +++++--- bioprov/src/prov.py | 4 ++-- bioprov/tests/test_src_prov.py | 4 +++- 4 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b2888e8..833b620 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ * Improve reserved aminoacid characters [x] * Add `SeqFile.max_seq` and `.min_seq` properties [x] * Patch `Project` deserializer to improve BioProvDocument creation [x] +* Make shorter Environment hashes [x] ### v0.1.22 * Simplify `bp.load_project()` function [x] diff --git a/bioprov/src/config.py b/bioprov/src/config.py index eac067b..a6aa02d 100644 --- a/bioprov/src/config.py +++ b/bioprov/src/config.py @@ -270,6 +270,7 @@ def __init__(self): Class constructor. All attributes are empty and are initialized with self.update() """ self.env_hash = None + self.env_hash_long = None self.env_dict = None self.user = None self.env_namespace = None @@ -277,7 +278,7 @@ def __init__(self): self._actedOnBehalfOf = False def __repr__(self): - return self.env_hash + return f"BioProvEnvironment{self.env_hash}" @property def actedOnBehalfOf(self): @@ -294,9 +295,10 @@ def update(self): """ env_dict = dict(os.environ.items()) env_hash = dict_to_sha256(env_dict) - if env_hash != self.env_hash: + if env_hash != self.env_hash_long: self.env_dict = env_dict - self.env_hash = env_hash + self.env_hash = env_hash[:7] + self.env_hash_long = env_hash # this is only to prevent build errors try: diff --git a/bioprov/src/prov.py b/bioprov/src/prov.py index e2e7ee0..f95ff76 100644 --- a/bioprov/src/prov.py +++ b/bioprov/src/prov.py @@ -287,14 +287,14 @@ def _create_program_entities(self, sample, kind="Sample"): ): if self.add_attributes: self._agents[_env_hash] = _user_bundle.agent( - f"envs:{_env}", + f"envs:{_env_hash}", other_attributes=build_prov_attributes( _env.env_dict, _env.env_namespace ), ) else: self._agents[_env_hash] = _user_bundle.agent( - f"envs:{_env}" + f"envs:{_env_hash}" ) if not _env.actedOnBehalfOf: _user_bundle.actedOnBehalfOf( diff --git a/bioprov/tests/test_src_prov.py b/bioprov/tests/test_src_prov.py index 0fbeb7e..c99e2d7 100644 --- a/bioprov/tests/test_src_prov.py +++ b/bioprov/tests/test_src_prov.py @@ -31,9 +31,11 @@ def test_EnvProv(): :return: """ env = Environment() + sh = dict_to_sha256(env.env_dict) for statement in ( env.env_dict == dict(environ.items()), - env.env_hash == dict_to_sha256(env.env_dict), + env.env_hash_long == sh, + env.env_hash == sh[:7], ): assert statement