Skip to content

Instantly share code, notes, and snippets.

@simonrad
simonrad / rename_files.py
Created February 16, 2012 22:05
Regex File-Renaming Utility
#!/usr/bin/python
"""
This is a command-line batch file-renaming utility.
It uses regular expressions to transform filenames.
This is especially helpful on Mac since Mac OS X does not include the Linux 'rename' command.
Run with no arguments for help info.
Author: Simon Radford (simonradf@gmail.com)
"""
@simonrad
simonrad / allow_staff_access.sh
Created July 1, 2013 00:34
Script to grant R/W permissions to staff
#!/bin/bash
# --------------------------------------------------
# This script grants READ AND WRITE permissions to staff, for the specified directory.
# Takes 1 parameter ($1): A directory.
# Recursively changes the directory to have group=staff,
# and adds read and write permissions for that group.
# Also adds execute permissions for all directories (but not files).
@simonrad
simonrad / bash_profile
Last active March 15, 2019 20:11
A basic shell setup file
# Set up custom bash command prompt.
# Default is "\h:\W \u\$ ".
RED="\[\e[0;31m\]"
GREEN="\[\e[0;32m\]"
BLUE="\[\e[0;34m\]"
PLAIN="\[\e[m\]"
PS1="${RED}\u@\h: \w\$ ${PLAIN}"
# Aliases.
@simonrad
simonrad / monit_cron.py
Last active January 7, 2018 21:49
A cron script to monitor and restart a set of processes.
#!/usr/bin/env python
"""
This script monitors a set of processes, (re)starting them if they are not already running.
This script is meant to be called periodically as a cron job.
Example crontab entry:
* * * * * $HOME/bin/python $HOME/misc/monit_cron.py 1>>$HOME/logs/cron/monit/monit_out.log 2>>$HOME/logs/cron/monit/monit_err.log
0 * * * * mv $HOME/logs/cron/monit/monit_out.log $HOME/logs/cron/monit/monit_out.log.1
0 * * * * mv $HOME/logs/cron/monit/monit_err.log $HOME/logs/cron/monit/monit_err.log.1
@simonrad
simonrad / setup_logging.py
Last active December 20, 2015 10:39
Snippet to setup basic logging in Python.
import logging
import os
import sys
import time
LOG_FORMAT = '%(asctime)s %(levelname)-8s %(message)s'
DATE_FORMAT = '%m/%d %H:%M:%S %Z'
# --------------------------------------------------
@simonrad
simonrad / iterm_key_bindings.txt
Last active May 16, 2023 18:18
My iTerm2 custom key bindings
iTerm2 Key Bindings
===================
Fast way
--------
1. Run this command:
echo '{"Touch Bar Items":[],"Key Mappings":{"0xf700-0x260000":{"Action":10,"Text":"[1;6A"},"0x37-0x40000":{"Action":11,"Text":"0x1f"},"0x32-0x40000":{"Action":11,"Text":"0x00"},"0xf709-0x20000":{"Action":10,"Text":"[17;2~"},"0xf70c-0x20000":{"Action":10,"Text":"[20;2~"},"0xf729-0x20000":{"Action":10,"Text":"[1;2H"},"0xf72b-0x40000":{"Action":10,"Text":"[1;5F"},"0xf705-0x20000":{"Action":10,"Text":"[1;2Q"},"0xf703-0x260000":{"Action":10,"Text":"[1;6C"},"0xf700-0x220000":{"Action":10,"Text":"[1;2A"},"0xf701-0x280000":{"Action":11,"Text":"0x1b 0x1b 0x5b 0x42"},"0x6c-0x100000":{"Text":"[C","Action":10},"0x38-0x40000":{"Action":11,"Text":"0x7f"},"0x33-0x40000":{"Action":11,"Text":"0x1b"},"0xf703-0x220000":{"Action":10,"Text":"[1;2C"},"0xf701-0x240000":{"Action":10,"Text":"[1;5B"},"0x4c-0x160000":{"Text":"","Action":34},"0xf70d-0x20000":{"Action":10,"Text":"[21;2~"},"0xf702-0x260000":{"Action":10,"Text":"[1;6D"},"0xf729-0x40000":{"
@simonrad
simonrad / cubing.py
Last active December 30, 2015 18:29
A webserver to scramble a Rubik's cube.
#! /usr/bin/env python
"""
A webserver to scramble a Rubik's cube.
It renders a sequence of moves which you should perform to scramble your cube.
The generated scrambles are perfectly, uniformly random.
"""
import sys
import logging
@simonrad
simonrad / setup_git_config.sh
Created December 10, 2013 03:16
Script to setup a good git config.
#!/bin/bash
git config --global user.name "Simon Radford"
git config --global user.email "simonradf@gmail.com"
git config --global color.ui true
git config --global merge.conflictstyle diff3
git config --global diff.renames true
git config --global push.default simple
git config --global alias.st status
git config --global alias.co checkout
@simonrad
simonrad / silence_pagerduty_temporarily.py
Last active August 29, 2015 14:00
Script to temporarily hush Pagerduty alerts.
"""This script will periodically poll pagerduty for triggered incidents, and acknowledge them."""
import urllib2
import time
import json
import sys
import getpass
if __name__ == '__main__':
@simonrad
simonrad / fix_merge.py
Last active August 29, 2015 14:00
A script framework for programmatically resolving merge conflicts.
'''
This script is a framework for programmatically resolving merge conflicts in a text file.
You should overwrite resolve_conflict() with your own logic.
Assumes the conflicts were generated inline by git, with the diff3 conflictstyle.
To enable the diff3 conflictstyle, run this command:
git config --global merge.conflictstyle diff3
Backup your source file before running with "overwrite" mode enabled.