Skip to content

Instantly share code, notes, and snippets.

View toddbirchard's full-sized avatar
📝
Working, learning, writing tutorials inspired by items 1 and 2.

Todd Birchard toddbirchard

📝
Working, learning, writing tutorials inspired by items 1 and 2.
View GitHub Profile
@toddbirchard
toddbirchard / Makefile
Last active December 27, 2023 05:24
Makefile template for Python projects
PROJECT_NAME := $(shell basename $CURDIR)
VIRTUAL_ENVIRONMENT := $(CURDIR)/.venv
LOCAL_PYTHON := $(VIRTUAL_ENVIRONMENT)/bin/python3
define HELP
Manage $(PROJECT_NAME). Usage:
make run - Run $(PROJECT_NAME) locally.
make dev - Run $(PROJECT_NAME) in development mode.
make install - Create local virtualenv & install dependencies.
"""Extract nested values from a JSON tree."""
def json_extract(obj, key):
"""Recursively fetch values from nested JSON."""
arr = []
def extract(obj, arr, key):
"""Recursively search for values of key in JSON tree."""
if isinstance(obj, dict):
@toddbirchard
toddbirchard / config.py
Last active August 26, 2023 01:26
Collects locally stored images listed in report generated from `https://www.deadlinkchecker.com/`
"""Script configuration."""
from os import path
BASE_DIR = path.abspath(path.dirname(__file__))
# CSV Exported from `deadlinkchecker`
CSV_EXPORTED_BROKEN_LINKS = f"{BASE_DIR}/data/brokenlinks.csv"
@toddbirchard
toddbirchard / timeit.py
Last active August 26, 2023 00:56
Python Decorator to determine execution time of a given function
"""Decorator to determine execution time of a given function."""
from time import time
def timeit(method: func):
"""Print execution time of decorated function."""
def timed(*args, **kw):
ts = time()
result = method(*args, **kw)
@toddbirchard
toddbirchard / pandas_dataframe_difference.py
Last active August 26, 2023 00:48
Helper function to compare two DataFrames and find rows which are unique or shared.
"""Find Symmetric Differences between two Pandas DataFrames."""
def dataframe_difference(df1, df2, which=None):
"""Find rows which are different."""
comparison_df = df1.merge(
df2,
indicator=True,
how='outer'
)
@toddbirchard
toddbirchard / Makefile
Last active July 20, 2023 19:17
Python Makefile Template
PROJECT_NAME := $(shell basename $CURDIR)
VIRTUAL_ENVIRONMENT := $(CURDIR)/.venv
LOCAL_PYTHON := $(VIRTUAL_ENVIRONMENT)/bin/python3
PROJECT_ENTRY_POINT := $(shell $CURDIR)/main.py
define HELP
Manage $(PROJECT_NAME). Usage:
make run - Run $(PROJECT_NAME) locally.
import sys
import pymysql
import logging
class Database:
"""Database connection class."""
def __init__(self, config):
self.host = config.db_host
@toddbirchard
toddbirchard / image_optimize.py
Last active December 7, 2021 00:25
Recursively retrieve all images in a given site, generate retina images, convert all images to webp format.
import os
import json
import glob
import PIL
from PIL import Image
def get_all_images():
"""Create an array of PNGs and JPGs."""
img_arr = []
@toddbirchard
toddbirchard / ghostupdate.sh
Last active September 24, 2021 16:57
Update Ghost & Add Storage Adapter
#!/bin/bash
PROJECT=$(pwd)
CURRENT_GHOST_VERSION=$(ghost version)
NEW_GHOST_VERSION=$(ghost check-update)
# Ensure latest NPM & Ghost CLI versions
echo "Getting latest version of NPM & Ghost-ClI"
sudo npm install -g npm@latest
sudo npm install -g ghost-cli@latest
import os
import json
import boto3
from botocore.client import Config
import botocore
from urllib.parse import unquote
import PIL
# Initialize a session using DigitalOcean Spaces.