Skip to content

Instantly share code, notes, and snippets.

View sloanlance's full-sized avatar
Certified GitHub Pro

Mr. Lance E Sloan sloanlance

Certified GitHub Pro
View GitHub Profile
@sloanlance
sloanlance / yq Notebook.md
Last active September 29, 2023 14:35
Useful notes about using yq

yq Notebook

Useful notes about using yq

  1. Display the path to each value in a YAML file
    $ cat << EOF | yq e '.. | select(. == "*") | {(path | join(".")): .} ' -
    > a:
    >   b: foo

> c: bar

@sloanlance
sloanlance / git-timestamps.sh
Created May 22, 2023 15:08
Set timestamps of files in a cloned git repository to when they were last committed.
#!/bin/sh --
# Set timestamps of files in a cloned git repository to when they were last committed.
# Default: All git-controlled files in current directory, recursively
if [ ${#} = 0 ]; then
files="$(git ls-files -z | xargs -0 echo)"
else
files="${@}"
fi
@sloanlance
sloanlance / IPA Reader Lambda (S3).py
Created April 4, 2023 16:10 — forked from katie7r/IPA Reader Lambda (S3).py
IPA Reader Lambda function (with S3 storage of saved audio files)
import boto3
import os
from contextlib import closing
from botocore.exceptions import ClientError
def object_exists(s3, bucket, key):
try:
boto3.resource('s3').Object(bucket, key).load()
return True
@sloanlance
sloanlance / bgsounds.sh
Last active March 5, 2024 20:06
bgsounds.sh: Toggle macOS Background Sounds on or off.
#!/bin/sh --
# Toggle macOS Background Sounds on or off.
#
# See the System Settings panel for Accessibility > Audio to change settings
# like the sound played, volume level, and automatic stop.
# `read` returns an integer, but…
if [ $(defaults read com.apple.ComfortSounds 'comfortSoundsEnabled') = 0 ]
then
#!/bin/sh
awk 'function wl() {
rate=64000;
return (rate/160)*(0.87055^(int(rand()*10)))};
BEGIN {
srand();
wla=wl();
while(1) {
wlb=wla;
wla=wl();
@sloanlance
sloanlance / recursiveFormat.py
Last active November 25, 2021 03:27
`recursiveFormat()` – Recursively apply `string.format()` to all strings in a data structure.
def recursiveFormat(args, **kwargs):
"""
Recursively apply `string.format()` to all strings in a data structure.
This is intended to be used on a data structure that may contain
format strings just before it is passed to `json.dump()` or `dumps()`.
Ideally, I'd like to build this into a subclass of `json.JsonEncoder`,
but it's tricky to separate out string handling in that class. I'll
continue to think about it.
@sloanlance
sloanlance / ngrok_public_url.sh
Last active August 25, 2020 18:00 — forked from lsloan/ngrok_public_url.sh
Get the local ngrok public URL, which includes the hostname, extracted using jq
#!/bin/sh --
# Get the local ngrok public URL, which includes the hostname, extracted using jq
# Inspiration: https://gist.github.com/rjz/af40158c529d7c407420fc0de490758b#gistcomment-2594627
curl --silent http://127.0.0.1:4040/api/tunnels | jq -r '.tunnels[0].public_url'
@sloanlance
sloanlance / durationToSeconds.py
Last active November 25, 2021 03:27
Convert ISO 8601 durations to seconds.
import re
from functools import reduce
import string
def durationToSecondsRegex(duration: str) -> int:
"""
Parse duration string; return integer number of seconds.
For example, the duration string "1h2m3s" (1 hour, 2 minutes, 3 seconds)
would return 3723 seconds.
@sloanlance
sloanlance / three_reasons.bas
Last active December 10, 2018 14:30
"Three Reasons To Own a Computer" for Commodore 64, from Compute!'s Gazette (via https://www.lemon64.com/forum/viewtopic.php?p=429351&sid=a78679bc4b8671443f9300905275b9e5#429351)
0 REM "THREE REASONS TO OWN A COMPUTER" FOR COMMODORE 64
1 REM FROM COMPUTE!'S GAZETTE
10 FORL=54272 TO 54295:POKEL,0:NEXT:POKE54296,15
20 POKE54277,8:POKE54278,255:POKE54276,23
30 F2=4
40 FORZ=1TO3:POKE54287,F2
50 FORF1=1TO200
60 POKE54273,F1:F2=F2+.01:NEXTF1:NEXTZ:POKE54278,15
@sloanlance
sloanlance / accessEncapsulation.py
Created July 19, 2018 17:33
Python: Class member access enforcement (not encapsulation)
# From Stack Overflow question:
# https://stackoverflow.com/questions/26216563/how-to-do-encapsulation-in-python
#
# Python has simple encapsulation, but it doesn't strictly enforce access to
# protected or private class members. This is one person's attempt to
# address that in a (misleadingly named) class.
#
# Could this be done with introspection rather than using on `self.privates` and
# `self.protected`? Follow the common Python style of naming variables `__a` and `_b`.
# Then the methods would check whether the names begin with `__` or `_` to allow or