Skip to content

Instantly share code, notes, and snippets.

View angstwad's full-sized avatar

Paul Durivage angstwad

View GitHub Profile
@angstwad
angstwad / .xonshrc
Created January 15, 2025 04:43
Xonsh
$XONSH_SHOW_TRACEBACK = True
$PROMPT_COMMAND = 'history -a'
$HISTSIZE = 25000
$HISTCONTROL = 'ignoreboth'
$CLICOLOR = 1
$EDITOR = 'vim'
$HOMEBREW_NO_ENV_HINTS = 1
$PROJECTS = '~/projects'
$GOPATH = $PROJECTS + '/go'
@angstwad
angstwad / singleton.py
Last active December 2, 2024 22:37
Simple, thread-safe singleton mixin class for Python
class SingletonMixin:
""" Threading safe singleton mixin class. Will run __init__ on subclasses for every
invocation, so check self._initialized from __init__ to get around this if not desired.
Use:
class MyClass(SingletonMixin):
def __init__(self):
if not self._initialized:
# initialize here
self.foo = 'bar'
@angstwad
angstwad / standard-logging.py
Last active December 2, 2024 22:37
Opinionated, standardized logging for Python CLIs
# An opinionated approach to logging in Python
import logging
import sys
def setup_logger(name: str | None = None,
level: str | int = 'WARN',
log_format: str | None = None) -> logging.Logger:
"""Sets up an opinionated logger bifurcating on log level to send
error and warning logs to stderr, info and debug to stdout
@angstwad
angstwad / infinite-default-dict.py
Last active December 2, 2024 22:38
Python defaultdict which can be assigned to an arbitrary depth
import collections
def infinidict(*args, **kwargs):
""" Magical defaultdict that allows key assignments to an aribtrary depth
Example:
d = infinidict()
d['foo']['bar']['baz'] == 'quux'
d['foo']['bar']['baz'] # result is 'quux'
"""
@angstwad
angstwad / enumerate_async.py
Last active January 13, 2025 16:22
enumerate with asyncio in Python
from typing import AsyncIterable
async def enumerate_async[T](iterable: AsyncIterable[T], start=0) -> tuple[int, T]:
counter = start
async for item in iterable:
yield counter, item
counter += 1
# example usage
@angstwad
angstwad / lazyfiletype.py
Last active December 2, 2024 22:39
Better argparse.FileType - lazily open files passed as arguments rather than them being opened immediately
import argparse
import pathlib
from typing import IO, Self
class LazyFileType(argparse.FileType):
"""Subclasses `argparse.FileType` in order to provide a way to lazily open
files for reading/writing from arguments. Initializes the same as the
parent, but provides `open` method which returns the file object.
Usage:
@angstwad
angstwad / chdir.py
Last active December 2, 2024 22:40
os.chdir, but a context manager
class Chdir:
def __init__(self, chdir: str | pathlib.Path):
self._pwd = os.getcwd()
self._chdir = chdir
def __enter__(self):
os.chdir(self._chdir)
return self
def __exit__(self, exc_type, exc_val, exc_tb):
@angstwad
angstwad / urls.txt
Created March 21, 2022 15:55
Apple TV 4K URLs
https://sylvan.apple.com/Aerials/2x/Videos/comp_CH_C007_C011_PSNK_v02_HDR_FINAL_20180709_HDR_4K_HEVC.mov
https://sylvan.apple.com/Aerials/2x/Videos/comp_CH_C002_C005_PSNK_v05_HDR_FINAL_20180709_HDR_4K_HEVC.mov
https://sylvan.apple.com/Aerials/2x/Videos/comp_CH_C007_C004_PSNK_v02_HDR_FINAL_20180709_HDR_4K_HEVC.mov
https://sylvan.apple.com/Aerials/2x/Videos/comp_C004_C003_PS_v01_HDR_PS_20180925_HDR_4K_HEVC.mov
https://sylvan.apple.com/Aerials/2x/Videos/comp_C003_C003_PS_v01_HDR_PS_20180925_HDR_4K_HEVC.mov
https://sylvan.apple.com/Aerials/2x/Videos/comp_C001_C005_PS_v01_HDR_PS_20180925_HDR_4K_HEVC.mov
https://sylvan.apple.com/Aerials/2x/Videos/DB_D008_C010_4K_HDR_HEVC.mov
https://sylvan.apple.com/Aerials/2x/Videos/comp_DB_D008_C010_PSNK_v21_HDR_PS_20180914_F0F16157_HDR_4K_HEVC.mov
https://sylvan.apple.com/Aerials/2x/Videos/DB_D001_C001_4K_HDR_HEVC.mov
https://sylvan.apple.com/Aerials/2x/Videos/comp_DB_D001_C001_PSNK_v06_HDR_PS_20180824_HDR_4K_HEVC.mov
@angstwad
angstwad / app.py
Last active June 2, 2021 23:11
todo flask app
# Import Flask, Jsonify and Requests
from flask import Flask, jsonify, request
# Create the web applicatiion via Flask
app = Flask(__name__)
# Existing To-Do List
# it's easiest to manipulate if this is a dict where key is the id and value is the todo
todos = {
1: "Buy Hitman 3",
@angstwad
angstwad / signals.py
Created October 14, 2020 18:00
A simple script to catch any valid OS signals and simulate an action in response to a signal
import argparse
import functools
import logging
import os
import signal
import sys
import time
_LOG_FORMAT = "%(levelname)s:%(asctime)s:%(name)s:%(message)s"
logging.basicConfig(stream=sys.stdout, format=_LOG_FORMAT)