Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am mpapi on github.
  • I am mpapi (https://keybase.io/mpapi) on keybase.
  • I have a public key whose fingerprint is 3C09 98F5 430D BAA5 04A7 3719 A5C6 24C8 3FA3 0ADB

To claim this, I am signing this object:

@mpapi
mpapi / password.sh
Created January 29, 2013 03:38
Generates a random password, after http://xkcd.com/936/.
#!/bin/bash
#
# Generates a random password, after http://xkcd.com/936/.
#
# -f [words file, one per line; default /usr/share/dict/words]
# -w [number of words; default 3]
# -n [min number of characters per word; default 6]
# -x [max number of characters per word; default 10]
#
@mpapi
mpapi / whenever.sh
Last active March 14, 2023 12:46
A simple shell script that uses inotify in Linux to run shell commands whenever files matching a pattern are changed.
#!/bin/sh
#
# Usage: whenever.sh [pattern] [command]
#
# Executes a command whenever files matching the pattern are closed in write
# mode. "{}" in the command is replaced with the matching filename (via xargs).
# Requires inotifywait from inotify-tools.
#
# For example,
@mpapi
mpapi / dates.sh
Created September 25, 2011 02:49
A quick shell script to generate ranges of dates
#!/bin/bash
LIMIT=${1:-1}
FORMAT=${2:-"%Y-%m-%d"}
if [[ $LIMIT -lt 0 ]]
then
SEQ=$(seq 0 -1 $(( $LIMIT + 1 )))
else
SEQ=$(seq 0 $(( $LIMIT - 1 )))
@mpapi
mpapi / batch.sh
Created September 25, 2011 02:47
A quick shell script to run commands in parallel batches
#!/bin/bash
COMMAND=$1
PARALLEL=${2:-2}
while read cmd
do
echo "'$(echo $COMMAND | sed "s/{}/$cmd/g")'"
done | xargs -n1 -P$PARALLEL sh -c
@mpapi
mpapi / cached.py
Created May 1, 2011 22:05
cached: Python contextmanager for cached URL fetches
from contextlib import closing, contextmanager
from urllib2 import urlopen
import hashlib
import os
import time
@contextmanager
def cached(url, expires=0, root='/tmp'):
'''
Returns the contents of the URL through a file-based cache. If "expires" is