From 50da65185242c2b3b93c4f1d4a5adb620db15b2d Mon Sep 17 00:00:00 2001 From: Lex Nederbragt Date: Mon, 3 Jan 2022 15:17:01 +0100 Subject: [PATCH] Combining the best of #216 and #223 --- lib/doconce/ipynb.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/lib/doconce/ipynb.py b/lib/doconce/ipynb.py index ac8b1485..5818d42a 100644 --- a/lib/doconce/ipynb.py +++ b/lib/doconce/ipynb.py @@ -760,22 +760,21 @@ def subst(m): replace this ID with a hash of the 'source' field. If needed, add a running number to duplicates to ensure uniqueness. """ - hashed_ids = {} # dict of created hashes - for cell in cells: - hashed_id = hashlib.sha224(cell['source'].encode('utf-8')).hexdigest()[:8] - cell['id'] = hashed_id - if hashed_id in hashed_ids: - # append running number for each next one, - # use the current count for this hash to append _1, _2, ... - cell['id'] = hashed_id + "_" + str(hashed_ids[hashed_id]) - hashed_ids[hashed_id] = hashed_ids.get(hashed_id, 0) + 1 - # Replace the random id with a reproducible hash of the content (issue #213) # nbformat 5.1.3 creates random cell ID with `uuid.uuid4().hex[:8]` + hashed_ids = {} # dict of created hashes for i in range(len(cells)): if 'id' in cell.keys(): - cell_content = str(i) + cells[i]['source'] - cells[i]['id'] = hashlib.sha224(cell_content.encode()).hexdigest()[:8] + cell_content = cells[i]['source'] + hashed_id = hashlib.sha224(cell_content.encode()).hexdigest()[:8] + cells[i]['id'] = hashed_id + # check for dupliatce IDs + if hashed_id in hashed_ids: + # append running number for each next one, + # use the current count for this hash to append _1, _2, ... + cells[i]['id'] = hashed_id + "_" + str(hashed_ids[hashed_id]) + # add to dict with existing IDs + hashed_ids[hashed_id] = hashed_ids.get(hashed_id, 0) + 1 # Create the notebook in string format nb = new_notebook(cells=cells)