Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 28 additions & 54 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ on:
required: true
type: string

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
test:
runs-on: ubuntu-latest
# activate conda environment automatically
defaults:
run:
shell: bash -el {0}
strategy:
fail-fast: false
matrix:
Expand All @@ -24,73 +27,44 @@ jobs:
environment-name: legacy-env
cache-environment: true

- name: Set up host Python
uses: actions/setup-python@v6
with:
python-version: 3.14
- name: Install uv
uses: astral-sh/setup-uv@v5

# Install Poetry. You could do this manually, or there are several actions that do this.
# `snok/install-poetry` seems to be minimal yet complete, and really just calls out to
# Poetry's default install script, which feels correct. I pin the Poetry version here
# because Poetry does occasionally change APIs between versions and I don't want my
# actions to break if it does.
#
# The key configuration value here is `virtualenvs-in-project: true`: this creates the
# venv as a `.venv` in your testing directory, which allows the next step to easily
# cache it.
- name: Install Poetry
uses: snok/install-poetry@v1
with:
version: 2.2.1
virtualenvs-create: true
virtualenvs-in-project: true
- name: Build Wheel (legacy python)
run: uv build
if: matrix.python-version == '3.6' || matrix.python-version == '3.7'

- name: Use Runner Python Environment
- name: Install dependencies
run: |
TARGET_PY=$HOME/micromamba/envs/legacy-env/bin/python
TARGET_PY="$HOME/micromamba/envs/legacy-env/bin/python"
uv build
uv export --no-emit-project -o requirements.txt
uv export --group dev --no-emit-project -o requirements.dev.txt

if [ "${{ matrix.python-version }}" = "3.6" ]; then
# poetry has trouble creating the venv since 3.6 is before __future__ annotations was introduced
$TARGET_PY -m venv .venv
poetry env use .venv/bin/python
if [[ "${{ matrix.python-version }}" == "3.6" ]] || [[ "${{ matrix.python-version }}" == "3.7" ]]; then
echo "Legacy python detected, using standard pip"

"$TARGET_PY" -m pip install -r requirements.txt
"$TARGET_PY" -m pip install -r requirements.dev.txt
"$TARGET_PY" -m pip install dist/*.whl
else
poetry env use "$TARGET_PY"
# we use `uv pip` instead of `uv sync` because we are installing into the conda environment, not the uv one.
# also we have this second check because uv should be a bit faster than bare pip for modern tests
uv pip install --python "$TARGET_PY" -r requirements.txt
uv pip install --python "$TARGET_PY" -r requirements.dev.txt
uv pip install --python "$TARGET_PY" -e .
fi

# Cache your dependencies (i.e. all the stuff in your `pyproject.toml`). Note the cache
# key: if you're using multiple Python versions, or multiple OSes, you need to include
# them in the cache key.
- name: cache deps
id: cache-deps
uses: actions/cache@v5
with:
path: .venv
key: pydeps-${{ runner.os }}-${{ matrix.python-version }}-${{ hashFiles('**/pyproject.toml', '**/poetry.lock') }}

# Install dependencies. `--no-root` means "install all dependencies but not the project
# itself", which is what you want to avoid caching _your_ code. The `if` statement
# ensures this only runs on a cache miss.
- name: Install dependencies
if: steps.cache-deps.outputs.cache-hit != 'true'
run: poetry install --no-interaction --no-root

# Now install _your_ project. This isn't necessary for many types of projects -- particularly
# things like Django apps don't need this. But it's a good idea since it fully-exercises the
# pyproject.toml and makes that if you add things like console-scripts at some point that
# they'll be installed and working.
- run: poetry install --no-interaction

- name: Static type checking
# do NOT try running mypy on 3.6
if: matrix.python-version != '3.6'
run: poetry run mypy .
run: mypy .

# run the tests and check for 100% coverage
- name: Run unit tests
run: poetry run pytest . --cov=randfacts --cov-report=term-missing --cov-report=xml
run: pytest . --cov=randfacts --cov-report=term-missing --cov-report=xml

- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v5.5.2
uses: codecov/codecov-action@v5
with:
token: ${{ secrets.CODECOV_TOKEN }}
flags: python-${{ matrix.python-version }}
1 change: 1 addition & 0 deletions .python-version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.14
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
.PHONY: test test-cov lint format clean check-all

test:
uv run pytest .

test-cov:
uv run pytest . --cov=randfacts --cov-report=term-missing --cov-report=xml

format:
uv run ruff format .

lint:
uv run ruff check .

clean:
find . -type d -name "__pycache__" -exec rm -rfv "{}" +
rm -rfv .mypy_cache .pytest_cache .ruff_cache dist
rm -fv .coverage coverage.xml

check-all: test-cov lint
uv run mypy .
uv run ruff format . --check

43 changes: 41 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,20 +22,32 @@ Randfacts is a Python module that generates random facts. You can use `randfacts

randfacts can either be installed via pip or via the AUR, whichever way you prefer.

### Installation via pip:
### Installation via uv/pipx/pip:

```sh
# Install with UV:
$ uv tool install randfacts

# Or just try it out:
$ uvx randfacts

# Install with pipx:
$ pipx install randfacts

# Install with bare pip:
$ pip3 install randfacts
```

### Installation via AUR:

Via your AUR helper, like paru:

```sh
$ paru -S python-randfacts
```

Or manually

```sh
$ git clone https://aur.archlinux.org/python-randfacts.git && cd python-randfacts
$ makepkg -si
Expand All @@ -48,10 +60,12 @@ import randfacts
x = randfacts.get_fact()
print(x)
```

The above example will print a random fact like:
`Penguins can't taste sweet or savory flavors, only sour and salty ones`

This package has a filter option to filter out potentially inappropriate facts. The filter is on by default. To disable the filter, you can just set the `filter_enabled` parameter to `False`.

```python
from randfacts import get_fact
print(get_fact(False))
Expand All @@ -67,7 +81,6 @@ print(get_fact(only_unsafe=True))

If you want to access the list of facts directly, you can just import the `safe_facts`, `unsafe_facts`, or `all_facts` lists from the randfacts module.


## Command line usage

randfacts can be executed via the command line with the following commands:
Expand Down Expand Up @@ -95,3 +108,29 @@ More help.
```sh
$ python3 -m randfacts --help
```

# Contributing

Contributions are welcome, this project uses [uv](https://github.com/astral-sh/uv) to manage it's dependencies, so setup is very simple.

## Development Setup

1. Make sure uv is properly installed
2. Fork and clone the repository with `git clone git@github.com:your-username/randfacts.git`
3. Install dependencies with `uv sync --dev`
4. After making changes, ensure the tests pass, and add any unit tests needed to bring code coverage to 100% with the following command:

```sh
# check everything, if you're just working on one particular issue,
# see the Makefile for more granular tasks
make check-all
```

## Duplicate facts test

I've created a fairly complex duplicate fact detection test in Rust. It will be easier to run on your machine to check/fix any issues, but it also runs in CI if you don't want to set up Rust. It can be run with the following:

```sh
cd tests/checkduplicates
cargo run --release
```
Loading