Skip to content

Instantly share code, notes, and snippets.

View bschlenk's full-sized avatar

Brian Schlenker bschlenk

View GitHub Profile
@bschlenk
bschlenk / hashtree.py
Created December 26, 2016 08:07
Reorganize a directory into subfolders the same way git organizes its object files.
#!/usr/bin/env python3
"""
Reduce the size of a directory by creating new directories for each
unique combination of the first two letters of the existing files.
This is the same way that git stores object files.
For example, a file with this name would result in the following:
fff394e6-214d-4f74-8607-d6825f17c8fd
@bschlenk
bschlenk / truthgen.py
Created January 28, 2017 20:32
Generate all possible truth table combinations for the given number of inputs. Wrote for students in logic class.
#!/usr/bin/env python
import sys
if len(sys.argv) != 2:
print "usage: %s <number of inputs>" % sys.argv[0]
sys.exit(1)
inputs = int(sys.argv[1])
rows = 2 ** inputs
@bschlenk
bschlenk / ini2json.py
Last active March 3, 2017 13:56 — forked from Natim/ini2json.py
Convert an ini configuration file into a json file. Retains original order of ini file entries and prints json out with four space indentation.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import json
import sys
from collections import OrderedDict
from ConfigParser import (ConfigParser, MissingSectionHeaderError,
ParsingError, DEFAULTSECT)
@bschlenk
bschlenk / hideit.user.js
Last active March 4, 2018 04:03
Hide any element on the page by meta+alt clicking it. Return the page to normal by shift+meta+alt clicking. https://gist.github.com/bschlenk/a9b19645d6611705b26eb93e3c0288ac/raw/hideit.user.js
// ==UserScript==
// @name HideIt
// @namespace http://tampermonkey.net/
// @version 1.1.2
// @description Hide any element on the page, with the ability to restore all.
// @author Brian Schlenker <bschlenk@umich.edu>
// @match *://*/*
// @noframes
// ==/UserScript==
@bschlenk
bschlenk / say.js
Last active March 23, 2018 07:36
Call macOS's "say" program with every possible voice at the same time with the given phrase.
#!/usr/bin/env node
/**
* Call macOS's `say` program with every possible voice at the same
* time with the given phrase. Scare your pets.
*/
const { exec } = require('child_process');
function getVoices(callback) {
@bschlenk
bschlenk / sortBy.js
Created June 1, 2019 19:20
A function to easily sort JS objects by key.
/**
* Create a sorting function that can sort objects by a calculated key. Can be passed to `Array.sort`.
* @param cb A function that will be passed each object and expected to return a key to sort by.
* The key can be either a string, number, or an array of those.
* @return A function that can be passed to `Array.sort`.
*/
function sortBy(cb) {
const stringCompare = (a, b) => a.localeCompare(b);
const numberCompare = (a, b) => a - b;
@bschlenk
bschlenk / png2console
Created August 5, 2019 15:25
Generate a node.js script that prints an image to the console using chalk. Requires pillow.
#!/usr/bin/env python3
import sys
from itertools import islice
from PIL import Image
PIXEL = '▄'
# The amount an image needs to be scaled to look good in terminal
TERM_RATIO = 0.8276
@bschlenk
bschlenk / nvm-clean-install.sh
Last active December 10, 2020 01:38
Update Node, reinstall global packages, and remove older versions. To be placed in shell profile.
# Automate installing a new version of node, re-installing global
# node modules, and uninstalling older node versions.
#
# Depends on nvm, semver, and python3.
#
# semver will be auto-installed if it doesn't exist yet.
#
# If a single arg is given:
#
# 1. Install the new node version given as the first arg.
@bschlenk
bschlenk / columns.py
Created January 11, 2023 17:02
A columns function to format a list of object into terminal-printable columns. I end up writing something like this everywhere I've worked, saving it here so I can reuse it next time.
import os
def columns(data, *, delimiter, width=None, config):
'''
config is a list of objects with a `key`, and optionally a `colorize` and `truncate` method
'''
# start by extracting out all the columns & getting the width of each