Skip to content

Instantly share code, notes, and snippets.

View mikeckennedy's full-sized avatar

Michael Kennedy mikeckennedy

View GitHub Profile
@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 / 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 / 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
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 / 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:
//
@mikeckennedy
mikeckennedy / scape_tp.md
Created May 23, 2023 04:27
Result of asking for scraping titles from talk python episodes

MK: Write a python function to use requests and beautiful soup to scrape the titles of episodes of talk python to me

ChatGPT: Sure, here's a basic example of how you might use requests and BeautifulSoup to scrape the titles of episodes of Talk Python to Me. This code assumes that the titles are stored in HTML elements in a specific way. You may need to adjust the code based on the actual structure of the web page.

import requests
from bs4 import BeautifulSoup

def get_episode_titles():
    url = 'https://talkpython.fm/episodes/all' # replace with the actual URL
@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 / build_topics.py
Created November 23, 2022 18:46
Python script to build a tag cloud for Hugo websites
# You need to write a topics_template.md file with the [TOPIC_CLOUD] in the location to have the tag cloud injected.
# You also need to add topics.md to your config.toml for Hugo.
# This will read content/posts/*.md and pull out 'tags' from the frontmatter section to create topics.md
from collections import Counter
import json
from pathlib import Path
def main():
@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