From 6390e833ee8802952a26f7724dc4c1248775c72b Mon Sep 17 00:00:00 2001 From: Gabriel Fontes Date: Sat, 31 Aug 2024 12:59:47 -0300 Subject: [PATCH] feat(templates): python template --- templates/python/.gitignore | 6 ++++ templates/python/README.md | 51 ++++++++++++++++++++++++++++ templates/python/flake.nix | 32 +++++++++++++++++ templates/python/foo_bar/__init__.py | 2 ++ templates/python/foo_bar/__main__.py | 9 +++++ templates/python/pyproject.toml | 31 +++++++++++++++++ templates/python/tests/example.py | 5 +++ 7 files changed, 136 insertions(+) create mode 100644 templates/python/.gitignore create mode 100644 templates/python/README.md create mode 100644 templates/python/flake.nix create mode 100644 templates/python/foo_bar/__init__.py create mode 100644 templates/python/foo_bar/__main__.py create mode 100644 templates/python/pyproject.toml create mode 100644 templates/python/tests/example.py diff --git a/templates/python/.gitignore b/templates/python/.gitignore new file mode 100644 index 00000000..efae3d74 --- /dev/null +++ b/templates/python/.gitignore @@ -0,0 +1,6 @@ +.venv/ +build/ +*.egg-info/ +venv/ +__pycache__/ +*.py[cod] diff --git a/templates/python/README.md b/templates/python/README.md new file mode 100644 index 00000000..4d4a6b46 --- /dev/null +++ b/templates/python/README.md @@ -0,0 +1,51 @@ +# Foo Bar + +TODO + +## Installation + +You can install the package using `pipx` +```bash +pipx install git+https://github.com/misterio77/foo-bar +``` + +Alternatively, use pip with `--user`: +```bash +pip install --user git+https://github.com/misterio77/foo-bar +``` + +Or use nix: +```bash +nix shell github:misterio77/foo-bar +``` + +## Usage + +TODO + +## Hacking + +Use [poetry](https://python-poetry.org/), like so: + +```bash +poetry install +poetry shell +``` + +Or use nix: +```bash +nix develop -c $SHELL +``` + +You can then use `python -m` to run it, as well as all the usual dev tools: + +```bash +python -m foo_bar # Run + +mypy # Type check +ruff check # Lint +ruff format # Format +pytest # Run tests +``` + +Python LSP will also be available. Check your editor docs on how to enable it. diff --git a/templates/python/flake.nix b/templates/python/flake.nix new file mode 100644 index 00000000..5d807875 --- /dev/null +++ b/templates/python/flake.nix @@ -0,0 +1,32 @@ +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixos-24.05"; + systems.url = "github:nix-systems/default"; + poetry2nix.url = "github:nix-community/poetry2nix"; + poetry2nix.inputs.nixpkgs.follows = "nixpkgs"; + }; + + outputs = { + nixpkgs, + systems, + poetry2nix, + ... + }: let + forEachSystem = nixpkgs.lib.genAttrs (import systems); + in { + packages = forEachSystem (system: let + pkgs = nixpkgs.legacyPackages.${system}; + p2nix = poetry2nix.lib.mkPoetry2Nix {inherit pkgs;}; + in { + default = p2nix.mkPoetryApplication { + projectDir = ./.; + preferWheels = true; + checkPhase = '' + mypy + ruff check + pytest + ''; + }; + }); + }; +} diff --git a/templates/python/foo_bar/__init__.py b/templates/python/foo_bar/__init__.py new file mode 100644 index 00000000..c5b16b98 --- /dev/null +++ b/templates/python/foo_bar/__init__.py @@ -0,0 +1,2 @@ +def hello_world() -> str: + return "Hello, world!" diff --git a/templates/python/foo_bar/__main__.py b/templates/python/foo_bar/__main__.py new file mode 100644 index 00000000..9813448e --- /dev/null +++ b/templates/python/foo_bar/__main__.py @@ -0,0 +1,9 @@ +from foo_bar import hello_world + + +def main() -> None: + print(hello_world()) + + +if __name__ == "__main__": + main() diff --git a/templates/python/pyproject.toml b/templates/python/pyproject.toml new file mode 100644 index 00000000..29b832c0 --- /dev/null +++ b/templates/python/pyproject.toml @@ -0,0 +1,31 @@ +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" + +[tool.poetry] +name = "foo-bar" +version = "0.1.0" +authors = [] +description = "" + +[tool.poetry.dependencies] +python = "^3.12" + +[tool.poetry.group.dev.dependencies] +pytest = "^8.3" +ruff = "0.6.1" +mypy = "^1.11" +python-lsp-server = "^1.12" +pylsp-mypy = "^0.6" + +[tool.poetry.scripts] +foo-bar = "foo_bar.__main__:main" + +[tool.ruff] +line-length = 88 +[tool.mypy] +files = "." +strict = true +[tool.pytest.ini_options] +testpaths = ["tests/*"] +pythonpath = ["."] diff --git a/templates/python/tests/example.py b/templates/python/tests/example.py new file mode 100644 index 00000000..e4a0cbf5 --- /dev/null +++ b/templates/python/tests/example.py @@ -0,0 +1,5 @@ +from foo_bar import hello_world + + +def test_hello_world() -> None: + assert hello_world() == "Hello, world!"