Skip to content

Instantly share code, notes, and snippets.

View cgoldberg's full-sized avatar
👋
¯\_(ツ)_/¯

Corey Goldberg cgoldberg

👋
¯\_(ツ)_/¯
View GitHub Profile
@cgoldberg
cgoldberg / monkeypatch_partialmethod.py
Created August 20, 2025 18:34
Python - monkeypatch a method on a class and call the original method from the new one
#!/usr/bin/env python
#
# monkeypatch a method on a class and call the original method from the new one
from functools import partialmethod
class MyClass:
# this is the original method
@cgoldberg
cgoldberg / selenium_wait_click.txt
Last active August 20, 2025 18:22
Python - Selenium WebDriver function to wait for an element and click it
#!/usr/bin/env python
#
# Selenium WebDriver function to wait for an element and click it
import time
from selenium import webdriver
from selenium.common.exceptions import (
ElementClickInterceptedException,
@cgoldberg
cgoldberg / unicode_data.py
Created August 20, 2025 14:46
Python - some handy functions for generating Unicode data
#!/usr/bin/env python
#
# Functions for generating Unicode data
import random
import unicodedata
def get_all_chars():
@cgoldberg
cgoldberg / pipx_inline_script_deps.py
Created August 3, 2025 14:45
Python - example script using pipx with dependencies defined using inline script metadata (PEP 723)
#!/usr/bin/env -S pipx run
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "selenium==4.34.2",
# ]
# ///
# This script has dependencies defined using inline script metadata (PEP 723)
# Running it will create a temporary virtual environment, install depenendencies, then run the code (requires pipx)
@cgoldberg
cgoldberg / getopts_single_short_long.sh
Created August 1, 2025 03:26
Bash - hack for `getopts` to handle a single short or long option with no args
#!/usr/bin/env bash
# this is a total hack for `getopts` so it handles a single short or long option
# - valid options are: -a, -h, --all, --help
# - no args for options are allowed
die () {
tput setaf 1; echo -en "\u2717 "; tput sgr0
tput bold; echo "$*" 1>&2; tput sgr0
@cgoldberg
cgoldberg / tox.ini
Last active July 28, 2025 12:48
Python - workaround for calling black formatter from tox (so success messages don't display in red)
# Problem:
# When you call the black formatter, it prints all output to standand
# error (even when no formatting is required). However, tox colors all
# output to standard error in bold red. This results in black's
# "All done! ... files left unchanged" message being colored red in
# console output (which is very annoying).
#
# Workaround:
# This is a `tox.ini` configuration file that invokes black through a
# Python subprocess and redirects output to standard output when
@cgoldberg
cgoldberg / test_screenshot_fixture.py
Created July 24, 2025 21:18
Python (pytest and selenium) - fixture for taking screenshots on test failures
# example fixture for taking screenshots on test failures
#
# requires:
# - pytest
# - selenium
import time
import pytest
from selenium import webdriver
@cgoldberg
cgoldberg / git-bash.bat
Last active June 10, 2025 20:13
Windows batch file to launch Git-Bash with symlinks enabled
REM - launch Git-Bash with symlinks enabled
REM - (they are disabled for non-Admin accounts by default)
set MSYS=winsymlinks:native
%USERPROFILE%\Scoop\apps\git\current\git-bash.exe
@cgoldberg
cgoldberg / find_modules.py
Last active May 29, 2025 15:38
Python - Find all modules in a package without importing the package
#!/usr/bin/env python3
#
# Find all modules in a package without importing the package.
# This will work for a package installed in `site-packages` or read from source.
#
# Requires Python 3.9+
import importlib.util
import os
import site
@cgoldberg
cgoldberg / unarchive.sh
Created March 31, 2025 17:56
Bash function to extract any type of archive
extract () {
if [ -f $1 ] ; then
case $1 in
*.tar.bz2) tar xjf $1 ;;
*.tar.gz) tar xzf $1 ;;
*.bz2) bunzip2 $1 ;;
*.rar) rar x $1 ;;
*.gz) gunzip $1 ;;
*.tar) tar xf $1 ;;
*.tbz2) tar xjf $1 ;;