Skip to content

Instantly share code, notes, and snippets.

@timmc
timmc / essentially.clj
Last active Jun 23, 2020
originally refheap.com/97879
View essentially.clj
(defmacro essentially
"Like letfn, but with the bindings at the end. Connotes that the
bindings are not important for the core logic, just for logging,
metrics, debugging etc. Recommend using footnote-looking bindings such
as *0, *1, *2 or even Unicode daggers if you are super-brave.
This is like Haskell's `where`. Thanks to amalloy for the pointer; no
blame attaches to him, though."
[& args]
`(letfn ~(last args)
~@(butlast args)))
@timmc
timmc / gist:4a4711c15ff87b2735f2c74bc1efba4a
Last active Mar 16, 2020
Download all CDC MMWR for 2017
View gist:4a4711c15ff87b2735f2c74bc1efba4a
for i in {01..42}; do curl -sS "https://www.cdc.gov/mmwr/volumes/66/wr/pdfs/mm66${i}.pdf" > "66_${i}.pdf" & done
for i in {43..50}; do curl -sS "https://www.cdc.gov/mmwr/volumes/66/wr/pdfs/mm66${i}-H.pdf" > "66_${i}.pdf" & done
curl -sS "https://www.cdc.gov/mmwr/volumes/66/wr/pdfs/mm665152-H.pdf" > "66_51_52.pdf"
Text extraction:
for x in ./*; do pdftotext $x $x.txt; done
View parallel.sh
#!/bin/bash
# Call a script in a tight loop from multiple processes.
# Run as `$0 master <#seconds> <#children> script-and-args...`.
# seconds and children control run length and concurrency
# Modes:
# master <seconds> <children>: Fork and run n child processes for m seconds
# child: Call on tight loop, indefinitely
# test: Call just once
View diff-bash-history-changes.sh
#!/bin/bash
cd ~/sync/
function remove_timestamps {
grep -v -Pe '^\+#[0-9]+$' -
}
function baseline_no_ts {
git show HEAD:home/.bash_history | remove_timestamps
View totp.sh
#!/bin/bash
# Generates a TOTP code and copies it to the clipboard.
# Accepts path to a GPG-encrypted TOTP secret key file.
# Usage: $0 <encrypted-secrets-file>
#
# Encrypt a secret like this: xsel -bo | gpg2 --encrypt --recipient 0x32D0F478 > ~/.2fa/aws.gpg
set -eu -o pipefail
secretsFile="$1"
@timmc
timmc / sync-setter.kt
Created Nov 7, 2019
A synchronized setter with side-effects in Kotlin (probably a bad idea!)
View sync-setter.kt
private var writeLock = Object()
/**
* The canonical version of config that is on disk, barring any sneaky
* writes; setting this property causes a disk write.
*/
var config: Config = repo.config.readJson()
set(newConfig) {
synchronized(writeLock) {
repo.config.writeJson(newConfig)
@timmc
timmc / pwned-passwords-sqlite-build.py
Last active Aug 6, 2020
Building a sqlite DB for the Pwned Passwords data
View pwned-passwords-sqlite-build.py
#!/usr/bin/env python3
# Build a SQLite3 DB for looking up SHA-1 hashes of leaked passwords.
#
# This can be fed the txt file from one of Have I Been Pwned's hash
# lists available from https://haveibeenpwned.com/Passwords -- but any
# text file with line format ``hash-hex:count`` will work.
#
# When run on the v5 hash-ordered SHA-1 file, expect the build to take
# about 35 minutes and produce a 15.7 GiB file (~30.5 bytes per record).
#
@timmc
timmc / output-2-10.tsv
Created Jun 5, 2019
Uptime output for in/out hysteresis healthcheck
View output-2-10.tsv
0.00 1.00000
0.01 0.99937
0.02 0.99507
0.03 0.98962
0.04 0.98327
0.05 0.96881
0.06 0.95235
0.07 0.94017
0.08 0.91365
0.09 0.88673
@timmc
timmc / elb-hysteresis-2-10.svg
Last active Jun 5, 2019
Simulating ELB-style hysteresis for a host that exhibits random failures for both the healthcheck and regular requests
View elb-hysteresis-2-10.svg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@timmc
timmc / weighted-shuffle-sampling.clj
Created Mar 25, 2019
A sampling-based version of weighted-shuffle (better to use the exponential random solution)
View weighted-shuffle-sampling.clj
;; This is asymptotically slower (n^2) than the exponential random sort
;; one (n log n) shown in https://gist.github.com/timmc/1211c1ac8ae96c2b42c94124005b5414
;; but it is preserved here for possible later interest
(defn weighted-random-sample
"Given a coll of weights, pick one according to a weighted-random
selection, and return its index. Weights must be non-negative."
[weights]
(when (empty? weights)
(throw (IllegalArgumentException. "Cannot sample from empty list")))
You can’t perform that action at this time.