Skip to content

Instantly share code, notes, and snippets.

def bool_array_find_clusters_and_islands(bool_array):
clusters = []
islands = []
cluster_start_index = None
currently_iterating_through_cluster = False
for i,value in enumerate(bool_array):
value = bool(value)
if currently_iterating_through_cluster and (value is True):
if i == len(bool_array)-1: # if last element
cluster_end_index = i
@simonLeary42
simonLeary42 / simplify.py
Last active November 3, 2023 06:29
simplify math expressions with sympy
import sympy
import readline # allow arrow keys while typing inputs
while True:
expression = input("expression: ")
unknowns = input("unknowns comma delimited: ")
exec_str = f"{unknowns} = sympy.symbols(\"{unknowns}\")"
print(f"> {exec_str}")
exec(exec_str)
@simonLeary42
simonLeary42 / fmt-table.py
Created October 17, 2023 22:22
`tabulate` but worse
def fmt_table(table, disable_column_headers=False) -> str:
"""
I would use tabulate but I don't want nonstandard imports
"""
table_output = ""
# no row has more elements than the header row
assert(all(len(row) <= len(table[0]) for row in table))
column_widths = [ 0 ] * len(table[0])
for row in table:
for i,element in enumerate(row):
#!/usr/bin/env python3
import os
import sys
import glob
import zipfile
import tempfile
from subprocess import check_output, CalledProcessError
assert len(sys.argv) >= 3
grep_args = ' '.join(sys.argv[1:-1])
@simonLeary42
simonLeary42 / critical-K-region-routh-herwitz.py
Created October 16, 2023 07:07
Given the coeficients of the denominator of the transfer function with respect to K, find the region of K in which the system is stable
from collections import deque
def round_up(x):
return round(x + 0.4999999)
def make_routh_table(coefs: list):
num_columns = round_up(len(coefs)/2)+1
num_rows = num_columns*2
routh_table = [[0]*num_columns for x in range(num_rows)]
i = 0 # column
@simonLeary42
simonLeary42 / list-exe.sh
Last active October 14, 2023 21:55
# list .exe files in $1/* # if one of those directories contains nothing but another subdirectory, # then list .exe files in $1/dir/subdir
#!/bin/bash
# list .exe files in $1/*
# if one of those directories contains nothing but another subdirectory,
# then list .exe files in $1/dir/subdir
function fail(){
echo $@ >&2
exit 1
}
function list_exe(){
import sys
import socket
import subprocess
LOOKUPS = [
"unity.rc.umass.edu.",
"ood.unity.rc.umass.edu.",
"docs.unity.rc.umass.edu.",
]
@simonLeary42
simonLeary42 / slurm-nodes-usage
Last active December 26, 2023 06:09
like `htop` but for all the nodes in a slurm cluster
#!/usr/bin/env python3
import os
import grp
import sys
import json
import shutil
import subprocess as subp
from typing import List
SINFO_CACHE_FILE_PATH="/modules/user-resources/cache/sinfo.json"
@simonLeary42
simonLeary42 / firefox-toggle-config-option.sh
Last active September 28, 2023 16:59
Script to toggle the firefox `mousewheel.default.delta_multiplier_y` setting between two values. This is useful when I want one scroll sensitivity for my touchpad and another sensitivity for my mouse.
#!/bin/bash
set -eo pipefail
SETTING="mousewheel.default.delta_multiplier_y"
CONFIG_FILE_PATH="/home/simon/.mozilla/firefox/shlbuz9i.default-esr/prefs.js"
LOW="30" # comfortable with my macbook 2015 touchpad
HIGH="101" # 100 is default, firefox deletes default values from the file
firefox_found=$(pgrep -U "$(whoami)" -fx '(.*/)?firefox(-esr)?$' | uniq || true)
@simonLeary42
simonLeary42 / .gitlab-ci.yml
Last active August 1, 2023 22:37
general purpose GitLab CI style check with prettier, plus local shell script to format with prettier. This ensures that the local formatting and the remote style check use the same version, and little effort is required on the local end
prettier_style_check:
image: node:latest
stage: test
script:
- npm install -g prettier
- prettier --version
- prettier . --check || { echo "did you run \`format.sh\`?"; exit 1; }