Skip to content

Instantly share code, notes, and snippets.

View samuelcolvin's full-sized avatar

Samuel Colvin samuelcolvin

View GitHub Profile
# /// script
# dependencies = [
# "pydantic-monty>=0.0.7",
# ]
# ///
import timeit
import pydantic_monty
code = "f'{get_greeting(tone='friendly')} {place}'"

The performance I got was:

request taken:  229745.32ms
model_construct taken:  33.59ms
model_validate_json taken:  13.34ms

Output tokens: 2707
@samuelcolvin
samuelcolvin / pypi.py
Created October 20, 2025 17:30
save this as "pypi" somewhere in your path to be able to query pypi versions from the terminal
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = [
# "httpx",
# "pydantic",
# "pygments",
# ]
# ///
import argparse
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "mcp",
# "python-dotenv",
# ]
# ///
import asyncio
import os
@samuelcolvin
samuelcolvin / test-logfire-mcp.py
Created July 31, 2025 10:07
test logfire-mcp
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "devtools",
# "mcp",
# "python-dotenv",
# ]
# ///
import asyncio
import os
SELECT
COUNT(*) AS num_downloads,
100 * COUNT(*) / SUM(COUNT(*)) OVER () AS percent_of_total,
'installer=' || coalesce(details.installer.name, '?') ||
' system=' || coalesce(details.system.name || details.system.release, '?') ||
' distro=' || coalesce(details.distro.name || details.distro.version, '?') as setup
FROM `bigquery-public-data.pypi.file_downloads`
WHERE
file.project = '...'
AND DATE_TRUNC(DATE(timestamp), MONTH) >= DATE_TRUNC(current_date(), month)
# /// script
# requires-python = ">=3.12"
# dependencies = [
# "logfire",
# "pydantic-ai-slim[google]",
# ]
# ///
import logfire
from pydantic_ai import Agent

Python HTTP clients

Results:

➤ uv run with_httpx.py  
time taken to make 10000 requests: 20.2337 seconds
Counter({200: 10000})

➤ uv run with_thread_pool.py 
@samuelcolvin
samuelcolvin / logfire_instrument_openai_agents.py
Created March 12, 2025 10:26
instrumenting openai agents with logfire and capturing exception tracebacks in function calls
from typing_extensions import TypedDict
import logfire
from httpx import AsyncClient
from agents import RunContextWrapper, Agent, function_tool, Runner
from agents.tool import WebSearchTool
logfire.configure()
logfire.instrument_openai_agents()