-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add validation headers script (#225)
* add validation headers script * cleanup * cleanup
- Loading branch information
Showing
5 changed files
with
64 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import argparse | ||
from pathlib import Path | ||
|
||
import nbformat | ||
|
||
HEADERS = ( | ||
"## 🌍 Use case:", | ||
"## ❓ Quality assessment question", | ||
"## 📢 Quality assessment statement", | ||
"## 📋 Methodology", | ||
"## 📈 Analysis and results", | ||
"## ℹ️ If you want to know more", | ||
) | ||
|
||
|
||
def validate_headers(path: Path) -> None: | ||
notebook = nbformat.read(path, nbformat.NO_CONVERT) | ||
|
||
title_count = 0 | ||
headers_count = dict.fromkeys(HEADERS, 0) | ||
for cell in notebook.cells: | ||
if cell["cell_type"] != "markdown": | ||
continue | ||
|
||
for line in cell.get("source", "").splitlines(): | ||
line = line.strip() | ||
if line.startswith("# "): | ||
title_count += 1 | ||
continue | ||
|
||
for header in headers_count: | ||
if line.startswith(header): | ||
headers_count[header] += 1 | ||
|
||
assert title_count == 1, f"{path=!s}: Invalid {title_count=}" | ||
for header, header_count in headers_count.items(): | ||
assert header_count == 1, f"{path=!s}: Invalid {header_count=} of {header=}" | ||
|
||
|
||
def main(paths: list[Path]) -> None: | ||
for path in paths: | ||
validate_headers(path) | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument("paths", action="store", type=Path, nargs="*") | ||
args = parser.parse_args() | ||
main(args.paths) |