Skip to content

Commit

Permalink
Add Rye example
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkun committed Jul 4, 2024
1 parent a0753fe commit 090fe82
Show file tree
Hide file tree
Showing 11 changed files with 231 additions and 0 deletions.
29 changes: 29 additions & 0 deletions .github/workflows/rye.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
name: Rye workflow
on:
push:
paths:
- 'rye-demo/**'
- '.github/workflows/rye.yml'
defaults:
run:
working-directory: ./rye-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 Rye
uses: eifinger/setup-rye@v3
with:
version: '0.35.0'
- name: Sync
run: rye sync
- name: Lint
run: rye lint
- name: Run tests
run: rye run pytest
1 change: 1 addition & 0 deletions rye-demo/.python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12.3
70 changes: 70 additions & 0 deletions rye-demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Rye Demo

## Steps

1. Install `rye`:
```bash
$ curl -sSf https://rye.astral.sh/get | bash
```

2. Create a new project (with `--script` option for later):
```bash
$ rye init --script rye-demo
```

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

```bash
$ rye add requests
$ rye add polars
$ rye add --dev pytest
$ rye add --dev ruff
```

Those commands added the following sections to our initial `pyproject.toml` file, and created/updated `requirements.lock` and `requirements-dev.lock` files.

```toml
[project]
...
dependencies = [
"requests>=2.32.3",
"polars>=1.0.0",
]

[tool.rye]
managed = true
dev-dependencies = [
"pytest>=8.2.2",
"ruff>=0.5.0",
]
```

4. Install the project?
```bash
$ rye sync
```

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

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

Note: `rye` comes with formatting/linting commands (as of version `0.20.0`), so we could skip the roundabout call to `ruff` and just call:
```bash
$ rye lint --fix
$ rye fmt
```

7. Run the app:
```bash
$ rye run python -m rye_demo
# or
$ rye run rye-demo
```

9. Add [rye-based GH workflow](../.github/workflows/rye.yml) to check linting and run tests.
33 changes: 33 additions & 0 deletions rye-demo/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[project]
name = "rye-demo"
version = "0.1.0"
description = ""
authors = [
{ name = "David Kun", email = "[email protected]" }
]
dependencies = [
"requests>=2.32.3",
"polars>=1.0.0",
]
readme = "README.md"
requires-python = ">= 3.8"

[project.scripts]
"rye-demo" = "rye_demo:main"

[build-system]
requires = ["hatchling"]
build-backend = "hatchling.build"

[tool.rye]
managed = true
dev-dependencies = [
"pytest>=8.2.2",
"ruff>=0.5.0",
]

[tool.hatch.metadata]
allow-direct-references = true

[tool.hatch.build.targets.wheel]
packages = ["src/rye_demo"]
31 changes: 31 additions & 0 deletions rye-demo/requirements-dev.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# generated by rye
# use `rye lock` or `rye sync` to update this lockfile
#
# last locked with the following flags:
# pre: false
# features: []
# all-features: false
# with-sources: false
# generate-hashes: false

-e file:.
certifi==2024.7.4
# via requests
charset-normalizer==3.3.2
# via requests
idna==3.7
# via requests
iniconfig==2.0.0
# via pytest
packaging==24.1
# via pytest
pluggy==1.5.0
# via pytest
polars==1.0.0
# via rye-demo
pytest==8.2.2
requests==2.32.3
# via rye-demo
ruff==0.5.0
urllib3==2.2.2
# via requests
23 changes: 23 additions & 0 deletions rye-demo/requirements.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# generated by rye
# use `rye lock` or `rye sync` to update this lockfile
#
# last locked with the following flags:
# pre: false
# features: []
# all-features: false
# with-sources: false
# generate-hashes: false

-e file:.
certifi==2024.7.4
# via requests
charset-normalizer==3.3.2
# via requests
idna==3.7
# via requests
polars==1.0.0
# via rye-demo
requests==2.32.3
# via rye-demo
urllib3==2.2.2
# via requests
5 changes: 5 additions & 0 deletions rye-demo/src/rye_demo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from . import demo

def main() -> int:
demo.main()
return 0
4 changes: 4 additions & 0 deletions rye-demo/src/rye_demo/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import rye_demo
import sys

sys.exit(rye_demo.main())
26 changes: 26 additions & 0 deletions rye-demo/src/rye_demo/demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from io import StringIO
import polars as pl
import requests

IRIS = "https://raw.githubusercontent.com/mwaskom/seaborn-data/master/iris.csv"


def main():
result = analyze(dataframe_from(iris_data()))
print(result)


def iris_data(url: str = IRIS) -> str:
return requests.get(url).text


def dataframe_from(csv_data: str) -> pl.DataFrame:
return pl.read_csv(StringIO(csv_data))


def analyze(df: pl.DataFrame) -> pl.DataFrame:
return df.filter(pl.col("sepal_length") > 5).group_by("species").agg(pl.all().sum())


if __name__ == "__main__":
main()
Empty file added rye-demo/tests/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions rye-demo/tests/test_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from rye_demo.demo import dataframe_from


def test_dataframe_from():
df = dataframe_from(
"""A,B,C
0.0,10.0,200.1"""
)
assert len(df.count()) == 1

0 comments on commit 090fe82

Please sign in to comment.