Skip to content

Commit

Permalink
Type hints; fix #8; bump to v0.2.3
Browse files Browse the repository at this point in the history
  • Loading branch information
miek770 committed Nov 20, 2024
1 parent 1628f82 commit 88f9798
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 14 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ Once installed, the utility is used on the command line; see `--help` for detail

> The `--pin` option was removed in version 0.2.2. See issue #7 for details.
> Starting with version 0.2.3, `PATH` can be either a filename or a directory.
## Development

Contributions are welcome; please:
Expand Down
11 changes: 8 additions & 3 deletions nbreqs/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def main(path: str, quiet: bool, verbose: bool, dry_run: bool):

if not dir.exists():
print(f"Invalid path: {dir}")
return 1 # Exit with error
exit(1) # Exit with error

# --verbose is implied with --dry-run
if dry_run:
Expand All @@ -58,7 +58,10 @@ def main(path: str, quiet: bool, verbose: bool, dry_run: bool):
if verbose:
quiet = False

explore_directory(dir, quiet, verbose, dry_run)
if dir.is_file():
process_notebook(dir, quiet, verbose, dry_run)
else:
explore_directory(dir, quiet, verbose, dry_run)


def explore_directory(dir: Path, quiet: bool, verbose: bool, dry_run: bool):
Expand Down Expand Up @@ -102,7 +105,9 @@ def process_notebook(nb: Path, quiet: bool, verbose: bool, dry_run: bool):

# Create the requirements file
if len(ext_libs) and not dry_run:
with open(Path(f"{nb._str.strip(ext)}_requirements.txt"), "w") as req_file:
with open(
Path(f"{nb.as_posix().strip(ext)}_requirements.txt"), "w"
) as req_file:
for lib in ext_libs:
req_file.write(f"{lib}\n")

Expand Down
27 changes: 26 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "nbreqs"
version = "0.2.2"
version = "0.2.3"
description = "Generate notebook_requirements.txt files for Jupyter Notebooks (.ipynb files)."
authors = ["Michel Lavoie <[email protected]>"]
license = "MIT"
Expand All @@ -18,6 +18,9 @@ requests = "^2.32.3"
pytest = "^8.3.3"
black = "^24.8.0"
pre-commit = "^3.8.0"
click = "^8.1.7"
types-requests = "^2.32.0.20241016"
types-stdlib-list = "^0.8.3.4"

[tool.poetry.scripts]
nbreqs = "nbreqs.cli:main"
Expand Down
26 changes: 26 additions & 0 deletions tests/cli_file_path.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import numpy\n",
"import pandas as pd\n",
"from sklearn.model_selection import train_test_split\n",
"import rich_click as click\n",
"from nbconvert import PythonExporter\n",
"from stdlib_list import stdlib_list\n"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
20 changes: 14 additions & 6 deletions tests/test_cli_functions.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from nbreqs.cli import filter_out_std_libs, is_on_pypi
from nbreqs.cli import filter_out_std_libs, is_on_pypi, main
from os import mkdir, rmdir
from pathlib import Path
from click.testing import CliRunner, Result


invalid_lib = "dasoie;oihag;oiehge"
invalid_lib: str = "dasoie;oihag;oiehge"


def test_filter_out_std_libs():
libs = {
def test_filter_out_std_libs() -> None:
libs: set = {
"numpy",
"click",
"pathlib",
Expand All @@ -18,9 +19,16 @@ def test_filter_out_std_libs():
]


def test_is_not_on_pypi():
def test_is_not_on_pypi() -> None:
assert is_on_pypi(invalid_lib) is False


def test_is_on_pypi():
def test_is_on_pypi() -> None:
assert is_on_pypi("click") is True


def test_cli_file_path() -> None:
file_path: str = "tests/cli_file_path.ipynb"
runner: CliRunner = CliRunner()
result: Result = runner.invoke(main, [file_path])
assert result.exit_code == 0
6 changes: 3 additions & 3 deletions tests/test_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
ext: str = "ipynb"


def test_basic_imports():
def test_basic_imports() -> None:
dir: str = "tests"
file: str = "basic_imports"

Expand All @@ -23,7 +23,7 @@ def test_basic_imports():
]


def test_latex_cell():
def test_latex_cell() -> None:
dir: str = "tests"
file: str = "latex_cell"

Expand All @@ -35,7 +35,7 @@ def test_latex_cell():
]


def test_non_pypi_pkg():
def test_non_pypi_pkg() -> None:
dir: str = "tests"
file: str = "non_pypi_pkg"

Expand Down

0 comments on commit 88f9798

Please sign in to comment.