Skip to content

Commit

Permalink
Define job id length as a constant instead of a repeated magic number. (
Browse files Browse the repository at this point in the history
#605)

* Define job id length as a constant instead of a repeated magic number.

* Rename ID_LENGTH.
  • Loading branch information
bdice authored and vyasr committed May 2, 2022
1 parent d54c42b commit fff47ed
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 10 deletions.
6 changes: 3 additions & 3 deletions signac/contrib/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
"null": type(None),
}


MAX_DEFAULT_ID = int("F" * 32, 16)
ID_LENGTH = 32
MAX_DEFAULT_ID = int("F" * ID_LENGTH, 16)


class _DictPlaceholder:
Expand Down Expand Up @@ -464,7 +464,7 @@ def _next_default_id(self):
self._next_default_id_ = len(self)
for i in range(len(self) + 1):
assert self._next_default_id_ < MAX_DEFAULT_ID
_id = str(hex(self._next_default_id_))[2:].rjust(32, "0")
_id = str(hex(self._next_default_id_))[2:].rjust(ID_LENGTH, "0")
self._next_default_id_ += 1
if _id not in self:
return _id
Expand Down
11 changes: 4 additions & 7 deletions signac/contrib/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,8 @@

logger = logging.getLogger(__name__)

INDEX_DEPRECATION_WARNING = (
"The index argument is deprecated and will be removed in signac 2.0."
)

JOB_ID_REGEX = re.compile("[a-f0-9]{32}")
JOB_ID_LENGTH = 32
JOB_ID_REGEX = re.compile(f"[a-f0-9]{{{JOB_ID_LENGTH}}}")

# The warning used for doc filter deprecation everywhere. Don't use
# triple-quoted multi-line string to avoid inserting newlines.
Expand Down Expand Up @@ -329,7 +326,7 @@ def min_len_unique_id(self):
"""
job_ids = list(self._find_job_ids())
tmp = set()
for i in range(32):
for i in range(JOB_ID_LENGTH):
tmp.clear()
for _id in job_ids:
if _id[:i] in tmp:
Expand Down Expand Up @@ -549,7 +546,7 @@ def open_job(self, statepoint=None, id=None):
# Worst case: no state point was provided and the state point cache
# missed. The Job will register itself in self._sp_cache when the
# state point is accessed.
if len(id) < 32:
if len(id) < JOB_ID_LENGTH:
# Resolve partial job ids (first few characters) into a full job id
job_ids = self._find_job_ids()
matches = [_id for _id in job_ids if _id.startswith(id)]
Expand Down

0 comments on commit fff47ed

Please sign in to comment.