@ -0,0 +1,104 @@ | |||||
# Python Start Project with Cookiecutter | |||||
This template allows you to easily setup new Python projects with the most important libraries and configuration files for managing dependencies and virtualenv, code formatting, debugging, testing, static code analysis and creating command-line interfaces. | |||||
This project is based on [python-new-project](https://github.com/stribny/python-new-project), with a heavy amount of changes | |||||
## What is included in the template | |||||
The template will: | |||||
- install [Pytest](https://docs.pytest.org/en/stable/) and [Pytest-sugar](https://pypi.org/project/pytest-sugar/) for tests | |||||
- install [pydantic](https://github.com/samuelcolvin/pydantic) for data validation | |||||
- install [Black](https://black.readthedocs.io/en/stable/) and [Flake8](https://flake8.pycqa.org/en/latest/) for standardization | |||||
- install [pre-commit](https://pre-commit.com/) to run Black, Flake8, etc before every commit | |||||
- install [sphinx](https://pypi.org/project/Sphinx/) and [sphinx-autobuild](https://pypi.org/project/sphinx-autobuild/) for documentation | |||||
- create `pyproject.toml` configuration file for managing project dependencies and virtual environments using [Poetry](https://python-poetry.org/) | |||||
- create `README.md` README file | |||||
- create `.gitignore` file | |||||
- initialize new Git repository | |||||
## How to use the template | |||||
This is a [cookiecutter](https://cookiecutter.readthedocs.io) template. | |||||
To use it, make sure you have at least Python 3.7 and git installed. | |||||
Then install `cookiecutter` and `poetry` via `pip`: | |||||
``` | |||||
pip install poetry --user | |||||
pip install cookiecutter --user | |||||
``` | |||||
You can now create a new Python project by running: | |||||
``` | |||||
cookiecutter <git-url> | |||||
``` | |||||
This will take you to a project setup asking for: | |||||
- `package_display_name` - The name that will be used in README, documentation, etc | |||||
- `package_name` - The Python package name and name of the project folder (will generate automatically) | |||||
- `package_description` - The Python package description | |||||
- `package_command` - This will configure poetry to allow calling the package, `package_name/console:entry_point`, via this command. Generally, this would be the same value as `package_name` | |||||
- `author_name` - The author of the package | |||||
- `author_email` - The email for the author | |||||
- `github_repo_name` - For documentation, such as URLs to the issue tracker | |||||
- `github_user_name` - For documentation, such as URLs to the issue tracker | |||||
- `python_version`- The Python version that should be defined in `pyproject.toml` | |||||
The generated structure looks like this: | |||||
``` | |||||
└── package_name/ | |||||
├── docs | |||||
│ ├── build | |||||
│ ├── make.bat | |||||
│ ├── Makefile | |||||
│ └── source | |||||
│ ├── conf.py | |||||
│ ├── dev | |||||
│ │ └── index.rst | |||||
│ ├── index.rst | |||||
│ └── user | |||||
│ └── index.rst | |||||
├── poetry.lock | |||||
├── pyproject.toml | |||||
├── README.md | |||||
├── src | |||||
│ └── package_name | |||||
│ ├── console.py | |||||
│ ├── __init__.py | |||||
│ └── logger.py | |||||
└── tests/ | |||||
├── __init__.py | |||||
└── test_project.py | |||||
``` | |||||
From here, you can install the current dependencies, if needed running `poetry install` from within `package_name`. We can run `poetry shell` and `package_name` to run the script. | |||||
## Default Configs | |||||
The project includes [pydantic](https://github.com/samuelcolvin/pydantic) to help with configuration and settings for projects. | |||||
The provided example `console.py` allows for the following object: | |||||
``` | |||||
>>> from package_name.console import Config | |||||
>>> config = Config() | |||||
>>> config | |||||
Config(api_key='EXAMPLEKEY', more_settings=SubModel(foo='bar')) | |||||
>>> config.more_settings.foo | |||||
'bar' | |||||
``` | |||||
The config settings can be defined either through environment variables, such as the following: | |||||
``` | |||||
API_KEY='MY_ACTUAL_KEY' | |||||
MORE_SETTINGS__FOO='OTHER' | |||||
``` |
@ -0,0 +1,16 @@ | |||||
{ | |||||
"package_display_name": "Package Name", | |||||
"package_name": "package_name", | |||||
"package_description": "Package description", | |||||
"package_command": "package_name", | |||||
"author_email": "author@website.com", | |||||
"author_name": "Project Author", | |||||
"github_repo_user": "", | |||||
"github_repo_name": "", | |||||
"python_version": "^3.7", | |||||
"_extensions": ["jinja2_time.TimeExtension"] | |||||
} |
@ -0,0 +1,2 @@ | |||||
#!/bin/bash | |||||
poetry install && git init && poetry run pre-commit install |
@ -0,0 +1,2 @@ | |||||
poetry | |||||
cookiecutter |
@ -0,0 +1,5 @@ | |||||
[flake8] | |||||
ignore = E203, E266, E501, W503, F403, F401 | |||||
max-line-length = 120 | |||||
max-complexity = 18 | |||||
select = B,C,E,F,W,T4,B9 |
@ -0,0 +1,120 @@ | |||||
# Swap files | |||||
*.sw[a-p] | |||||
# Byte-compiled / optimized / DLL files | |||||
__pycache__/ | |||||
*.py[cod] | |||||
*$py.class | |||||
# C extensions | |||||
*.so | |||||
# Distribution / packaging | |||||
.Python | |||||
build/ | |||||
develop-eggs/ | |||||
dist/ | |||||
downloads/ | |||||
eggs/ | |||||
.eggs/ | |||||
lib/ | |||||
lib64/ | |||||
parts/ | |||||
sdist/ | |||||
var/ | |||||
wheels/ | |||||
*.egg-info/ | |||||
.installed.cfg | |||||
*.egg | |||||
MANIFEST | |||||
poetry.lock | |||||
# PyInstaller | |||||
# Usually these files are written by a python script from a template | |||||
# before PyInstaller builds the exe, so as to inject date/other infos into it. | |||||
*.manifest | |||||
*.spec | |||||
# Installer logs | |||||
pip-log.txt | |||||
pip-delete-this-directory.txt | |||||
# Unit test / coverage reports | |||||
htmlcov/ | |||||
.tox/ | |||||
.nox/ | |||||
.coverage | |||||
.coverage.* | |||||
.cache | |||||
nosetests.xml | |||||
coverage.xml | |||||
*.cover | |||||
.hypothesis/ | |||||
.pytest_cache/ | |||||
.mypy_cache/ | |||||
# Translations | |||||
*.mo | |||||
*.pot | |||||
# Django stuff: | |||||
*.log | |||||
local_settings.py | |||||
db.sqlite3 | |||||
# Flask stuff: | |||||
instance/ | |||||
.webassets-cache | |||||
# Scrapy stuff: | |||||
.scrapy | |||||
# Sphinx documentation | |||||
docs/_build/ | |||||
# PyBuilder | |||||
target/ | |||||
# Jupyter Notebook | |||||
.ipynb_checkpoints | |||||
# IPython | |||||
profile_default/ | |||||
ipython_config.py | |||||
# pyenv | |||||
.python-version | |||||
# celery beat schedule file | |||||
celerybeat-schedule | |||||
# SageMath parsed files | |||||
*.sage.py | |||||
# Environments | |||||
.env | |||||
.secrets | |||||
.venv | |||||
env/ | |||||
venv/ | |||||
ENV/ | |||||
env.bak/ | |||||
venv.bak/ | |||||
# Spyder project settings | |||||
.spyderproject | |||||
.spyproject | |||||
# Rope project settings | |||||
.ropeproject | |||||
# mkdocs documentation | |||||
/site | |||||
# mypy | |||||
.mypy_cache/ | |||||
.dmypy.json | |||||
dmypy.json | |||||
# Pyre type checker | |||||
.pyre/ |
@ -0,0 +1,9 @@ | |||||
repos: | |||||
- repo: https://github.com/psf/black | |||||
rev: "22.6.0" | |||||
hooks: | |||||
- id: black | |||||
- repo: https://gitlab.com/pycqa/flake8 | |||||
rev: "3.8.4" | |||||
hooks: | |||||
- id: flake8 |
@ -0,0 +1,15 @@ | |||||
# {{cookiecutter.package_display_name}} | |||||
{{cookiecutter.package_description}} | |||||
## Introduction | |||||
## Installation | |||||
## Configuration | |||||
## Usage |
@ -0,0 +1,20 @@ | |||||
# Minimal makefile for Sphinx documentation | |||||
# | |||||
# You can set these variables from the command line, and also | |||||
# from the environment for the first two. | |||||
SPHINXOPTS ?= | |||||
SPHINXBUILD ?= sphinx-build | |||||
SOURCEDIR = source | |||||
BUILDDIR = build | |||||
# Put it first so that "make" without argument is like "make help". | |||||
help: | |||||
@$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) | |||||
.PHONY: help Makefile | |||||
# Catch-all target: route all unknown targets to Sphinx using the new | |||||
# "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). | |||||
%: Makefile | |||||
@$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) |
@ -0,0 +1,35 @@ | |||||
@ECHO OFF | |||||
pushd %~dp0 | |||||
REM Command file for Sphinx documentation | |||||
if "%SPHINXBUILD%" == "" ( | |||||
set SPHINXBUILD=sphinx-build | |||||
) | |||||
set SOURCEDIR=source | |||||
set BUILDDIR=build | |||||
%SPHINXBUILD% >NUL 2>NUL | |||||
if errorlevel 9009 ( | |||||
echo. | |||||
echo.The 'sphinx-build' command was not found. Make sure you have Sphinx | |||||
echo.installed, then set the SPHINXBUILD environment variable to point | |||||
echo.to the full path of the 'sphinx-build' executable. Alternatively you | |||||
echo.may add the Sphinx directory to PATH. | |||||
echo. | |||||
echo.If you don't have Sphinx installed, grab it from | |||||
echo.https://www.sphinx-doc.org/ | |||||
exit /b 1 | |||||
) | |||||
if "%1" == "" goto help | |||||
%SPHINXBUILD% -M %1 %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% | |||||
goto end | |||||
:help | |||||
%SPHINXBUILD% -M help %SOURCEDIR% %BUILDDIR% %SPHINXOPTS% %O% | |||||
:end | |||||
popd |
@ -0,0 +1,103 @@ | |||||
# -- Path setup -------------------------------------------------------------- | |||||
# If extensions (or modules to document with autodoc) are in another directory, | |||||
# add these directories to sys.path here. If the directory is relative to the | |||||
# documentation root, use os.path.abspath to make it absolute, like shown here. | |||||
# | |||||
import os | |||||
import re | |||||
import sys | |||||
from datetime import datetime | |||||
from sphinx.ext import apidoc | |||||
# sys.path.insert(0, os.path.abspath('.')) | |||||
# Get the version from the pyproject.toml configuration | |||||
regexp = re.compile(r'.*version = [\'\"](.*?)[\'\"]', re.S) | |||||
repo_root = os.path.dirname(os.path.dirname(os.path.dirname(__file__))) | |||||
init_file = os.path.join(repo_root, 'pyproject.toml') | |||||
with open(init_file, 'r') as f: | |||||
module_content = f.read() | |||||
match = regexp.match(module_content) | |||||
if match: | |||||
version = match.group(1) | |||||
else: | |||||
raise RuntimeError( | |||||
'Cannot find version in {}'.format(init_file)) | |||||
# -- Project information ----------------------------------------------------- | |||||
project = '{{cookiecutter.package_display_name}}' | |||||
copyright = '{% now "utc", "%Y" %}, {{cookiecutter.author_name}}' | |||||
author = '{{cookiecutter.author_name}}' | |||||
release = version # The full version, including alpha/beta/rc tags. | |||||
version = version | |||||
# -- General configuration --------------------------------------------------- | |||||
# Add any Sphinx extension module names here, as strings. They can be | |||||
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom | |||||
# ones. | |||||
extensions = [ | |||||
'sphinx.ext.autodoc', | |||||
'sphinx.ext.viewcode', | |||||
] | |||||
# The suffix(es) of source filenames. | |||||
# You can specify multiple suffix as a list of string: | |||||
# | |||||
# source_suffix = ['.rst', '.md'] | |||||
source_suffix = '.rst' | |||||
# The master toctree document. | |||||
master_doc = 'index' | |||||
# Add any paths that contain templates here, relative to this directory. | |||||
templates_path = ['_templates'] | |||||
# List of patterns, relative to source directory, that match files and | |||||
# directories to ignore when looking for source files. | |||||
# This pattern also affects html_static_path and html_extra_path. | |||||
exclude_patterns = [] | |||||
# The name of the Pygments (syntax highlighting) style to use. | |||||
pygments_style = 'sphinx' | |||||
# If true, `todo` and `todoList` produce output, else they produce nothing. | |||||
todo_include_todos = False | |||||
# If true, links to the reST sources are added to the pages. | |||||
html_show_sourcelink = False | |||||
# If true, "(C) Copyright ..." is shown in the HTML footer. Default is True. | |||||
html_show_copyright = False | |||||
# Suppress nonlocal image warnings | |||||
suppress_warnings = ['image.nonlocal_uri'] | |||||
# -- Options for HTML output ------------------------------------------------- | |||||
# The theme to use for HTML and HTML Help pages. See the documentation for | |||||
# a list of builtin themes. | |||||
html_theme = 'alabaster' | |||||
# Theme options are theme-specific and customize the look and feel of a theme | |||||
# further. For a list of options available for each theme, see the | |||||
# documentation. | |||||
html_theme_options = { | |||||
'description': '{{cookiecutter.package_description}}', | |||||
'show_powered_by': False, | |||||
# 'logo': 'my-logo.png', | |||||
'logo_name': False, | |||||
'page_width': '80%', | |||||
} | |||||
# Add any paths that contain custom static files (such as style sheets) here, | |||||
# relative to this directory. They are copied after the builtin static files, | |||||
# so a file named "default.css" will overwrite the builtin "default.css". | |||||
html_static_path = ['_static'] |
@ -0,0 +1,84 @@ | |||||
Developers Guide | |||||
################ | |||||
.. _testing-label: | |||||
Testing | |||||
======= | |||||
The {{cookiecutter.package_display_name}} project implements a regression | |||||
test suite that improves developer productivity by identifying capability | |||||
regressions early. | |||||
Developers implementing fixes or enhancements must ensure that they have | |||||
not broken existing functionality. The {{cookiecutter.package_display_name}} | |||||
project provides some convenience tools so this testing step can be quickly | |||||
performed. | |||||
.. code-block:: console | |||||
({{cookiecutter.package_name}}) $ pytest -v | |||||
Individual unit tests can be run also. | |||||
.. code-block:: console | |||||
({{cookiecutter.package_name}}) $ pytest -v test_name | |||||
Code Style | |||||
========== | |||||
Adopting a consistent code style assists with maintenance. This project uses | |||||
Black to format code, flake8, and isort to sort imports | |||||
.. _annotations-label: | |||||
Type Annotations | |||||
---------------- | |||||
The code base contains type annotations to provide helpful type information | |||||
that can improve code maintenance. | |||||
.. _documentation-label: | |||||
Documentation | |||||
============= | |||||
To simplify testing and generation, give `sphinx-autobuild` a shot, which will build and start a server for testing: | |||||
.. code-block:: console | |||||
({{cookiecutter.package_name}}) $ sphinx-autobuild docs/source docs/build/html | |||||
In the future, we may integrate and automate doc autobuild as well. | |||||
.. _release-label: | |||||
Release Process | |||||
=============== | |||||
The following steps are used to make a new software release. | |||||
The steps assume they are executed from within a development virtual | |||||
environment. | |||||
- Check that the package version label in ``pyproject.toml`` is correct. | |||||
- Create and push a repo tag to Github. As a convention use the package | |||||
version number (e.g. YY.MM.MICRO) as the tag. | |||||
.. code-block:: console | |||||
$ git checkout master | |||||
$ git tag YY.MM.MICRO -m "A meaningful release tag comment" | |||||
$ git tag # check release tag is in list | |||||
$ git push --tags origin master | |||||
- This will trigger Github to create a release at: | |||||
:: | |||||
https://github.com/{username}/{{cookiecutter.package_name}}/releases/{tag} |
@ -0,0 +1,30 @@ | |||||
{{cookiecutter.package_display_name}} | |||||
{{cookiecutter.package_display_name|length * '#' }} | |||||
{{cookiecutter.package_description}} | |||||
.. toctree:: | |||||
:maxdepth: 2 | |||||
:numbered: | |||||
:hidden: | |||||
user/index | |||||
dev/index | |||||
Quick Start | |||||
=========== | |||||
{{cookiecutter.package_display_name}} is not yet available on PyPI. | |||||
.. code-block:: console | |||||
$ pip install . | |||||
After installing {{cookiecutter.package_display_name}} you can use it like any other Python module. | |||||
Here is a simple example: | |||||
.. code-block:: python | |||||
import {{cookiecutter.package_name}} |
@ -0,0 +1,47 @@ | |||||
User Guide | |||||
########## | |||||
This section of the documentation provides user focused information such as | |||||
installing and quickly using this package. | |||||
.. _install-guide-label: | |||||
Install Guide | |||||
============= | |||||
.. note:: | |||||
It is best practice to install Python projects in a virtual environment, | |||||
which can be created and activated as follows using Python 3.6+. | |||||
.. code-block:: console | |||||
$ python -m venv venv --prompt myvenv | |||||
$ source venv/bin/activate | |||||
(myvenv) $ | |||||
The simplest way to install {{cookiecutter.package_display_name}} is using Pip from the local directory's path. | |||||
.. code-block:: console | |||||
$ pip install . | |||||
This will install ``{{cookiecutter.package_name}}`` and all of its dependencies. | |||||
**Note:** This package is not yet on pypi | |||||
{% if cookiecutter.github_repo_user|length %} | |||||
.. _report-bugs-label: | |||||
Report Bugs | |||||
=========== | |||||
Report bugs at the `issue tracker <https://github.com/{{cookiecutter.github_repo_user}}/{{cookiecutter.github_repo_name}}/issues>`_. | |||||
Please include: | |||||
- Operating system name and version. | |||||
- Any details about your local setup that might be helpful in troubleshooting. | |||||
- Detailed steps to reproduce the bug. | |||||
{% endif %} |
@ -0,0 +1,36 @@ | |||||
[tool.poetry] | |||||
name = "{{cookiecutter.package_name}}" | |||||
version = "0.0.1" | |||||
description = "{{cookiecutter.package_description}}" | |||||
authors = ["{{cookiecutter.author_name}} <{{cookiecutter.author_email}}>"] | |||||
#license = "GPLv3" | |||||
readme = "README.md" | |||||
[tool.poetry.dependencies] | |||||
python = "{{cookiecutter.python_version}}" | |||||
pydantic = "*" | |||||
python-dotenv = "*" | |||||
[tool.poetry.dev-dependencies] | |||||
black = "==22.6" | |||||
flake8 = "==3.8.4" | |||||
pre-commit = "*" | |||||
pytest = "*" | |||||
pytest-sugar = "*" | |||||
sphinx = "*" | |||||
sphinx-autobuild = "*" | |||||
[tool.poetry.scripts] | |||||
{{cookiecutter.package_command}} = "{{cookiecutter.package_name}}.console:entry_point" | |||||
[tool.semantic_release] | |||||
version_variable = "pyproject.toml:version" | |||||
branch = "master" | |||||
build_command = "pip install poetry && poetry build" | |||||
dist_path = "dist/" | |||||
upload_to_pypi = false | |||||
remove_dist = false | |||||
[build-system] | |||||
requires = ["poetry-core>=1.0.0"] | |||||
build-backend = "poetry.core.masonry.api" |
@ -0,0 +1,7 @@ | |||||
from importlib.metadata import version | |||||
from .logger import create_logger | |||||
version = version("{{cookiecutter.package_name}}") | |||||
logger = create_logger(__package__) |
@ -0,0 +1,26 @@ | |||||
from pydantic import ( | |||||
BaseModel, | |||||
BaseSettings, | |||||
) | |||||
class SubModel(BaseModel): | |||||
foo: str = "bar" | |||||
class Config(BaseSettings): | |||||
api_key: str = "EXAMPLEKEY" | |||||
more_settings: SubModel = SubModel() | |||||
class Config: | |||||
env_file = ".env" | |||||
env_file_encoding = "utf-8" | |||||
env_nested_delimiter = "__" | |||||
def entry_point(): | |||||
config = Config() | |||||
if __name__ == "__main__": | |||||
entry_point() |
@ -0,0 +1,6 @@ | |||||
import logging | |||||
def create_logger(logger_name: str) -> logging: | |||||
logger = logging.getLogger(logger_name) | |||||
return logger |
@ -0,0 +1,5 @@ | |||||
from {{cookiecutter.package_name}} import version | |||||
def test_version(): | |||||
assert version == '0.0.1' |