Python Code
Samples
Medium snippets you can copy, study, and run in the browser editor.
How to Build a Burn Rate Alert with Multiple Time Windows in Python
Track token consumption and trigger alerts when the burn rate exceeds a threshold across multiple time windows using deque and time-based sliding windows.
import time
from collections import deque
class BurnRateAlert:
def __init__(self, windows_seconds=(60, 300, 900), threshold_rate=0.8):
self.windows = {w: deque() for w in windows_seconds}
self.threshold_rate = threshold_rate
self.previous_tokens = None
def record_sample(self, current_…
How to Implement an Error Budget Policy in Python
This code implements a mock error budget policy that decides whether to freeze deployments based on a simulated error rate and monthly freeze limits.
from datetime import datetime, timedelta
class ErrorBudgetPolicy:
FREEZE_WINDOW_HOURS = 24
MAX_FREEZES_PER_MONTH = 3
def __init__(self, budget_percentage=99.9):
self.budget_percentage = budget_percentage
self.freeze_count = 0
self.last_freeze_start = None
self.freeze_enabl…
Browse by section
Each section groups closely related Python snippets.
Guide: free Python code samples library
Copy-ready Python snippets for learners and developers
PythonSkillset code samples are short, focused examples organised by topic and difficulty. Every snippet is server-rendered HTML — readable by search engines and easy to copy. Open any sample, read the notes, copy the code, then press Try in editor to run it in the browser with Pyodide.
How to use this library
- Pick a topic section — strings, lists, files, functions, and more
- Open a sample, read How it works, and copy the code block
- Run it in the IDE, tweak values, then take a related quiz or tutorial lesson
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.