Skip to content

Instantly share code, notes, and snippets.

View bbelderbos's full-sized avatar

Bob Belderbos bbelderbos

View GitHub Profile
"""Single-file Pyodide exercise starter.
Run:
python pyodide_starter.py
Writes index.html next to this file and serves it on http://localhost:8765.
Open the URL, hit "Run tests", and watch pytest run inside the browser
against a small stdlib Pybites Bite (anagrams). Edit the HTML to swap in
your own exercises.
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "anthropic>=0.40",
# "httpx>=0.27",
# "openai>=1.50",
# "python-decouple>=3.8",
# ]
# ///
# AI trend digest: fetch HN + Reddit concurrently, rank with an LLM, print markdown.
"""
Finite State Machine — GitHub PR workflow
Full implementation + tests from: https://belderbos.dev/blog/build-finite-state-machine-python
Run: uvx pytest fsm.py -v
"""
import pytest
# --- Implementation ---
import asyncio
import random
from datastar_py import ServerSentEventGenerator as SSE
from datastar_py.fastapi import DatastarResponse
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
app = FastAPI(title="Datastar Stock Ticker")
# /// script
# dependencies = [
# "python-decouple",
# "requests",
# ]
# ///
import argparse
import json
import sys
# 1) Literal patterns: Cleanly dispatch on exact values (great for status codes and enums).
def handle_response(status: int) -> str:
match status:
case 200:
return "OK"
case 404:
return "Not Found"
case 500:
from collections import defaultdict
from graphlib import TopologicalSorter
from heapq import heappush, heappop, heapify
def topo_lex(pairs):
deps = defaultdict(set) # node -> set(dependencies)
for a, b in pairs:
deps.setdefault(a, set())
deps[b].add(a)
import json
from pathlib import Path
import sys
from urllib.request import urlretrieve
API = "https://rustplatform.com/api/"
DOWNLOAD_FILE = "exercises.json"
if not Path(DOWNLOAD_FILE).exists():
urlretrieve(API, DOWNLOAD_FILE)
print(f"Saved to {DOWNLOAD_FILE}")
function vid {
uvx --from pybites-search search video "$@"
}
function tip {
uvx --from pybites-search search tip "$@"
}
function pod {
uvx --from pybites-search search podcast "$@"
}
function bite {
# start rust alarm app script/process when opening terminal, but only one time
function run_alarm_if_not_running {
if ! pgrep -f "alarm -m 45 -M" > /dev/null; then
echo "Starting alarm..."
alarm -m 45 -M "stand up" -t 2 -r &
fi
}
run_alarm_if_not_running