Skip to content

Instantly share code, notes, and snippets.

View davidh-raybeam's full-sized avatar

David Hollis davidh-raybeam

View GitHub Profile
@davidh-raybeam
davidh-raybeam / bar.rb
Created December 6, 2013 16:59
bar usage: bar foo
#! /usr/bin/env ruby
bar_char = '-'
lead_count = 5
cols = `tput cols`.strip.to_i
max_len = [cols - ((2 * lead_count) + 4), 0].max
bar_color = `tput setaf 3`
reset = `tput sgr0`
title = ARGV.join ' '
@davidh-raybeam
davidh-raybeam / avg_timespan.sql
Last active December 25, 2015 05:19
avg_timespan.sql is a Vertica SQL statement which will, given a table like the one in setup.sql, calculate the mean amount of time a user spends on our website before completing a purchase, plus its standard deviation. Note that very little effort has been made to optimize this operation.
SELECT
AVG(session_length) as mean_session_length,
STDDEV(session_length_seconds) as session_length_stddev
FROM (
SELECT
event_name,
MAX(action_timestamp) OVER(PARTITION BY user_hostname, pattern_id)
- MIN(action_timestamp) OVER(PARTITION BY user_hostname, pattern_id)
as session_length,
datediff('second',
@davidh-raybeam
davidh-raybeam / markup-problem.md
Created May 28, 2013 18:10
The problem with tabular data and markup languages

Part of the problem is that markup languages are inherently 1-dimensional (and the ones that aren't suck to actually write in), whereas tablular data is often 2+-dimensional. This problem is compounded if you want to represent anything that isn't strictly a plain m by n table (e.g., something with additional formatting or cells with non-1 column or row spans).

Frankly, I think the best way to make tables is to have a GUI that generates some machine-readable markup for the table (or even just an image of it). I'd like for there to be something like Grid that does this.

<%= <<WAT
<%= whatever %>
WAT %>
@davidh-raybeam
davidh-raybeam / .bashrc
Created April 3, 2013 20:15
make and change to a dir
# ...
mdcd () {
mkdir -p $1 && cd $1
}
# ...
@davidh-raybeam
davidh-raybeam / clip.py
Created March 28, 2013 20:03
These ruby and python scripts provide functions that allow you to copy their arguments to the clipboard.
# Put the following function definition into a file called ~/.pythonrc
# and add the line
# export PYTHONSTARTUP="$HOME/.pythonrc"
# to your .bash{rc,_profile}
import os
def clip(x):
os.popen('pbcopy','w').write(str(x))
@davidh-raybeam
davidh-raybeam / gist:5263794
Last active December 15, 2015 12:59
after pasting this into your terminal, you should be able to just "copy_key REMOTE_SERVER"
copy_key () {
local remote_server=$1
[[ -z "$1" ]] && echo "Must provide a server." 2>&1 && return 1
cd "$HOME"
cat .ssh/id_rsa.pub | ssh $remote_server "mkdir -p .ssh && cat - >>.ssh/authorized_keys2"
}
@davidh-raybeam
davidh-raybeam / .bashrc
Created March 20, 2013 19:32
A simple shell function to quickly stub out python packages.
pypkg () {
local pkgpath="$(echo $1 | sed 's:\.: :g')"
local fspath="."
for el in $pkgpath; do
fspath="${fspath}/${el}"
(mkdir "$fspath" && touch "${fspath}/__init__.py") &>/dev/null
done
}
@davidh-raybeam
davidh-raybeam / PL : NL ::
Last active December 15, 2015 04:09
A quick comparison of programming and natural languages.
syntactic atoms : vocabulary
language features and larger syntatctic constructs : grammar
idioms, patterns : idioms
3rd-party code : literature
source-code repositories : corpora
@davidh-raybeam
davidh-raybeam / gist:5162503
Created March 14, 2013 15:50
Apparently python actually creates local variables for list comprehensions. I always assumed that comprehension expressions created their own scope.
$ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 42
>>> l = [1,2,3,4,5]
>>> [x*x for x in l]
[1, 4, 9, 16, 25]
>>> x
5