Python Code
Samples
Easy snippets you can copy, study, and run in the browser editor.
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…
Verify Git tag signatures with HMAC in Python
Create and verify deterministic HMAC-SHA256 signatures for git tags using the Python standard library.
import hashlib
import hmac
def sign_tag(tag: str, secret_key: str) -> str:
"""Create a deterministic HMAC signature for a tag."""
message = tag.encode("utf-8")
key = secret_key.encode("utf-8")
return hmac.new(key, message, hashlib.sha256).hexdigest()
def verify_signed_tag(tag: str, signature: str, …
How to Evaluate Mock NACL Rules in Python
Simulate numbered AWS Network ACL rule evaluation with HMAC integrity checks on request payloads.
import base64
import json
import hmac
import hashlib
def evaluate_mock_rule(rule_number, request_data, secret):
"""
Simulates evaluating an NACL-like numbered rule by:
1. Checking if the rule number exists in the mock policy.
2. Computing an HMAC over the request payload for integrity.
"""
# M…
How to Generate and Verify HMAC Signatures in Python
Create and validate HMAC-SHA256 signatures with a shared secret key using Python's hmac and hashlib modules.
import hashlib
import hmac
SECRET_KEY = b"pepper-secret-2024"
def generate_hmac(message: str) -> str:
return hmac.new(SECRET_KEY, message.encode("utf-8"), hashlib.sha256).hexdigest()
def verify_hmac(message: str, received_hmac: str) -> bool:
expected = generate_hmac(message)
return hmac.compare_digest(e…
How to Verify Passwords in Constant Time in Python
Use hmac.compare_digest to verify passwords in constant time, preventing timing attacks that could reveal password length or character positions.
import hmac
import time
# Mock of a constant-time password comparison (prevents timing attacks)
def verify_password(stored_password: str, supplied_password: str) -> bool:
# hmac.compare_digest runs in constant time (for a given length)
return hmac.compare_digest(stored_password.encode(), supplied_password.enc…
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.