Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move calc_id into job.py #873

Merged
merged 2 commits into from
Dec 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Changed
- Tests are run with ``xfail_strict = True`` (#850).
- Detection of an invalid config will raise an error rather than a debug log (#855).
- The package namespace has been flattened so that most functionality is directly available in the ``signac`` namespace (#756, #868).
- The ``calc_id`` function has been moved from the ``hashing`` module to the ``job`` module (#873).

Removed
+++++++
Expand Down
35 changes: 0 additions & 35 deletions signac/hashing.py

This file was deleted.

30 changes: 27 additions & 3 deletions signac/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@
"""Job class defined here."""

import errno
import hashlib
import json
import logging
import os
import shutil
from copy import deepcopy
from json import JSONDecodeError
from threading import RLock
from typing import FrozenSet

Expand All @@ -18,15 +19,38 @@
json_attr_dict_validator,
)
from ._synced_collections.errors import KeyTypeError
from ._synced_collections.utils import SyncedCollectionJSONEncoder
from ._utility import _mkdir_p
from .errors import DestinationExistsError, JobsCorruptedError
from .h5store import H5StoreManager
from .hashing import calc_id
from .sync import sync_jobs

logger = logging.getLogger(__name__)


def calc_id(statepoint):
"""Calculate and return a hash value for the given statepoint.

The hash is computed as an MD5 checksum of the input data. The input data
is first encoded as JSON, with dictionary keys sorted to ensure the hash
is reproducible.

Parameters
----------
statepoint : dict
A JSON-encodable mapping.

Returns
-------
str
Encoded hash in hexadecimal format.
"""
blob = json.dumps(statepoint, cls=SyncedCollectionJSONEncoder, sort_keys=True)
m = hashlib.md5()
m.update(blob.encode())
return m.hexdigest()


# Note: All children of _StatePointDict will be of its parent type because they
# share a backend and the SyncedCollection registry parses the classes in order
# of registration. _If_ we need more control over this, that process can be
Expand Down Expand Up @@ -203,7 +227,7 @@ def load(self, job_id):
"""
try:
data = self._load_from_resource()
except JSONDecodeError:
except json.JSONDecodeError:
raise JobsCorruptedError([job_id])

if calc_id(data) != job_id:
Expand Down
3 changes: 1 addition & 2 deletions signac/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@
)
from .filterparse import _add_prefix, _root_keys, parse_filter
from .h5store import H5StoreManager
from .hashing import calc_id
from .job import Job
from .job import Job, calc_id
from .schema import ProjectSchema
from .sync import sync_projects
from .version import SCHEMA_VERSION, __version__
Expand Down
2 changes: 1 addition & 1 deletion tests/test_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
StatepointParsingError,
WorkspaceError,
)
from signac.hashing import calc_id
from signac.job import calc_id
from signac.linked_view import _find_all_links
from signac.project import JobsCursor, Project # noqa: F401
from signac.schema import ProjectSchema
Expand Down