Skip to content

Instantly share code, notes, and snippets.

View mikeckennedy's full-sized avatar

Michael Kennedy mikeckennedy

View GitHub Profile
@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 June 23, 2024 18:01
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 September 26, 2024 18:50
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
@mikeckennedy
mikeckennedy / cost_of_threads.py
Last active March 7, 2022 04:10
How much memory do Python threads use? Apparently not too much.
# Requires psutil package
import os
import threading
import time
import psutil
should_run = True
# requires the uvloop package
import asyncio
import threading
import time
import uuid
from typing import Any, Coroutine
import uvloop
@mikeckennedy
mikeckennedy / syncify.py
Last active April 2, 2023 07:02
Convert an async method to a synchronous one.
import asyncio
import functools
from typing import Any, Coroutine
loop = asyncio.new_event_loop()
def run(async_coroutine: Coroutine[Any, Any, Any]):
"""
Convert an async method to a synchronous one.