Skip to content

Commit 209a041

Browse files
authored
Switch to poetry. (#12)
* Switch to poetry. * Fix env and pytest call. * Call flake8 and black with poetry. * Run flake8 just on project files. * Windows compatible. * Remove venv setup. * Update pre-commit hooks. * Testing hooks. * Test hooks. * Test hooks. * Update prehooks. * Format. * Update docs. * Remove hatchling toml.
1 parent 7a32ac3 commit 209a041

18 files changed

+489
-95
lines changed

.flake8

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[flake8]
2-
exclude = .git,build
2+
exclude = .git, __pycache__, build, dist
33
ignore = E203, E266, E501, W503, F403, F401, C901
44
max-line-length = 100
55
max-complexity = 18

.github/workflows/poetry.yml

+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Poetry GitHub Action: https://github.com/marketplace/actions/python-poetry-action
2+
name: pydevtips
3+
4+
# on: [push, pull_request]
5+
on:
6+
# trigger on pushes and PRs to main
7+
push:
8+
branches:
9+
- main
10+
pull_request:
11+
branches:
12+
- main
13+
14+
jobs:
15+
build:
16+
17+
runs-on: ${{ matrix.os }}
18+
strategy:
19+
fail-fast: false
20+
max-parallel: 12
21+
matrix:
22+
os: [ubuntu-latest, macos-latest, windows-latest]
23+
python-version: ["3.10", "3.11"]
24+
poetry-version: ["1.8.4"]
25+
steps:
26+
- uses: actions/checkout@v4
27+
- name: Checkout submodules
28+
shell: bash
29+
run: |
30+
auth_header="$(git config --local --get http.https://github.com/.extraheader)"
31+
git submodule sync --recursive
32+
git -c "http.extraheader=$auth_header" -c protocol.version=2 submodule update --init --force --recursive --depth=1
33+
- name: Set up Python ${{ matrix.python-version }}
34+
uses: actions/setup-python@v4
35+
with:
36+
python-version: ${{ matrix.python-version }}
37+
- name: Install poetry
38+
uses: abatilo/[email protected]
39+
with:
40+
poetry-version: ${{ matrix.poetry-version }}
41+
- name: Install dependencies
42+
run: |
43+
poetry install --with dev
44+
- name: Lint with flake8
45+
run: |
46+
# stop the build if there are Python syntax errors or undefined names
47+
poetry run flake8 pydevtips --count --select=B,C,E,F,W,T4,B9 --show-source --statistics --max-complexity=18 --max-line-length=100 --ignore=E203,E266,E501,W503,F403,F401,C901
48+
poetry run flake8 examples --count --select=B,C,E,F,W,T4,B9 --show-source --statistics --max-complexity=18 --max-line-length=100 --ignore=E203,E266,E501,W503,F403,F401,C901
49+
poetry run flake8 profile --count --select=B,C,E,F,W,T4,B9 --show-source --statistics --max-complexity=18 --max-line-length=100 --ignore=E203,E266,E501,W503,F403,F401,C901
50+
poetry run flake8 tests --count --select=B,C,E,F,W,T4,B9 --show-source --statistics --max-complexity=18 --max-line-length=100 --ignore=E203,E266,E501,W503,F403,F401,C901
51+
- name: Format with black
52+
run: |
53+
poetry run black pydevtips -l 100
54+
poetry run black examples -l 100
55+
poetry run black profile -l 100
56+
poetry run black tests -l 100
57+
- name: Test with pytest
58+
run: poetry run pytest -v

.github/workflows/python.yml renamed to .github/workflows/setuptools.yml

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,11 @@
1-
name: pydevtips
1+
name: pydevtips setuptools
22

3-
# on: [push, pull_request]
3+
# HACK to not trigger GitHub action
44
on:
5-
# trigger on pushes and PRs to main
65
push:
76
branches:
8-
- main
9-
pull_request:
10-
branches:
11-
- main
7+
- non-existant-branch
8+
129

1310
jobs:
1411
build:

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
project_env/
22
.DS_Store
3+
dataset/
34

45
# from hydra
56
outputs/
@@ -13,6 +14,7 @@ __pycache__/
1314
*.so
1415

1516
# Distribution / packaging
17+
poetry.lock
1618
.Python
1719
build/
1820
develop-eggs/

.pre-commit-config.yaml

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
11
repos:
2+
- repo: https://github.com/python-poetry/poetry
3+
rev: '1.8.4' # add version here
4+
hooks:
5+
- id: poetry-check
6+
- id: poetry-lock
7+
- id: poetry-export
8+
- id: poetry-install
29
- repo: https://github.com/psf/black
310
rev: 22.12.0
411
hooks:
@@ -7,4 +14,9 @@ repos:
714
- repo: https://github.com/PyCQA/flake8
815
rev: 6.0.0
916
hooks:
10-
- id: flake8
17+
- id: flake8
18+
- repo: https://github.com/pycqa/isort
19+
rev: 5.13.2
20+
hooks:
21+
- id: isort
22+
name: isort (python)

setup.py renamed to OLD_setup.py

File renamed without changes.

README.rst

+13-12
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ pydevtips: Python Development Tips
2020
:alt: Slides
2121

2222

23-
.. |ss| raw:: html
23+
.. .. |ss| raw:: html
2424
25-
<strike>
25+
.. <strike>
2626
27-
.. |se| raw:: html
27+
.. .. |se| raw:: html
2828
29-
</strike>
29+
.. </strike>
3030
3131
3232
Reproducibility is important for software: *if it's not reproducible,
@@ -54,13 +54,13 @@ But hopefully this gives you a starting point. Feel free to pick and
5454
choose the features that you like. This flexibility is one of the best
5555
(and worst parts) of open source. Some of the things we cover:
5656

57+
* Packaging and distribution with `Poetry <https://python-poetry.org/>`_.
5758
* Virtual environments.
5859
* Version control.
5960
* Reproducible examples.
6061
* Documentation.
6162
* Code formatting.
6263
* Unit tests and continuous integration.
63-
* Packaging and distribution.
6464
* Remove development.
6565

6666
The accompanying
@@ -75,7 +75,7 @@ Feel free to modify and use it for your own purposes.
7575
Copilot <https://github.com/features/copilot>`_, which I highly recommend for development. If you don't like
7676
writing documentation, it is a great way to get started as it is able to
7777
understand the functionality of your code and produce meaningful text to describe it.
78-
It should be used be used with caution, |ss| *but it can be a great tool for getting started* |se|
78+
It should be used be used with caution, *but it can be a great tool for getting started*
7979
and you often you need to make a few tweaks (*like the previous repetition*).
8080
But it's a huge time-saver!
8181

@@ -93,6 +93,8 @@ Or from source, e.g. with Anaconda / Miniconda:
9393
.. code:: bash
9494
9595
# create new environment, press enter to accept
96+
# -- important to set python version, otherwise `python` executable may not exist
97+
# -- (would be `python3` instead)
9698
conda create -n project_env python=3.11
9799
98100
# view available environments
@@ -102,11 +104,10 @@ Or from source, e.g. with Anaconda / Miniconda:
102104
conda activate project_env
103105
104106
# install package locally
105-
(project_env) pip install -e .
107+
(project_env) poetry install --with dev
106108
107109
# run tests
108-
# - one time: pip install pytest
109-
(project_env) pytest
110+
(project_env) poetry run pytest
110111
111112
# deactivate environment
112113
(project_env) conda deactivate
@@ -123,19 +124,19 @@ repository, e.g.:
123124
python examples/real_convolve.py
124125
125126
Parameter setting is done with `hydra <https://hydra.cc/>`_. More on that
126-
in the :ref:`Reproducible examples<Reproducible examples>` section of the
127-
documentation.
127+
in the `Reproducible examples <https://pydevtips.readthedocs.io/en/latest/reproducible.html>`_
128+
section of the documentation.
128129

129130

130131
TODO
131132
====
132133

134+
- switch to ruff for code formatting: https://docs.astral.sh/ruff/
133135
- numba: https://numba.pydata.org/
134136
- picking a license
135137
- change documentation links to main branch
136138
- github page
137139
- point out features in scripts: object-oriented, asserts, tqdm, type hints
138-
- matplotlib, pytest, black in dev install
139140
- manifest file to not include file in package
140141
- GitHub actions for releasing to PyPi when changes to version
141142
- pytorch compatible

docs/source/clean_code.rst

+15-4
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,16 @@ tools that can help us with this task. The ones we use are:
1616

1717
* `Black <https://github.com/psf/black>`_ which will reformat your code
1818
in-place to conform to the PEP8 standard.
19-
* `Flake8 <https://flake8.pycqa.org/en/latest/>`__ which is a *linter* that
19+
* `Flake8 <https://flake8.pycqa.org/en/latest/>`_ which is a *linter* that
2020
will check your code for errors and style violations, but not reformat it. For
2121
example, for me it has identified code where I have unused variables or
2222
scripts / functions that are too long.
23+
* `isort <https://pycqa.github.io/isort/>`_ which will sort your imports
24+
alphabetically and group them by type.
25+
26+
There are many alternatives for there tools. An increasingly popular alternative
27+
is `ruff <https://docs.astral.sh/ruff//>`_, which is written in Rust and is meant
28+
to replace Flake8, Black, and isort.
2329

2430
While you can use these tools manually, it is much more convenient to use them
2531
as pre-commit hooks. This means that before you commit your code, these tools
@@ -34,7 +40,7 @@ A few files are needed to setup pre-commit hooks:
3440
pre-commit hooks. It specifies which tools to use, and how to use them.
3541
* `.flake8 <https://github.com/ebezzam/python-dev-tips/blob/main/.flake8>`_: This file contains the configuration for Flake8. It specifies
3642
e.g. which errors to ignore, and which line length to use.
37-
* `pyproject.toml <https://github.com/ebezzam/python-dev-tips/blob/main/pyproject.toml>`_: This file contains the configuration for Black. It
43+
* `pyproject.toml <https://github.com/ebezzam/python-dev-tips/blob/main/pyproject.toml>`_: This file contains the configuration for Black and isort. It
3844
specifies e.g. which line length to use.
3945

4046
You can then install the pre-commit hooks for your project by running the
@@ -43,13 +49,18 @@ following commands:
4349
.. code:: bash
4450
4551
# inside virtual environment
46-
(project_env) pip install pre-commit
47-
(project_env) pip install black
52+
# -- black, flake8, isort are in the dev group
53+
(project_env) poetry install --with dev
54+
55+
# -- if not using Poetry
56+
# (project_env) pip install pre-commit black flake8 isort
4857
4958
# Install git hooks
5059
(project_env) pre-commit install
5160
# pre-commit installed at .git/hooks/pre-commit
5261
62+
More pre-commit hooks are available provided by `Poetry <https://python-poetry.org/docs/pre-commit-hooks/>`_.
63+
5364

5465
Avoiding long ``if-else`` statements with object instantiation
5566
--------------------------------------------------------------

docs/source/conf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
# sys.path.insert(0, os.path.abspath('.'))
1616

1717
import datetime
18+
import os
1819
import sys
1920
from unittest import mock
20-
import os
2121

2222
sys.path.insert(0, os.path.abspath(os.path.join("..", "..")))
2323

@@ -55,7 +55,7 @@
5555
intersphinx_mapping = {
5656
"python": ("https://docs.python.org/3/", None),
5757
"NumPy [latest]": ("https://docs.scipy.org/doc/numpy/", None),
58-
"matplotlib": ("http://matplotlib.sourceforge.net/", None),
58+
"matplotlib [stable]": ("https://matplotlib.org/stable/", None),
5959
}
6060
intersphinx_disabled_domains = ["std"]
6161

docs/source/index.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ Contents
1515

1616
.. toctree::
1717

18+
packaging
1819
virtual_env
1920
version_control
2021
reproducible
2122
documentation
2223
clean_code
2324
testing
24-
packaging
2525
remote_development
2626
badges
2727

0 commit comments

Comments
 (0)