mirror of
https://github.com/pre-commit/pre-commit-hooks.git
synced 2026-03-29 10:16:52 +00:00
Add comments
This commit is contained in:
parent
cf898e2ce2
commit
86dc39a572
1 changed files with 10 additions and 3 deletions
|
|
@ -14,19 +14,26 @@ def fix_strings(filename: str) -> int:
|
|||
cells = notebook_contents['cells']
|
||||
return_value = 0
|
||||
for cell in cells:
|
||||
if cell['cell_type'] == 'code':
|
||||
if cell.get('cell_type') == 'code' and 'source' in cell:
|
||||
# Each element in cell['source'] is a string that ends with \n,
|
||||
# except for the last element, which is why we don't join by \n
|
||||
source_in_1_line = ''.join(cell['source'])
|
||||
fixed = fix_strings_in_file_contents(source_in_1_line)
|
||||
if fixed != source_in_1_line:
|
||||
fixed_lines = fixed.split('\n')
|
||||
cell['source'] = [_ + '\n' for _ in fixed_lines[:-1]] + [fixed_lines[-1]]
|
||||
cell['source'] = (
|
||||
[_ + '\n' for _ in fixed_lines[:-1]]
|
||||
+ [fixed_lines[-1]]
|
||||
)
|
||||
return_value = 1
|
||||
|
||||
if return_value == 1:
|
||||
notebook_contents['cells'] = cells
|
||||
with open(filename, 'w') as f:
|
||||
json.dump(notebook_contents, f, indent=1)
|
||||
f.write("\n") # because json.dump doesn't put \n at the end
|
||||
# Jupyter notebooks (.ipynb) always ends with a new line
|
||||
# but json.dump does not.
|
||||
f.write("\n")
|
||||
|
||||
return return_value
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue