Skip to content

Instantly share code, notes, and snippets.

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

Corey Goldberg cgoldberg

👋
¯\_(ツ)_/¯
View GitHub Profile
@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 ;;
@cgoldberg
cgoldberg / download_selenium-manager.sh
Last active April 20, 2025 03:54
Bash script to download Selenium Manager binary for Linux to current directory
#!/usr/bin/env bash
#
# This script downloads the latest stable version of the `selenium-manager` binary
# for Linux (x86-64) from `https://github.com/SeleniumHQ/selenium_manager_artifacts/releases`
#
# When you run this script, it will download the binary to the current working directory.
set -e
@cgoldberg
cgoldberg / download_selenium-manager_to_repo.sh
Last active April 20, 2025 03:53
Bash script to download Selenium Manager binary for Linux to local selenium source tree
#!/usr/bin/env bash
#
# This script downloads the latest stable version of the `selenium-manager` binary
# for Linux (x86-64) from `https://github.com/SeleniumHQ/selenium_manager_artifacts/releases`
#
# Before running this script, you should have the selenium repo cloned locally:
# $ git clone --filter=blob:none [email protected]:SeleniumHQ/selenium.git
#
# Make sure to set the `SELENIUM_HOME` variable below to match the repo's base directory.
#
@cgoldberg
cgoldberg / selenium_logging.py
Last active March 16, 2025 18:48
Python - Enable DEBUG logging for Selenium WebDriver
import logging
# add one of these logging configurations to your selenium script to enable logging.
# 6 logger levels are supported: CRITICAL, ERROR, WARNING, INFO, DEBUG, NOTSET.
# (default is WARNING)
# enable DEBUG logging to console for all modules globally (selenium, urllib3, etc)
logging.basicConfig(level=logging.DEBUG)
# enable DEBUG logging to console for selenium module