Skip to content

Instantly share code, notes, and snippets.

View bsmithyman's full-sized avatar
📎
"It looks like you're writing a distributed web app!"

Brendan Smithyman bsmithyman

📎
"It looks like you're writing a distributed web app!"
View GitHub Profile
@bsmithyman
bsmithyman / JustTheNumbers
Last active December 17, 2015 17:39
Python gist to take a complicated nested object and grab a list of numbers from it.
import re
parser = re.compile('(?:[0-9\.]+)')
lambda x: [float(item) for item in parser.findall(repr(x))]
amodulename = 'modulename'
try:
import pyximport
pyximport.install()
except:
print('Cython import failed; {0} will use the legacy (pure Python) mode.'.format(amodulename))
from modulenamevanilla import class1, class2, class3
else:
try:
@bsmithyman
bsmithyman / uniquerefs.sh
Created July 30, 2013 23:49
Takes a LaTeX *.aux file as its first argument. Outputs a sorted, unique list of all citekeys from the original document. Useful for e.g., copying formatted bibliographic entries to a new document manually. Written when I was taking Chapter 5 of my PhD thesis and turning it into a paper.
#!/bin/sh
< $1 grep \\citation | cut -c 10- | sed -e "s/[{},]/\n/g" | sed -e "/^$/d" | sort -u
@bsmithyman
bsmithyman / dictate.sh
Last active August 29, 2015 14:07
Dictate a routing table for a hostname on the network
#!/bin/bash
HOST=$1
TABLE=$2
IP=$(getent hosts $HOST | cut -d \ -f 1)
CURRENT=$(ip rule | grep $IP | cut -d \ -f 4)
PRIORITY=1000
if [ $CURRENT ]; then
echo Resetting rule for $HOST with IP $IP
@bsmithyman
bsmithyman / up.sh
Created October 5, 2014 04:20
OpenVPN up script to add route to custom table
#!/bin/bash
TUNNEL="tun0"
TABLE="tablename"
OUTADDR=$(ip addr | grep $TUNNEL | sed -nr 's/.*peer ([0-9]+\.[0-9]+\.[0-9]+\.[0-9]+).*/\1/p')
ip route add default via $OUTADDR dev $TUNNEL table $TABLE
@bsmithyman
bsmithyman / down.sh
Created October 5, 2014 04:21
OpenVPN down script to remove custom entry in table
#!/bin/bash
TUNNEL="tun0"
TABLE="tablename"
ROUTE=$(ip route list table $TABLE)
ip route del $ROUTE table $TABLE
@bsmithyman
bsmithyman / dropbox
Last active August 29, 2015 14:07 — forked from nyarla/dropbox
#!/bin/sh
# /etc/init.d/SpiderOak
### BEGIN INIT INFO
# Provides: SpiderOsk
# Required-Start: $network $syslog $remote_fs
# Required-Stop: $network $syslog $remote_fs
# Should-Start: $named $time
# Should-Stop: $named $time
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
@bsmithyman
bsmithyman / cylon.py
Last active August 29, 2015 14:09
Python infinite iterator for use with map(...)
cylon = lambda plan: iter(lambda: plan, None)
if __name__ == '__main__'
for i, skinjob in enumerate(cylon(6)):
print('%d:\t%d'%(i+1, skinjob))
if i >= 11:
break
@bsmithyman
bsmithyman / matchdelete.py
Created December 5, 2014 15:45
Remove conflicted Dropbox files from a directory
import os
def findMatches(source, substr):
matches = []
for root, dirnames, filenames in os.walk(source):
for filename in filenames:
if filename.find(substr) > -1:
matches.append(os.path.join(root, filename))
return matches
@bsmithyman
bsmithyman / reducer.py
Last active August 29, 2015 14:14
Common reducer object. This is a subclass of dict that may be particularly useful when accumulating results on remote workers in a parallel execution context, then doing reduce operations on these results.
class CommonReducer(dict):
'''
Object based on 'dict' that implements the binary addition (obj1 + obj1) and
accumulation (obj += obj2). These operations pass through to the entries in
the commonReducer.
Instances of commonReducer are also callable, with the syntax:
cr(key, value)
this is equivalent to cr += {key: value}.
'''