Skip to content

Instantly share code, notes, and snippets.

View mikeckennedy's full-sized avatar

Michael Kennedy mikeckennedy

View GitHub Profile
# Requires pip install unsync
import time
from unsync import unsync
import multiprocessing as mp
import sys
#Start and end numbers
start_number = 1
num_processes = mp.cpu_count()
import datetime
def run_with_no_ex(count: int):
idx = 0
while idx < count:
idx += 1
s = "".upper()
def run_with_ex(count: int):
@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)
@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.
# requires the uvloop package
import asyncio
import threading
import time
import uuid
from typing import Any, Coroutine
import uvloop
@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
@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 / 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 / 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 / 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