Skip to content

Commit

Permalink
Add Poetry example
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkun committed Jul 4, 2024
1 parent 5fa84e9 commit 57204b1
Show file tree
Hide file tree
Showing 11 changed files with 476 additions and 15 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/poetry.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Poetry workflow
on:
push:
paths:
- 'poetry-demo/**'
defaults:
run:
working-directory: ./poetry-demo

jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.12'
- name: Install Poetry
uses: snok/install-poetry@v1
- name: Lint
run: poetry run ruff check
- name: Run tests
run: poetry run pytest
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# Comparing Hatch, Poetry, and Rye

* [hatch-demo](./hatch-demo/README.md)
* [poetry-demo](./poetry-demo/README.md)
27 changes: 12 additions & 15 deletions hatch-demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

## Steps

1. Install `hatch`

1. Install `hatch`:
```bash
$ pipx install hatch
# or
$ brew install pipx
$ brew install hatch
```

2. Create new project
2. Create new project:
```bash
$ hatch new "Hatch Demo"
$ cd hatch-demo
```

3. Create default environment
3. Create default environment:
```bash
$ hatch env create
```
Expand Down Expand Up @@ -47,23 +46,22 @@ dependencies = [
```
_Note: You have to reference `hatch-demo` in order to get the project deps needed for running tests._

6. Add `test` and `lint` scripts to `dev` env
6. Add `test` and `lint` scripts to `dev` env:

```toml
[tool.hatch.envs.dev.scripts]
test = "pytest"
lint = "ruff check --fix && ruff format"
```

7. Create/install the env

7. Create/install the env:
```bash
$ hatch env create dev
```

8. Add code (`demo.py`) and tests (`test_demo.py`)
8. Add code (`demo.py`) and tests (`test_demo.py`).

9. Check envs
9. Check envs:

```bash
$ hatch env show
Expand All @@ -80,26 +78,25 @@ $ hatch env show
└─────────┴─────────┴──────────────┴─────────┘
```

10. Run tests and lint/format code

10. Run tests and lint/format code:
```bash
$ hatch run dev:test
$ hatch run dev:lint
```

11. Add default script to run the app
11. Add default script to run the app:

```toml
[tool.hatch.envs.default.scripts]
run = "python src/hatch_demo/demo.py"
```

12. Run the app
12. Run the app:
```bash
$ hatch run default:run
```

13. Add [hatch-based GH workflow](../.github/workflows/hatch.yml) to check linting and run tests
13. Add [hatch-based GH workflow](../.github/workflows/hatch.yml) to check linting and run tests.

## Notes

Expand Down
77 changes: 77 additions & 0 deletions poetry-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Poetry Demo

## Steps

1. Install `poetry`:

```bash
$ pipx install poetry
# or
$ brew install poetry
```

2. Create new project (with [src-layout](https://packaging.python.org/en/latest/discussions/src-layout-vs-flat-layout/)):
```bash
$ poetry new --src poetry-demo
```

3. Add dependencies (`requests`, `polars`, `pytest`, `ruff`)
* Note: Add `pytest` and `ruff` under the `dev` group.

```bash
$ poetry add requests
$ poetry add polars
$ poetry add pytest --group dev
$ poetry add ruff --group dev
```

Those commands added the following sections to our initial `pyproject.toml` file, and created/updated a `poetry.lock` file.

```toml
[tool.poetry.dependencies]
python = "^3.12"
requests = "^2.32.3"
polars = "^1.0.0"

[tool.poetry.group.dev.dependencies]
pytest = "^8.2.2"
ruff = "^0.5.0"
```

4. Install the project:
```bash
$ poetry install
```

5. Add code (`demo.py`) and tests (`test_demo.py`).

6. Run tests and lint/format code:
```bash
$ poetry run pytest
$ poetry run ruff check --fix
$ poetry run ruff format
```

7. Add entrypoint script to run the app:
```toml
[tool.poetry.scripts]
rundemo = 'poetry_demo.demo:main'
```

8. Run the app:
```bash
$ rundemo

shape: (3, 5)
┌────────────┬──────────────┬─────────────┬──────────────┬─────────────┐
│ species ┆ sepal_length ┆ sepal_width ┆ petal_length ┆ petal_width │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ str ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
╞════════════╪══════════════╪═════════════╪══════════════╪═════════════╡
│ setosa ┆ 116.9 ┆ 81.7 ┆ 33.2 ┆ 6.1 │
│ virginica ┆ 324.5 ┆ 146.2 ┆ 273.1 ┆ 99.6 │
│ versicolor ┆ 281.9 ┆ 131.8 ┆ 202.9 ┆ 63.3 │
└────────────┴──────────────┴─────────────┴──────────────┴─────────────┘
```

9. Add [poetry-based GH workflow](../.github/workflows/poetry.yml) to check linting and run tests.
Loading

0 comments on commit 57204b1

Please sign in to comment.