Skip to content

ldom1/python-template

Repository files navigation

Python template

This is a b template for Python projects develop to facilitate the creation of new projects following the best practices in the Python community.

This template can be used as a starting point for new projects.

Table of contents

I - Features

FAST API

This template uses FAST API to create the API. It is a modern, fast (high-performance), web framework for building APIs with Python 3.6+ based on standard Python type hints.

Docker

This template uses Docker to containerize the application. Docker is a set of platform as a service products that use OS-level virtualization to deliver software in packages called containers.

Poetry

This template uses Poetry to manage the dependencies. Poetry is a tool for dependency management and packaging in Python. It allows you to declare the libraries your project depends on and it will manage (install/update) them for you.

MongoDB

This template uses MongoDB as database. MongoDB is a general purpose, document-based, distributed database built for modern application developers and for the cloud era.

Amazon S3

This template uses Amazon S3 to store the files. Amazon S3 or Amazon Simple Storage Service is a service offered by Amazon Web Services that provides object storage through a web service interface.

Sphinx

This template uses Sphinx to generate the documentation. Sphinx is a tool that makes it easy to create intelligent and beautiful documentation, written by Georg Brandl and licensed under the BSD license.

Testing

Pytest

This template uses Pytest to test the code. The pytest framework makes it easy to write small tests, yet scales to support complex functional testing for applications and libraries.

We use also pytest-cov to generate the coverage report.

Docker in Docker for integration testing

This template uses Docker in Docker to run the integration tests. Docker can now run within Docker. This is a feature that allows you to run Docker containers as part of your Docker build CI/CD pipeline. See section Github Actions for more details.

Indeed, if you run the tests/integration, we need to have the FastAPI server running. We can use Docker to run the FastAPI server in a container and then run the tests in another container, the github action runner.

You can see the configuration in the file .github/workflows/git_ci.yml.

Pre-commit

This template uses Pre-commit to run some checks before commiting the code. pre-commit is a framework for managing and maintaining multi-language pre-commit hooks. The pre-commit hooks are run in the order given, and if any of them fail, the commit is aborted.

Github Actions

This template uses Github Actions to automate the workflow. GitHub Actions makes it easy to automate all your software workflows, now with world-class CI/CD. Build, test, and deploy your code right from GitHub. Make code reviews, branch management, and issue triaging work the way you want.

Logging

This template uses Loguru to log the messages. Loguru is a library which aims to bring enjoyable logging in Python. By adding a simple and powerful configuration, it allows to have a more human-friendly logging.

Linting & Formatting

Flake8

This template uses Flake8 to lint the code. Flake8 is a wrapper around these tools:

Black

This template uses Black to format the code. Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.

Isort

This template uses Isort to sort the imports. isort is a Python utility / library to sort imports alphabetically, and automatically separated into sections and by type. It provides a command line utility, Python library and plugins for various editors to quickly sort all your imports.

Pylint

This template uses Pylint to lint the code. Pylint is a tool that checks for errors in Python code, tries to enforce a coding standard and looks for code smells.

II - How to use

Requirements

  • Python 3.9+
  • Poetry 1.6.1+
  • Pyenv 2.3.0+

Installation

  1. Clone this repository
git clone https://github.com/ldom1/python-template.git
  1. Create a new repository in github

  2. Change the remote url to the new repository

git remote set-url origin
  1. Change the name of the project in the following files:
  • README.md
  • pyproject.toml
  • Makefile
  • python_template/config.py
  1. Install the dependencies and create the virtual environment

NB: I prefer to create my virtual environment in the project folder. If you prefer to create it in another folder, you can ignore this step and let poetry create the virtual environment in the default folder.

poetry config virtualenvs.in-project true

Then run the following commands:

pyenv install 3.10.11
pyenv shell 3.10.11
poetry env use python
source .venv/bin/activate
poetry install
pre-commit install
  1. Start coding!

III - References

This template is partely based on the following articles:

NB: Congratulations to the authors of the articles above for the quality of their work.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published