-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
366 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
name: uv workflow | ||
on: | ||
push: | ||
paths: | ||
- 'uv-demo/**' | ||
- '.github/workflows/uv.yml' | ||
defaults: | ||
run: | ||
working-directory: ./uv-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 uv 0.4.2 | ||
run: curl -LsSf https://astral.sh/uv/0.4.2/install.sh | sh | ||
- name: Sync | ||
run: uv sync --all-extras --dev | ||
- name: Lint | ||
run: uv run ruff check | ||
- name: Run tests | ||
run: uv run pytest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
# uv Demo | ||
|
||
## Steps | ||
|
||
1. Install `uv`: | ||
```bash | ||
$ curl -LsSf https://astral.sh/uv/install.sh | sh | ||
``` | ||
|
||
2. Create a new project (with the `--lib` [option](https://docs.astral.sh/uv/concepts/projects/#libraries)): | ||
```bash | ||
$ uv init --lib uv-demo | ||
``` | ||
|
||
> [!TIP] | ||
> The `--lib` option specifies the `build-system` for us in `pyproject.toml` and creates the project in the [src layout](https://packaging.python.org/en/latest/discussions/src-layout-vs-flat-layout/). | ||
|
||
3. Add dependencies (`requests`, `polars`, `pytest`, `ruff`) | ||
* Note: Add `pytest` and `ruff` under the `dev` group. | ||
|
||
```bash | ||
$ uv add requests | ||
$ uv add polars | ||
$ uv add --dev pytest | ||
$ uv add --dev ruff | ||
``` | ||
|
||
Those commands added the following sections to our initial `pyproject.toml` file, and created/updated a `uv.lock` file. | ||
|
||
```toml | ||
[project] | ||
name = "uv-demo" | ||
version = "0.1.0" | ||
description = "" | ||
readme = "README.md" | ||
requires-python = ">=3.12" | ||
dependencies = [ | ||
"polars>=1.6.0", | ||
"requests>=2.32.3", | ||
] | ||
|
||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
|
||
[tool.uv] | ||
dev-dependencies = [ | ||
"pytest>=8.3.2", | ||
"ruff>=0.6.3", | ||
] | ||
``` | ||
|
||
4. Add code (`demo.py`, `__init__.py`, `__main__.py`) and tests (`test_demo.py`). | ||
|
||
5. Run tests and lint/format code: | ||
```bash | ||
$ uv run pytest | ||
$ uv run ruff check --fix | ||
$ uv run ruff format | ||
``` | ||
|
||
6. Run the app: | ||
```bash | ||
$ uv run python -m uv_demo | ||
``` | ||
|
||
Or, add the following section to `pyproject.toml`: | ||
```toml | ||
[project.scripts] | ||
"uv-demo" = "uv_demo:main" | ||
``` | ||
|
||
to run it more directly: | ||
```bash | ||
$ uv run uv-demo | ||
``` | ||
|
||
9. Add [uv-based GH workflow](../.github/workflows/uv.yml) to lint and run tests. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[project] | ||
name = "uv-demo" | ||
version = "0.1.0" | ||
description = "" | ||
readme = "README.md" | ||
requires-python = ">=3.12" | ||
dependencies = [ | ||
"polars>=1.6.0", | ||
"requests>=2.32.3", | ||
] | ||
|
||
[build-system] | ||
requires = ["hatchling"] | ||
build-backend = "hatchling.build" | ||
|
||
[tool.uv] | ||
dev-dependencies = [ | ||
"pytest>=8.3.2", | ||
"ruff>=0.6.3", | ||
] | ||
|
||
[project.scripts] | ||
"uv-demo" = "uv_demo:main" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from . import demo | ||
|
||
def main() -> int: | ||
demo.main() | ||
return 0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import uv_demo | ||
import sys | ||
|
||
sys.exit(uv_demo.main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from uv_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 |
Oops, something went wrong.