Skip to content

Instantly share code, notes, and snippets.

View bholt's full-sized avatar

Brandon Holt bholt

View GitHub Profile
@bholt
bholt / fmt.py
Last active February 7, 2016 00:08
String interpolation in Python 2 using `eval` and `inspect` to get caller's locals.
import inspect
import re
def fmt(s):
"""
Interpolate arbitrary expressions inside '{}' in strings (similar to Ruby or Scala).
It should go without saying that because this uses `eval`, it is unsafe to use with any user-provided inputs.
It is also probably terribly slow because it's using `inspect` to get the caller's local variables.
Example:
@bholt
bholt / .tmux.conf
Created October 28, 2015 00:45
Put this in your home directory (~/.tmux.conf). Make "ctrl+t" the global tmux shortcut, makes splits easier by creating shortcuts for "ctrl+t, -" to split horizontal and "ctrl+t, |" (pipe) for vertical.
# start a new session if there isn't one to attach to already
new-session
# Make global tmux shortcut "ctrl+t"
set -g prefix ^t
# Renumber windows when they're closed so you don't have weird missing numbers.
set-option -g renumber-windows on
# Make colors work right.
@bholt
bholt / slurm-ssh.sh
Created August 18, 2015 01:11
Slurm SSH Script
#!/bin/bash
# usage: slurm-ssh [host] [shell]
# e.g.: salloc -N2 slurm-ssh
# n01 (salloc n[01,04]) >
# or:
# slurm-ssh n01
# or:
# slurm-ssh n01 zsh
DIR=$(pwd)
@bholt
bholt / color_scheme.r
Created August 12, 2015 17:49
Custom color scheme for ggplot
c.blue <- "#0072B2"
c.yellow <- "#E69F00"
c.green <- "#009E73"
c.red <- "#D55E00"
c.pink <- "#CC79A7"
c.gray <- "#999999"
palette <- c(
'follow'=c.blue,
'newuser'=c.yellow,
@bholt
bholt / ConcurrentQueue.hpp
Created July 30, 2015 16:00
Concurrent queue built for single-producer/single-consumer that uses a condition variable to signal consumer.
#include <future>
using namespace std;
template< typename T >
class ConcurrentQueue {
Queue<T> q;
mutex m;
condition_variable cv;
bool closed;
@bholt
bholt / keybase.md
Created June 5, 2015 20:13
Keybase proof

Keybase proof

I hereby claim:

  • I am bholt on github.
  • I am bholt (https://keybase.io/bholt) on keybase.
  • I have a public key whose fingerprint is AEAA C7A8 DE91 8434 B88F 88CD EE4E 54E0 0859 5279

To claim this, I am signing this object:

@bholt
bholt / slurm-ssh
Created March 12, 2015 03:28
Ssh to first allocated node, passing Slurm environment variables through.
#!/bin/bash
# Usage: salloc <options> slurm-ssh
first=$(scontrol show hostname $SLURM_NODELIST | head -n1)
env=$(printenv | grep SLURM | sed -rn "s/=(.*)/='\1'/p" | paste -d' ' -s)
exec ssh $first -t "$env zsh"
template< typename T >
void work(T t) {
try { throw t; }
catch (int x) { printf("int\n"); }
catch (std::string x) { printf("string\n"); }
}
int main(int argc, char *argv[]) {
work(0);
@bholt
bholt / wc.cpp
Last active August 29, 2015 14:04
Word Count Example
forall(words, n, [=](string w) {
// hash partitioning
size_t idx = hash(w) % ncells;
delegate(cells+idx, [=](Cell& cell)
{ // runs atomically
if (cell.count(w) == 0) cell[w] = 1;
else cell[r] += 1;
});
});
@bholt
bholt / graphlab.cpp
Last active August 29, 2015 14:03
Lata code snippet
GlobalAddress<Graph> g;
///////////////////////////////////////////////
// GraphLab synchronous engine: scatter phase
// loop over all vertices in graph
forall(g, [=](Vertex& v){
// only scatter from vertices that have been signaled to do scatter
if (v->active_minor_step) {
v->active_minor_step = false;