Skip to content

Instantly share code, notes, and snippets.

View mikeckennedy's full-sized avatar

Michael Kennedy mikeckennedy

View GitHub Profile
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 April 6, 2024 03:10
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.
@mikeckennedy
mikeckennedy / loops_vs_comps_speed.py
Created February 20, 2022 17:05
Are list comprehensions faster than loops?
import datetime
count = 10_000_000
def with_loop():
lst = []
for n in range(1, count):
if n % 2 == 0:
lst.append(n)