Skip to content

Instantly share code, notes, and snippets.

View littlebenlittle's full-sized avatar
👋
Looking for gigs

Ben Little littlebenlittle

👋
Looking for gigs
View GitHub Profile
@littlebenlittle
littlebenlittle / input_seq.py
Last active February 27, 2020 01:03
Oveload the input() builtin with a generator of strings
import builtins
from unittest.mock import patch
class InputSeq():
'''Emit elements of a sequence by invoking __call__'''
def __init__(self, input_seq):
self._input_seq = input_seq.copy()
self._index = -1
def __call__(self, _=None):
@littlebenlittle
littlebenlittle / sync.sh
Last active November 17, 2019 16:11
Sync files to a cloud VM
LOCAL_DIR=$1
REMOTE_DIR=$2
DEV_IP=$3
while inotifywait -e modify -e create --exclude '/\..+' -r $LOCAL_DIR; do
rsync -avz --exclude '/\..+' $LOCAL_DIR $USER@$DEV_IP:$REMOTE_DIR
done
@littlebenlittle
littlebenlittle / parseEnv.go
Last active November 10, 2019 15:12
env parsing in various languages
func parseEnv() (map[string]string) {
pass := true
m := make(map[string]string)
for _, k := range []string{
"GRPC_PORT",
"GOOGLE_CLIENT_ID",
"GOOGLE_CLIENT_SECRET",
"CERTFILE",
"KEYFILE",
} {
openssl req -new -newkey rsa:2048 -days 365 -nodes -x509 \
-subj "/C=US/ST=TEST/O=TEST/O=TEST/CN=localhost" \
-passout pass:abc123 \
-out $PWD/server.crt \
-keyout $PWD/server.key
%display latex
def get_vecs(n):
'''Get the orthonormal basis vectors of RR^n'''
vecs = []
for i in range(n):
vecs.append([1 if x == i else 0 for x in range(n)])
return vecs
def apply_permutation(lst, perm):
" Prevent <esc>+[key] from generating a foreign character
" Credit to https://github.com/neovim/neovim/issues/2093#issuecomment-135685601
set ttimeout
set ttimeoutlen=0
set matchtime=0
@littlebenlittle
littlebenlittle / api.py
Created April 22, 2019 16:29
Handling multiple routes with App Engine python37 runtime
from flask import Flask, request
import sys
app = Flask(__name__)
@app.route('/api/test', methods=['GET', 'POST'])
def test():
content = '<p>handler: ' + sys._getframe(0).f_code.co_name + \
'</p>\n<ul>\n'
" set the active tmux pane for the current buffer
nnoremap <leader>0 :let b:activepane = 0<cr>
nnoremap <leader>1 :let b:activepane = 1<cr>
nnoremap <leader>2 :let b:activepane = 2<cr>
nnoremap <leader>3 :let b:activepane = 3<cr>
nnoremap <leader>4 :let b:activepane = 4<cr>
" send the current line to the active pane
nnoremap <leader><cr> :silent :execute ':.w !cat \| (read cmd; tmux send-keys -t' b:activepane ' "$cmd" enter)'<cr>
import numpy as np
# The transition matrix. Notice that each column sums to 1
T = np.array([[0.25, 0.33, 0.3 ],
[0.5 , 0.66, 0.2 ],
[0.25, 0.01, 0.5 ]])
# The state vector. We are initially in the first state with 100% probability
s = np.array([1.0, 0.0, 0.0])
@littlebenlittle
littlebenlittle / peano-nats.hs
Created July 30, 2018 17:38
Peano's Axioms in Haskell
-- A straightforward implementation of Peano's Axioms for the Natural Numbers in Haskell.
-- This is intended as a demonstration of similarity between functional code and its equivalent in (informal) first order logic
import Prelude hiding ( (+), (*) )
-- There exists a set Nat with the distinguished element Zero and the function Suc, such that if n is in Nat, then Suc(n) is in Nat
data Nat = Zero | Suc Nat deriving (Eq, Show)
-- There exists a binary function + on (Nat x Nat) such that