Python Code
Samples
Easy snippets you can copy, study, and run in the browser editor.
Find Most Active Contributors in a Repository with Python
Filter recent commits by date and count the most active contributors using Counter and datetime.
from collections import Counter
from datetime import datetime, timedelta
# Simulated commit data
commits = [
{"author": "Alice", "timestamp": datetime.now() - timedelta(days=1)},
{"author": "Bob", "timestamp": datetime.now() - timedelta(days=2)},
{"author": "Alice", "timestamp": datetime.now() - timedelta…
How to Track Checkpoint Offset After Batch Commit in Python
A batch processor that tracks the last successfully committed offset after processing records in batches, advancing the checkpoint only when each batch commits successfully.
import json
from typing import Any
class BatchProcessor:
"""Tracks checkpoint offset after committing batches."""
def __init__(self, batch_size: int = 3):
self.batch_size = batch_size
self.offset = 0 # last successfully committed offset (exclusive)
self.total_committed = 0
def …
Get Git Status Info in Python
Run git commands from Python to gather branch name, number of changes, total commits, and clean status, returning them as a dict.
import subprocess
import json
from pathlib import Path
def get_git_status(repo_path="."):
"""Return basic git info about a repository as a dict."""
try:
branch = subprocess.check_output(
["git", "branch", "--show-current"],
cwd=repo_path,
stderr=subprocess.DEVNULL,…
Git Signing in Python
Sign and verify Git commits with a mock GPG implementation using HMAC and SHA-256.
import hashlib
import hmac
class GPGMock:
def __init__(self, secret_key):
self.secret_key = secret_key.encode()
def sign_commit(self, commit_message):
"""Mock GPG signing by computing an HMAC of the commit message."""
signature = hmac.new(self.secret_key, commit_message.encode(), hash…
How to Auto-Suggest a SemVer Bump From Git Commit Messages in Python
This code scans Git commit messages (recent or sample) and suggests the next Semantic Versioning bump type — major, minor, patch, or none.
import re
import subprocess
from pathlib import Path
def get_commit_messages(path="."):
"""Read commit messages from a repo or use sample messages."""
if (Path(path) / ".git").exists():
out = subprocess.run(
["git", "-C", path, "log", "--pretty=%s"], capture_output=True, text=True
…
How to Squash Commits Range into One in Python
A mock script that displays the last N git commits as a single squashed commit, showing original commit subjects.
import subprocess
import re
def squash_last_commits(count):
"""Mock squashing the last N commits into one by display."""
git_log = subprocess.run(
["git", "log", f"-{count}", "--pretty=format:%h %s"],
capture_output=True, text=True
)
if git_log.returncode != 0:
return "Git comm…
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.