Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 Modernize boilerplate #354

Merged
merged 17 commits into from
Apr 15, 2024
12 changes: 6 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ requirements:

## Format the code using isort and black
format:
isort --profile black ccds hooks tests docs/scripts "{{ cookiecutter.repo_name }}/{{ cookiecutter.module_name }}"
black ccds hooks tests docs/scripts "{{ cookiecutter.repo_name }}/{{ cookiecutter.module_name }}"
isort --profile black ccds hooks tests docs/scripts
black ccds hooks tests docs/scripts

lint:
flake8 ccds hooks tests docs/scripts "{{ cookiecutter.repo_name }}/{{ cookiecutter.module_name }}"
isort --check --profile black ccds hooks tests docs/scripts "{{ cookiecutter.repo_name }}/{{ cookiecutter.module_name }}"
black --check ccds hooks tests docs/scripts "{{ cookiecutter.repo_name }}/{{ cookiecutter.module_name }}"
flake8 ccds hooks tests docs/scripts
isort --check --profile black ccds hooks tests docs/scripts
black --check ccds hooks tests docs/scripts


### DOCS
Expand Down
26 changes: 26 additions & 0 deletions tests/test_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,3 +183,29 @@ def verify_makefile_commands(root, config):
assert "clean Delete all compiled Python files" in stdout_output

assert result_returncode == 0

def lint(root):
pjbull marked this conversation as resolved.
Show resolved Hide resolved
"""Run the linters on the project."""
result = run(
["make", "lint"],
cwd=root,
stderr=PIPE,
stdout=PIPE,
)
result_returncode = result.returncode

encoding = sys.stdout.encoding

if encoding is None:
encoding = "utf-8"

# normally hidden by pytest except in failure we want this displayed
print("PATH=", os.getenv("PATH"))
print("\n======================= STDOUT ======================")
stdout_output = result.stdout.decode(encoding)
print(stdout_output)

print("\n======================= STDERR ======================")
print(result.stderr.decode(encoding))

assert result_returncode == 0
2 changes: 1 addition & 1 deletion {{ cookiecutter.repo_name }}/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ clean:
.PHONY: lint
lint:
flake8 {{ cookiecutter.module_name }}
isort --check --profile black {{ cookiecutter.module_name }}
black --check --config pyproject.toml {{ cookiecutter.module_name }}


## Format source code with black
.PHONY: format
format:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
import importlib.metadata
import json
import os
from pathlib import Path

from dotenv import load_dotenv
Expand All @@ -10,35 +7,10 @@
# Load environment variables from .env file if it exists
load_dotenv()


# Check if the package is installed as editable
def _is_editable():
# https://peps.python.org/pep-0660/#frontend-requirements
try:
dist = importlib.metadata.distribution("{{ cookiecutter.module_name }}")
direct_url_data = dist.read_text("direct_url.json")
if direct_url_data is None:
return False
return json.loads(direct_url_data).get("dir_info", {}).get("editable", False)
except importlib.metadata.PackageNotFoundError:
return False


IS_EDITABLE = _is_editable()

# Determine PROJ_ROOT path
if os.getenv("PROJ_ROOT"):
logger.debug("Reading PROJ_ROOT from environment variable.")
PROJ_ROOT = Path(os.getenv("PROJ_ROOT"))
elif IS_EDITABLE:
logger.debug("Setting PROJ_ROOT relative to editable package.")
PROJ_ROOT = Path(__file__).resolve().parents[1]
else:
logger.debug("Using current working directory as PROJ_ROOT.")
PROJ_ROOT = Path.cwd()
# Paths
PROJ_ROOT = Path(__file__).resolve().parents[1]
logger.info(f"PROJ_ROOT path is: {PROJ_ROOT}")

## Paths
DATA_DIR = PROJ_ROOT / "data"
RAW_DATA_DIR = DATA_DIR / "raw"
INTERIM_DATA_DIR = DATA_DIR / "interim"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@

@app.command()
def main(
input_path: Path = RAW_DATA_DIR / "some_dataset.csv",
output_path: Path = PROCESSED_DATA_DIR / "some_dataset.csv",
# ---- REPLACE DEFAULT PATHS AS APPROPRIATE ----
input_path: Path = RAW_DATA_DIR / "dataset.csv",
output_path: Path = PROCESSED_DATA_DIR / "dataset.csv",
# ----------------------------------------------
):
# ---- REPLACE THIS WITH YOUR OWN CODE ----
logger.info("Processing some dataset...")
logger.info("Processing dataset...")
for i in tqdm(range(10), total=10):
if i == 5:
logger.info("Something happened for iteration 5.")
logger.success("Processing some dataset complete.")
logger.success("Processing dataset complete.")
# -----------------------------------------


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,17 @@

@app.command()
def main(
input_path: Path = PROCESSED_DATA_DIR / "some_dataset.csv",
output_path: Path = PROCESSED_DATA_DIR / "some_features.csv",
# ---- REPLACE DEFAULT PATHS AS APPROPRIATE ----
input_path: Path = PROCESSED_DATA_DIR / "dataset.csv",
output_path: Path = PROCESSED_DATA_DIR / "features.csv",
# -----------------------------------------
):
# ---- REPLACE THIS WITH YOUR OWN CODE ----
logger.info("Generating some features from some dataset...")
logger.info("Generating features from dataset...")
for i in tqdm(range(10), total=10):
if i == 5:
logger.info("Something happened for iteration 5.")
logger.success("Some features generation complete.")
logger.success("Features generation complete.")
# -----------------------------------------


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

@app.command()
def main(
# ---- REPLACE DEFAULT PATHS AS APPROPRIATE ----
features_path: Path = PROCESSED_DATA_DIR / "test_features.csv",
model_path: Path = MODELS_DIR / "some_model.pkl",
predictions_path: Path = PROCESSED_DATA_DIR / "some_predictions.csv",
model_path: Path = MODELS_DIR / "model.pkl",
predictions_path: Path = PROCESSED_DATA_DIR / "test_predictions.csv",
# -----------------------------------------
):
# ---- REPLACE THIS WITH YOUR OWN CODE ----
logger.info("Performing inference for some model...")
logger.info("Performing inference for model...")
for i in tqdm(range(10), total=10):
if i == 5:
logger.info("Something happened for iteration 5.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@

@app.command()
def main(
features_path: Path = PROCESSED_DATA_DIR / "some_features.csv",
labels_path: Path = PROCESSED_DATA_DIR / "some_labels.csv",
model_path: Path = MODELS_DIR / "some_model.pkl",
# ---- REPLACE DEFAULT PATHS AS APPROPRIATE ----
features_path: Path = PROCESSED_DATA_DIR / "features.csv",
labels_path: Path = PROCESSED_DATA_DIR / "labels.csv",
model_path: Path = MODELS_DIR / "model.pkl",
# -----------------------------------------
):
# ---- REPLACE THIS WITH YOUR OWN CODE ----
logger.info("Training some model...")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@

@app.command()
def main(
input_path: Path = PROCESSED_DATA_DIR / "some_dataset.csv",
output_path: Path = FIGURES_DIR / "some_plot.png",
# ---- REPLACE DEFAULT PATHS AS APPROPRIATE ----
input_path: Path = PROCESSED_DATA_DIR / "dataset.csv",
output_path: Path = FIGURES_DIR / "plot.png",
# -----------------------------------------
):
# ---- REPLACE THIS WITH YOUR OWN CODE ----
logger.info("Generating some plot from some data...")
logger.info("Generating plot from data...")
for i in tqdm(range(10), total=10):
if i == 5:
logger.info("Something happened for iteration 5.")
Expand Down
Loading