Skip to content

Commit

Permalink
Add UV demo
Browse files Browse the repository at this point in the history
  • Loading branch information
win845 committed Nov 19, 2024
1 parent 5e282be commit 08a8c00
Show file tree
Hide file tree
Showing 14 changed files with 454 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: 2
updates:
- package-ecosystem: "pip"
directory: "/pkg"
schedule:
interval: "daily"
groups:
patches:
update-types:
- "minor"
- "patch"
open-pull-requests-limit: 100
38 changes: 38 additions & 0 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: On Push Workflow

on:
push:

jobs:
check-uv:
runs-on: ubuntu-latest
permissions:
contents: write

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2
- name: Install UV
run: |
pipx install uv
- name: Check UV
run: ./bin/check-uv.sh

build:
runs-on: ubuntu-latest
needs: check-uv

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Install UV
run: |
pipx install uv
- name: Run tests
run: |
uv run pytest tests
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Python-generated files
__pycache__/
*.py[oc]
build/
dist/
wheels/
*.egg-info

# Virtual environments
.venv
.vscode
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.12
68 changes: 68 additions & 0 deletions bin/check-uv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env bash
set -e

PYTHON_VERSION=$(cat .python-version)
BRANCH_NAME=${GITHUB_REF#refs/heads/}

# Case: user has added new dependencies to uv
# Check if there is a change in uv.lock but not in requirements.txt in the last commit
if git diff --name-only HEAD~1 | grep -q "uv.lock" && \
! git diff --name-only HEAD~1 | grep -q "requirements.txt"; then

echo "Export new uv.lock to requirements.txt"
uv export --no-hashes -o requirements.txt
fi

# Case: dependabot has updated a requirements.txt
# Check if there is a change in requirements.txt but not in uv.lock in the last commit
if git diff --name-only HEAD~1 | grep -q "requirements.txt" &&\
! git diff --name-only HEAD~1 | grep -q "uv.lock" && \
! ( \
grep -q "uv export" requirements.txt && \
grep -q "pip-compile pyproject.toml" requirements.txt \
); then
# Read requirements.txt, exclude comments, and format as TOML array
constraints=$(grep -vE '^\s*#' requirements.txt | awk '{print " \""$0"\","}')

# Create the section to append
cat << EOF >> pyproject.toml
[tool.uv]
constraint-dependencies = [
$constraints
]
EOF

echo "Lock uv with a new requirements.txt as constraint"
uv lock
uv export --no-hashes -o requirements.txt
fi

# Remove comments from requirements.txt
sed -i '' '/^#/d' requirements.txt

# Add pip-compile like comment to the top of requirements.txt
# for dependabot to detect a pip-compile workflow
cat << EOF | cat - requirements.txt > temp && mv temp requirements.txt
#
# This file is autogenerated by pip-compile with Python $PYTHON_VERSION
# by the following command:
#
# pip-compile pyproject.toml
#
# The above comment was added for dependabot to support uv.
# The file was exported via:
#
# uv export --no-hashes -o requirements.txt
#
EOF

git add uv.lock requirements.txt
if ! git diff --cached --quiet; then
git commit -m "Sync uv.lock and requirements.txt"
git push origin $BRANCH_NAME
exit 1
else
echo "No changes detected in uv.lock and requirements.txt"
exit 0
fi
5 changes: 5 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from uv_light.lens import Lens
from uv_light.beam import Beam

if __name__ == "main":
print(Beam().project_on(Lens()))
12 changes: 12 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[project]
name = "uv-light"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = [
"flask==3.*",
"pandas==2.*",
"pyarrow==17.*",
"pytest>=8.3.3",
]
Empty file added tests/__init__.py
Empty file.
Empty file added tests/uv_light/__init__.py
Empty file.
7 changes: 7 additions & 0 deletions tests/uv_light/test_beam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from uv_light.beam import Beam
from uv_light.lens import Lens

def test_beam_project_on():
actual = Beam().project_on(Lens())
expected = "~~~|--->\n~~~|--->\n~~~|--->\n~~~|--->"
assert actual == expected
279 changes: 279 additions & 0 deletions uv.lock

Large diffs are not rendered by default.

Empty file added uv_light/__init__.py
Empty file.
12 changes: 12 additions & 0 deletions uv_light/beam.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import pandas as pd
import pyarrow as pa

class Beam:
def __init__(self):
# Represent the beam with an arrow
self.beam_pattern = pa.array(["--->", "--->", "--->", "--->"])

def project_on(self, lens):
# Convert pyarrow array to pandas DataFrame
beam_frame = pd.DataFrame({'Beam': self.beam_pattern.to_pandas()})
return lens.focus_beam(beam_frame)
9 changes: 9 additions & 0 deletions uv_light/lens.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import pandas as pd

class Lens:
def focus_beam(self, beam_frame):
# Add lens symbol to the DataFrame
beam_frame['Lens'] = '~~~|'
# Combine Beam and Lens columns into a single string representation
beam_frame['Projection'] = beam_frame['Lens'] + beam_frame['Beam']
return beam_frame['Projection'].to_string(index=False, header=False)

0 comments on commit 08a8c00

Please sign in to comment.