Skip to content

Instantly share code, notes, and snippets.

@algal
algal / mdfindfile
Last active November 12, 2020 01:09
Easily search for files based their names or glob patterns, by wrapping Spotlight.
#!/usr/bin/env bash
# case-insensitive search for $1, maybe in dir $2
case "$#" in
0) echo "usage: $(basename $0) PATTERN [DIR]"
echo ""
echo " Lists paths of files matching PATTERN in DIR or below"
;;
1) exec mdfind "kMDItemDisplayName == '$1'c"
;;
*) exec mdfind "kMDItemDisplayName == '$1'c" -onlyin "$2"
@algal
algal / plot_cm.py
Created September 9, 2020 20:08
Plot a confusion matrix in scikitlearn from data not from an estimator
# This uses scikit learn internals, since the sk public API requires you to pass
# in an estimator and sometimes you just want to pass in the some data you'd
# use to calculate a raw CM
def plot_cm(y_true,y_pred,labels):
from sklearn.metrics._plot.confusion_matrix import ConfusionMatrixDisplay
sample_weight = None
normalize = None
include_values = True
@algal
algal / SFUSDCalendar.md
Created August 29, 2020 20:20
To use an SFUSD Gmail account to subscribe to a non-SFUSD Google Calendar

Subscribing to a Google Calendar from an SFUSD Google Account

SFUSD Google accounts can only send and receive emails from SFUSD teachers, so they cannot receive calendar invitations from normal gmail accounts, like the gmail accounts of their parents. So you cannot invite them to join a calendar as usual.

Here's a workaround.

In the normal gmail account:

  1. Create the calendar to share from a normal gmail account.
  2. Go to Calendar Settings / General
@algal
algal / excel_parseDates.vba
Last active September 27, 2023 21:28
please god why aren't these predefined?
' Expects a datetimestr in the format "YYYYMMDD" with - or / or no separator
' Parses independently of local region, unlike VBA.DateTime.DateValue()
' Known-good on Excel for Mac Version 16.4
Function parseYYYYMMDD(dateTimeStr As String) As Date
Dim normalized As String
normalized = VBA.Strings.Trim(dateTimeStr)
normalized = VBA.Strings.Replace(dateTimeStr, "/", "")
normalized = VBA.Strings.Replace(normalized, "-", "")
from typing import Dict, List, Tuple
def makeKeyDict(items:List[Tuple]) -> Dict:
"Gives a list of tuples (a,b), returns a dict mapping a to set of b"
d = {}
for (a,b) in items:
d[a] = set([b]).union(d.get(a,set()))
return d
def findKeysWithMultipleValues(keydict) -> List:
@algal
algal / DataFrameFromMD5.py
Created July 28, 2020 21:01
Read file paths, names, hashes into a data frame
from fastai2.vision.all import * # to get L
import pandas as pd
def readMD5file(md5path:Path) -> pd.DataFrame:
"""
Generate MD5 output file by doing a search like:
find /home/jupyter/data/foldersToAdd/ -iname '*jpg' -print0 | xargs -0 -n 100 md5sum >> /home/jupyter/data/foldersToAdd.md5.out
@algal
algal / addWideMargins.bash
Created July 15, 2020 01:44
Add wide margins to a PDF for annotations
#!/bin/bash
if [ "$#" -ne 1 ]; then
echo "usage: $0 PDF_filename..."
echo
echo "This script takes a PDF file as command line arguments,"
echo "and generates a new, landscape-formatted PDF file, where every "
echo "page has very large margins which may be useful for editorial notes"
echo
echo "Requires: pdfjam"
echo
@algal
algal / ReverseH2.swift
Last active June 22, 2020 00:57
Deno and Swift scripts to reverse H2 headers in text
#!/usr/bin/swift
/*
Reads stdin, requiring UTF8-encoded text.
Parses it as initial text followed by a sequence of H2 markdown blocks.
Prints to stdout the initial text plus the H2 blocks in reversed order.
*/
import Foundation
@algal
algal / colorize-emacs.bashsource
Last active August 4, 2023 17:46
Setting up truecolor (24 bit color) in emacs in the terminal, under iTerm2, blink.sh, and others.
# sourcing this file will define a bash functions that
# tries to run subsequent calls to emacs with 24 bit color.
#
# It sets TERM=xterm-emacs-leg if
# - we've created a user-local terminfo record for xterm-emacs-leg, and
# - we're using iTerm2 or something has set COLORTERM=truecolor
#
# This will cause emacs to use 24 bit color only when it will work,
# inside or outside of tmux. I haven't found a way to auto-detect Blink.sh yet.
#
@algal
algal / fixssh-forwarding.el
Created February 20, 2020 01:41
Within emacs, fix ssh agent forwarding, which breaks in long-running tmux sessions
;; The purpose of this file is to define the function `fixssh-in-tmux`,
;; which attempts to fix ssh agent forwarding when it breaks within
;; a tmux session
;; from https://github.com/magnars/s.el/blob/master/s.el
(defun fixssh--s-match (regexp s &optional start)
"When the given expression matches the string, this function returns a list
of the whole matching string and a string for each matched subexpressions.
If it did not match the returned value is an empty list (nil).