Skip to content

Instantly share code, notes, and snippets.

@simonrad
simonrad / zshrc
Last active August 24, 2023 18:36
My zshrc shell setup file
# Set up custom zsh command prompt.
PROMPT='%(?.%F{green}√.%F{red}%?)%f %B%F{240}%1~%f%b %# '
# Aliases.
alias l='ls -G'
alias la='l -a'
alias ll='l -l'
alias lla='l -la'
alias b='cd ..'
@simonrad
simonrad / example_bash_script.sh
Last active March 2, 2019 03:04
Just some example bash code.
#!/usr/bin/env bash
cd `dirname "$0"` # cd to the directory containing this script.
cd `git rev-parse --show-toplevel` # cd to the root of the git repo.
set -e # Fail on errors.
set -x # Print commands.
# Some example code below:
@simonrad
simonrad / graph_helpers.py
Last active June 15, 2018 19:21
Code to help transform a DOT file (graph)
# This module is meant to be imported.
# You'll need to run:
# pip install pydot
'''
# Example usage:
import graph_helpers as gh
@simonrad
simonrad / diff.py
Last active April 23, 2018 03:35
Compute difference between two words.
from funcy import memoize
# Returns difference between two words.
# O(N^2) in space and time.
@memoize
def diff(a, b):
if a == "" and b == "":
return (0, "")
if a == "":
@simonrad
simonrad / run_bash_command.py
Last active August 23, 2017 01:20
Helper function to easily run a bash command and get its output
import subprocess
def run_bash_command(command):
'''
Returns the stdout of the command.
If the exit code is non-zero, raises a subprocess.CalledProcessError.
Note: If the command involves ssh, it's recommended that you pass the -T option to ssh, to prevent it from messing with your terminal modes.
'''
return subprocess.check_output(['bash', '-c', command]).strip()
@simonrad
simonrad / histogram.py
Created January 14, 2016 19:30
Python script to plot a histogram of a set of numbers.
#!/usr/bin/env python
'''
This script reads a set of numbers and plots a histogram.
'''
import sys
import matplotlib.pyplot as plt
import numpy as np
@simonrad
simonrad / stat_significance.py
Created September 1, 2015 05:15
Statistical Significance
from math import sqrt
from scipy.stats import norm
def normal_dist_params(num_trials, num_successes):
# Returns the mean and variance of the normal distribution approximation that models the actual success rate given the experiment results.
p = float(num_successes) / float(num_trials)
n = num_trials
mean = p
variance = p * (1-p) / n
return (mean, variance)
@simonrad
simonrad / shard_distribution_probability.py
Created February 25, 2015 01:05
shard_distribution_probability.py
# --------------------------------------------------
# Output:
# We randomly distribute T tasks across N shards.
# We compute the 95th percentile of (size of largest shard / size of average shard).
# 2 shards, 2 tasks = 2.00
# 2 shards, 8 tasks = 1.75
# 2 shards, 40 tasks = 1.30
# 2 shards, 200 tasks = 1.14
# 5 shards, 5 tasks = 3.00
# 5 shards, 20 tasks = 2.00
@simonrad
simonrad / gist:35b63bba9dc535b8cbcd
Created July 22, 2014 19:43
SourceTree custom actions
Cherry Pick without committing:
Script target: /usr/bin/env
Parameters: git cherry-pick --no-commit $SHA
Checkout working tree from commit:
Script target: /usr/bin/env
Parameters: git checkout $SHA -- $REPO
# bash/zsh completion support for core Git.
#
# Copyright (C) 2006,2007 Shawn O. Pearce <spearce@spearce.org>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
# Distributed under the GNU General Public License, version 2.0.
#
# The contained completion routines provide support for completing:
#
# *) local and remote branch names
# *) local and remote tag names