Skip to content

Instantly share code, notes, and snippets.

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

Corey Goldberg cgoldberg

👋
¯\_(ツ)_/¯
View GitHub Profile
@cgoldberg
cgoldberg / generate_monorepo_sboms.py
Last active September 12, 2025 23:02
Python - Generate language specific SBOMs from a multi-language monorepo on GitHub
#!/usr/bin/env python3
#
# Copyright (c) 2025 Corey Goldberg (https://github.com/cgoldberg)
# License: MIT
#
# Generate language specific SBOMs from a multi-language monorepo on GitHub
# - fetches repo-wide SBOM from GitHub
# - generates individual SBOMs for each language specified
#
# requires:
@cgoldberg
cgoldberg / hash-sha3-512.py
Created September 4, 2025 13:04
Python - Generate a SHA3-512 hash with optional salt
# Generate a SHA3-512 hash with optional salt
import re
# requires pycryptodome
# - https://pycryptodome.readthedocs.io/en/latest/src/hash/sha3_512.html
from Crypto.Hash import SHA3_512
def generate_hash(s, salt=None):
@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