Reference library

Python Code Samples

Easy snippets you can copy, study, and run in the browser editor.

5 matches
Git + Python easy

Git Signing in Python

Sign and verify Git commits with a mock GPG implementation using HMAC and SHA-256.

git signing hmac
Python
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…
13 0 Open
Git + Python easy

Verify Git tag signatures with HMAC in Python

Create and verify deterministic HMAC-SHA256 signatures for git tags using the Python standard library.

hmac git security
Python
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, …
14 0 Open
Cloud + Python easy

How to Evaluate Mock NACL Rules in Python

Simulate numbered AWS Network ACL rule evaluation with HMAC integrity checks on request payloads.

cloud network nacl
Python
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…
16 0 Open
Auth & security at scale easy

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.

hmac security cryptography
Python
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…
15 0 Open
Auth & security at scale easy

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.

security authentication timing-attacks
Python
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…
16 0 Open

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

  1. Pick a topic section — strings, lists, files, functions, and more
  2. Open a sample, read How it works, and copy the code block
  3. 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.