From 078c1be69ff5b3d055297ed10057e353dd52a90c Mon Sep 17 00:00:00 2001 From: Sebastian Straub Date: Fri, 8 Dec 2023 15:49:12 +0100 Subject: [PATCH 1/8] update and extend pyproject.toml --- {{cookiecutter.project_slug}}/pyproject.toml | 37 +++++++++++++++----- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/{{cookiecutter.project_slug}}/pyproject.toml b/{{cookiecutter.project_slug}}/pyproject.toml index b03fe76..c8d3120 100644 --- a/{{cookiecutter.project_slug}}/pyproject.toml +++ b/{{cookiecutter.project_slug}}/pyproject.toml @@ -3,7 +3,7 @@ name = "{{ cookiecutter.project_slug }}" version = "0.1.0" description = "{{ cookiecutter.project_short_description }}" authors = ["{{ cookiecutter.full_name }} <{{ cookiecutter.email }}>"] -license = "Proprietary" +license = "proprietary" packages = [{ include = "{{ cookiecutter.module_name }}", from = "src" }, ] include = ["src/{{ cookiecutter.module_name }}/res/*"] {% if cookiecutter.create_cli == "yes" %} @@ -11,17 +11,38 @@ include = ["src/{{ cookiecutter.module_name }}/res/*"] {{ cookiecutter.project_slug }} = "{{ cookiecutter.module_name }}.main:app" {% endif %} [tool.poetry.dependencies] -python = "^3.10"{% if cookiecutter.config_file == 'hocon' %} +python = "^3.11"{% if cookiecutter.config_file == 'hocon' %} pyhocon = "^0.3.59"{% elif cookiecutter.config_file == 'yaml' %} PyYAML = "^6.0"{% endif %}{% if cookiecutter.create_cli == 'yes' %} -typer = {extras = ["all"], version = "^0.7.0"}{% endif %} +typer = {extras = ["all"], version = "^0.9.0"}{% endif %} -[tool.poetry.dev-dependencies]{% if cookiecutter.code_formatter == 'black' %} -black = "^22.10"{% endif %} -pre-commit = "^2.20" -pytest = "^7.2" -pytest-cov = "^4.0"{% if cookiecutter.use_notebooks == 'yes' %} +[tool.poetry.group.test.dependencies] +pytest = "^7.0" +pytest-cov = "^4.0" + +[tool.poetry.group.linter.dependencies]{% if cookiecutter.code_formatter == 'black' %} +black = "^23.11"{% else %} +ruff = "^0.1.7"{% endif %} +isort = "^5.12.0" + +[tool.poetry.group.dev.dependencies] +pre-commit = "^3.0"{% if cookiecutter.use_notebooks == 'yes' %} jupyterlab = "^3.5"{% endif %} +{% if cookiecutter.code_formatter != 'black' %} +[tool.ruff] +line-length = 100 +src = ["src", "tests"] +ignore = ["F401"] +{% endif %} +[tool.isort]{% if cookiecutter.code_formatter == 'black' %} +profile = "black"{% else %} +py_version = 310 +line_length = 100 +multi_line_output = 3{% endif %} + +[tool.pytest.ini_options] +minversion = "7.0" +testpaths = ["tests"] [build-system] requires = ["poetry-core"] From 867a1d392a79f34e8b698fbc53b250bac85d31e9 Mon Sep 17 00:00:00 2001 From: Sebastian Straub Date: Fri, 8 Dec 2023 15:54:44 +0100 Subject: [PATCH 2/8] update and extend pre-commit hooks --- .../.pre-commit-config.yaml | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml index 32091a9..a808417 100644 --- a/{{cookiecutter.project_slug}}/.pre-commit-config.yaml +++ b/{{cookiecutter.project_slug}}/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.4.0 + rev: 'v4.4.0' hooks: - id: check-added-large-files - id: check-ast @@ -13,13 +13,21 @@ repos: - id: mixed-line-ending - id: trailing-whitespace{% if cookiecutter.code_formatter == 'black' %} - repo: https://github.com/psf/black - rev: stable + rev: 'stable' hooks: - id: black language_version: python3.8 exclude: ^notebooks{% else %} - - repo: https://github.com/PyCQA/flake8 - rev: '5.0.4' + - repo: https://github.com/astral-sh/ruff-pre-commit + rev: 'v0.1.7' hooks: - - id: flake8 - args: ['--max-line-length=100', '--ignore=F401,W503', '--exclude=tests/*']{% endif %} + - id: ruff{% endif %} + - repo: https://github.com/pycqa/isort + rev: '5.12.0' + hooks: + - id: isort + - repo: https://github.com/asottile/pyupgrade + rev: 'v3.15.0' + hooks: + - id: pyupgrade + args: [ --py38-plus ] From 7e1cf570c9d2ea8017d2bc342a872c08a180fbeb Mon Sep 17 00:00:00 2001 From: Sebastian Straub Date: Fri, 8 Dec 2023 16:02:11 +0100 Subject: [PATCH 3/8] update and extend .gitlab-ci.yml --- {{cookiecutter.project_slug}}/.gitlab-ci.yml | 22 +++++++++++++------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/{{cookiecutter.project_slug}}/.gitlab-ci.yml b/{{cookiecutter.project_slug}}/.gitlab-ci.yml index 316e614..65790a0 100644 --- a/{{cookiecutter.project_slug}}/.gitlab-ci.yml +++ b/{{cookiecutter.project_slug}}/.gitlab-ci.yml @@ -16,7 +16,7 @@ variables: {%- if cookiecutter.package_manager == 'conda' %} CONDA_PKGS_DIRS: $CI_PROJECT_DIR/.cache/conda {%- elif cookiecutter.package_manager == 'poetry' %} - POETRY_VERSION: 1.0.5 + POETRY_VERSION: 1.7.1 POETRY_CACHE_DIR: $CI_PROJECT_DIR/.cache/poetry {%- endif %} @@ -30,7 +30,7 @@ stages: {% if cookiecutter.package_manager == 'poetry' -%} build-wheel: - image: python:3-slim + image: python:3.11 stage: build artifacts: name: 'app-wheel' @@ -41,7 +41,7 @@ build-wheel: - pip install poetry==$POETRY_VERSION - poetry build -f wheel {% else -%} build-wheel: - image: python:3-slim + image: python:3.11 stage: build artifacts: name: 'app-wheel' @@ -54,20 +54,26 @@ build-wheel: {% if cookiecutter.package_manager == 'poetry' -%} test-unit: stage: test - image: python:3-slim + image: python:3.11 cache: key: files: - - pyproject.toml + - poetry.lock paths: - $PIP_CACHE_DIR - $POETRY_CACHE_DIR + artifacts: + reports: + coverage_report: + coverage_format: cobertura + path: coverage.xml + coverage: '/(?i)total.*? (100(?:\.0+)?\%|[1-9]?\d(?:\.\d+)?\%)$/' before_script: - pip install poetry==$POETRY_VERSION - - poetry install --no-root + - poetry install --only=main,test - source `poetry env info --path`/bin/activate script: - - pytest tests {% elif cookiecutter.package_manager == 'conda' -%} + - pytest tests --cov src --cov-report=term --cov-report xml:coverage.xml{% elif cookiecutter.package_manager == 'conda' -%} test-unit: stage: test image: continuumio/miniconda3 @@ -89,7 +95,7 @@ test-unit: - pytest tests {% elif cookiecutter.package_manager == 'pip' -%} test-unit: stage: test - image: python:3-slim + image: python:3.11 cache: key: files: From 4ad6065406c2fe994ad0c10106c31cba100d420f Mon Sep 17 00:00:00 2001 From: Sebastian Straub Date: Fri, 8 Dec 2023 16:07:53 +0100 Subject: [PATCH 4/8] update poetry info in readme --- {{cookiecutter.project_slug}}/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/{{cookiecutter.project_slug}}/README.md b/{{cookiecutter.project_slug}}/README.md index 44f892f..617689e 100644 --- a/{{cookiecutter.project_slug}}/README.md +++ b/{{cookiecutter.project_slug}}/README.md @@ -96,9 +96,9 @@ Note that we mainly use notebooks for experiments, visualizations and reports. E To build a distribution package (wheel), please use - python setup.py bdist_wheel + {% if cookiecutter.package_manager == 'poetry' %}poetry build{% else %}python setup.py bdist_wheel{% endif %} -this will clean up the build folder and then run the `bdist_wheel` command. +You will find your distribution package in the `dist` folder. ### Contributions From 0f6013276ae2d79d439e8d822a197b7118ac1bf9 Mon Sep 17 00:00:00 2001 From: Sebastian Straub Date: Fri, 8 Dec 2023 16:08:07 +0100 Subject: [PATCH 5/8] cleanup imports in setup.py --- {{cookiecutter.project_slug}}/setup.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/{{cookiecutter.project_slug}}/setup.py b/{{cookiecutter.project_slug}}/setup.py index 8ff3637..fc5e4e5 100644 --- a/{{cookiecutter.project_slug}}/setup.py +++ b/{{cookiecutter.project_slug}}/setup.py @@ -1,8 +1,4 @@ import os -import shutil -import subprocess -import sys -from distutils.cmd import Command from runpy import run_path from setuptools import find_packages, setup From 47c474ad3fb341a31b05349fec7a90f6e668876d Mon Sep 17 00:00:00 2001 From: Sebastian Straub Date: Fri, 8 Dec 2023 16:24:10 +0100 Subject: [PATCH 6/8] remove pytest.ini for poetry --- hooks/post_gen_project.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/hooks/post_gen_project.py b/hooks/post_gen_project.py index 3cf7a48..c52ca3c 100644 --- a/hooks/post_gen_project.py +++ b/hooks/post_gen_project.py @@ -18,12 +18,14 @@ 'requirements.txt', 'requirements-dev.txt', 'setup.py', + 'tests/pytest.ini', } files_conda = { 'environment.yml', 'environment-dev.yml', 'setup.py', + 'tests/pytest.ini', } files_poetry = { @@ -165,6 +167,7 @@ def handle_editor_settings(): print(f"Error: unsupported editor {editor_settings}") sys.exit(1) + def handle_ci(): ci_pipeline = '{{ cookiecutter.ci_pipeline }}' if ci_pipeline == "gitlab": From b298d3ee0913ed7a1aa55ac2f7fba71c8f702348 Mon Sep 17 00:00:00 2001 From: Sebastian Straub Date: Fri, 8 Dec 2023 16:24:22 +0100 Subject: [PATCH 7/8] readme update --- {{cookiecutter.project_slug}}/README.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/{{cookiecutter.project_slug}}/README.md b/{{cookiecutter.project_slug}}/README.md index 617689e..1239186 100644 --- a/{{cookiecutter.project_slug}}/README.md +++ b/{{cookiecutter.project_slug}}/README.md @@ -98,14 +98,13 @@ To build a distribution package (wheel), please use {% if cookiecutter.package_manager == 'poetry' %}poetry build{% else %}python setup.py bdist_wheel{% endif %} -You will find your distribution package in the `dist` folder. +You can find the build artifacts in the `dist` folder. ### Contributions Before contributing, please set up the pre-commit hooks to reduce errors and ensure consistency pip install -U pre-commit - pre-commit install If you run into any issues, you can remove the hooks again with `pre-commit uninstall`. From 095871db904e38c6f9e900c20edf38d72f5e5b40 Mon Sep 17 00:00:00 2001 From: Sebastian Straub Date: Fri, 8 Dec 2023 16:38:55 +0100 Subject: [PATCH 8/8] small changes so pre-commit hooks don't fail --- {{cookiecutter.project_slug}}/.gitlab-ci.yml | 8 ++++---- {{cookiecutter.project_slug}}/pyproject.toml | 2 +- .../src/{{cookiecutter.module_name}}/main.py | 1 - .../{{cookiecutter.module_name}}/main__cli.py | 17 +++++++---------- .../{{cookiecutter.module_name}}/util__yaml.py | 2 +- .../tests/test_example.py | 1 - 6 files changed, 13 insertions(+), 18 deletions(-) diff --git a/{{cookiecutter.project_slug}}/.gitlab-ci.yml b/{{cookiecutter.project_slug}}/.gitlab-ci.yml index 65790a0..90700c2 100644 --- a/{{cookiecutter.project_slug}}/.gitlab-ci.yml +++ b/{{cookiecutter.project_slug}}/.gitlab-ci.yml @@ -39,7 +39,7 @@ build-wheel: expire_in: 6 mos script: - pip install poetry==$POETRY_VERSION - - poetry build -f wheel {% else -%} + - poetry build -f wheel{% else -%} build-wheel: image: python:3.11 stage: build @@ -49,7 +49,7 @@ build-wheel: - dist/{{ cookiecutter.module_name }}-*.whl expire_in: 6 mos script: - - python setup.py dist {%- endif %} + - python setup.py dist{%- endif %} {% if cookiecutter.package_manager == 'poetry' -%} test-unit: @@ -92,7 +92,7 @@ test-unit: - source activate .venv - pip install dist/{{ cookiecutter.module_name }}-*.whl script: - - pytest tests {% elif cookiecutter.package_manager == 'pip' -%} + - pytest tests{% elif cookiecutter.package_manager == 'pip' -%} test-unit: stage: test image: python:3.11 @@ -109,7 +109,7 @@ test-unit: - pip install -r requirements.txt -r requirements-dev.txt - pip install dist/{{ cookiecutter.module_name }}-*.whl script: - - pytest tests {%- endif %} + - pytest tests{%- endif %} # Nonprod deployments diff --git a/{{cookiecutter.project_slug}}/pyproject.toml b/{{cookiecutter.project_slug}}/pyproject.toml index c8d3120..feb3d7b 100644 --- a/{{cookiecutter.project_slug}}/pyproject.toml +++ b/{{cookiecutter.project_slug}}/pyproject.toml @@ -8,7 +8,7 @@ packages = [{ include = "{{ cookiecutter.module_name }}", from = "src" }, ] include = ["src/{{ cookiecutter.module_name }}/res/*"] {% if cookiecutter.create_cli == "yes" %} [tool.poetry.scripts] -{{ cookiecutter.project_slug }} = "{{ cookiecutter.module_name }}.main:app" {% endif %} +{{ cookiecutter.project_slug }} = "{{ cookiecutter.module_name }}.main:app"{% endif %} [tool.poetry.dependencies] python = "^3.11"{% if cookiecutter.config_file == 'hocon' %} diff --git a/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main.py b/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main.py index 4d27bd2..021ce3e 100644 --- a/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main.py +++ b/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main.py @@ -1,4 +1,3 @@ - def main(): # TODO your journey starts here print("hello :)") diff --git a/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main__cli.py b/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main__cli.py index 755a70e..3df7895 100644 --- a/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main__cli.py +++ b/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/main__cli.py @@ -2,14 +2,11 @@ import typer -from {{ cookiecutter.module_name }} import __title__ , __version__{% if cookiecutter.config_file != 'none' %}, util{% endif %} +from {{ cookiecutter.module_name }} import __title__, __version__{% if cookiecutter.config_file != 'none' %}, util{% endif %} logger = logging.getLogger('{{ cookiecutter.module_name }}') -app = typer.Typer( - name='{{ cookiecutter.module_name }}', - help="{{ cookiecutter.project_short_description }}" -) +app = typer.Typer(name='{{ cookiecutter.module_name }}') def version_callback(version: bool): @@ -25,8 +22,6 @@ def version_callback(version: bool): metavar='PATH', help="path to the program configuration" ) - - VersionOption = typer.Option( None, '-v', @@ -40,11 +35,13 @@ def version_callback(version: bool): @app.command() def main(config_file: str = ConfigOption, version: bool = VersionOption): """ - This is the entry point of your command line application. The values of the CLI params that - are passed to this application will show up als parameters to this function. + {{ cookiecutter.project_short_description }} + Note: This is the entry point of your command line application. The values of the CLI params + that are passed to this application will show up als parameters to this function. This docstring is where you describe what your command line application does. - Try running `python -m {{ cookiecutter.module_name }} --help` to see how this shows up in the command line. + Try running `python -m {{ cookiecutter.module_name }} --help` to see how this shows up in the + command line. """ {% if cookiecutter.config_file != 'none' %}config = util.load_config(config_file) util.logging_setup(config){% endif %} diff --git a/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/util__yaml.py b/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/util__yaml.py index 78b62e8..5bf8e33 100644 --- a/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/util__yaml.py +++ b/{{cookiecutter.project_slug}}/src/{{cookiecutter.module_name}}/util__yaml.py @@ -28,7 +28,7 @@ def load_config(config_file: Union[str, Path]) -> Dict[str, Any]: :param config_file: path of the config file to load :return: the parsed config as dictionary """ - with open(config_file, 'r') as fp: + with open(config_file) as fp: return yaml.safe_load(fp) diff --git a/{{cookiecutter.project_slug}}/tests/test_example.py b/{{cookiecutter.project_slug}}/tests/test_example.py index e6ae4d9..6ef09e2 100644 --- a/{{cookiecutter.project_slug}}/tests/test_example.py +++ b/{{cookiecutter.project_slug}}/tests/test_example.py @@ -1,3 +1,2 @@ - def test_the_universe(): assert 6 * 7 == 42