Skip to content

Instantly share code, notes, and snippets.

View mikeckennedy's full-sized avatar

Michael Kennedy mikeckennedy

View GitHub Profile
@mikeckennedy
mikeckennedy / auto_python_venv.sh
Created August 12, 2025 00:38
Auto-activate Python virtual environment for any project with a venv directory in your shell (macOS/Linux).
# Include this in your shell *rc file (e.g. .bashrc, .zshrc, etc).
# Update the folder name to your convention.
# This uses venv as the virtual environment name, but if you use .venv, change it in the script.
# Auto-activate virtual environment for any project with a venv directory
function chpwd() {
# Function to find venv directory in current path or parent directories
local find_venv() {
local dir="$PWD"
@mikeckennedy
mikeckennedy / cache_buster.py
Created September 10, 2025 16:34
Utility to add cache busting query parameter to all static elements on a site.
import hashlib
import os
from typing import Optional
from pyramid.request import Request
from pyramid.response import Response
def build_cache_id(
filename: str,
@mikeckennedy
mikeckennedy / .rumdl.toml
Created August 20, 2025 03:37
Michael Kennedy's rumdl configuraiton for his book "Talk Python in Production"
# Global settings
[global]
exclude = ["node_modules", "build", "dist"]
respect_gitignore = true
# Disable specific rules
disable = ["MD013", "MD033", "MD045", "MD025", "MD042", "MD041", "MD024", "MD042", "MD052", "MD036", "MD029", "MD026", "MD002", "MD014"]
# MD013 - Line length: Allows lines longer than the default limit for better readability in technical docs
# MD033 - Inline HTML: Permits HTML tags within markdown for enhanced formatting when needed
# MD045 - Images should have alternate text: Allows images without alt text (useful for decorative images)
@mikeckennedy
mikeckennedy / turnstile.py
Last active June 18, 2025 02:14
Python code for server-side Cloudflare validation (paired with client-side Cloudflare code)
# See details about Turnstile at https://blog.cloudflare.com/turnstile-private-captcha-alternative/
from typing import Optional
import pydantic
import requests
cloudflare_secret_key: Optional[str] = None
import datetime
import sys
from typing import Optional
import colorama
import pyramid.config
from loguru import logger
from pyramid.request import Request
from pyramid.response import Response
@mikeckennedy
mikeckennedy / speed_of_exceptions.py
Created July 9, 2020 18:02
Simple example to test how much faster or slower it is to simply try and convert then catch an error vs. testing up front.
import datetime
import random
from typing import List
def main():
random.seed(172)
count = 1_000_000
data = build_data(count)
run_with_except(data)
@mikeckennedy
mikeckennedy / account_views.py
Last active December 17, 2024 07:25
Turnstile Python Example
# Short usage example in form post handler on the server:
def form_post_handler():
turnstile_response = form_dict.get('cf-turnstile-response')
validation_response = turnstile.validate(turnstile_response, get_client_ip())
if not validation_response.success:
# Handle the error
...
# All is good from here out...
@mikeckennedy
mikeckennedy / asset_bundler_watcher.py
Last active October 15, 2024 18:07
Example of livereload in python: Creates a file watcher that runs `python build_assets.py`, a utility based on webassets package whenever a css or js file is edited. It bundles a bunch of our CSS files into a single minified one and does the same for our JS files.
from pathlib import Path
# Creates a file watcher that runs `python build_assets.py`, a utility based on
# webassets package whenever a css or js file is edited. It bundles a bunch of
# our CSS files into a single minified one and does the same for our JS files.
def main():
# Only import this if we run it, don't let the webframework try to pull it in prod.
import livereload.server
# requires the uvloop package
import asyncio
import threading
import time
import uuid
from typing import Any, Coroutine
import uvloop
@mikeckennedy
mikeckennedy / server-hot-reload.js
Last active August 20, 2023 18:55
Server-side hot reload checker by Michael Kennedy. When the contents of the page change in any way, the page will be reloaded.
// *************************************************************************
// server-hot-reload.js v1.0.5
//
// Server-side hot reload check by Michael Kennedy (https://mkennedy.codes).
// Released under the MIT open source license, 2023.
//
// When the contents of the page change in any way, the page will be reloaded.
//
// Usage:
//