Skip to content

Instantly share code, notes, and snippets.

View bencord0's full-sized avatar

Ben Cordero bencord0

View GitHub Profile
@bencord0
bencord0 / pvm-xen.sh
Created August 10, 2012 03:01
A neat little script to quickly spinup a Xen PV Gentoo guest (domU).
#!/bin/bash
if [ -z "$1" ]; then
echo "Please provide a name for the new vm"
exit 1
fi
export NAME="$1"
export UU=$(uuidgen)
echo $NAME $UU
@bencord0
bencord0 / concurrent_map.py
Created August 31, 2012 09:09
A multithreaded implementation of python's map().
def concurrent_map(func, *iterm, **kwargs):
"""
threaded version of map()
Usage:
>>> my_data = [1,2,3,4,5]
>>> def my_func(x):
... return x*x
...
>>> concurrent_map(my_func, my_data)
[1, 4, 9, 16, 25]
@bencord0
bencord0 / current_dir.py
Last active October 10, 2015 15:47
Find the directory that this file is in.
import os
current_dir = os.path.dirname(os.path.abspath(__file__))
@bencord0
bencord0 / widthprint.py
Created November 1, 2012 13:34
Like pprint, but prints horizontally.
def widthprint(lst, width=5):
for n, i in enumerate(lst):
if n % width == 0:
print('')
print i,
@bencord0
bencord0 / attempt_import.py
Created November 30, 2012 20:12
Requests should be part of the standard library.
try:
import requests
except ImportError:
def install_package(dep):
# If you don't have pip, you're screwed.
from pip.commands.install import InstallCommand
dep_l = [dep]
inst = InstallCommand()
opts, args = inst.parser.parse_args()
try:
@bencord0
bencord0 / listeningsockethandler.py
Created December 12, 2012 23:36
ListeningSocketHandler ====================== A python logging handler. logging.handlers.SocketHandler is a TCP client that sends log records to a TCP server. This class is the opposite. When a TCP client connects (e.g. telnet or netcat), new log records are sent through the tcp connection.
#!/usr/bin/env python
"""
ListeningSocketHandler
======================
A python logging handler.
logging.handlers.SocketHandler is a TCP Socket client that sends log
records to a tcp server.
This class is the opposite.
@bencord0
bencord0 / etc-pam.d-nginx
Created December 16, 2012 23:40
nginx pam (and thus kerberos) authentication in gentoo. Copied from /usr/portage/www-servers/nginx/nginx-1.2.5.ebuild tweaked to add USE=pam
# /etc/pam.d/nginx
auth required /lib/security/pam_krb5.so
account required /lib/security/pam_krb5.so
@bencord0
bencord0 / gist:4753968
Created February 11, 2013 11:29
Bootstrap python basics. Use when installing a fresh python installation (such as pypy) on a system that doesn't come with python. i.e. windows.
import os,
import urllib2
def run_webscript(script_url):
script_file = os.path.basename(script_url)
with urllib2.urlopen(script_url) as u:
with open(script_file, 'w') as s:
s.write(u.read())
execfile(script_file, dict(__file__=script_file))
@bencord0
bencord0 / gist:5124444
Created March 9, 2013 15:06
screen_it
function screen_it {
NL=`echo -ne '\015'`
SCREEN_NAME=${SCREEN_NAME:-screeened}
screen -S "$SCREEN_NAME" -X screen -t $1
sleep 1.5
screen -S "$SCREEN_NAME" -p $1 -X stuff "$2""$NL"
}
@bencord0
bencord0 / hello.py.asc
Last active December 15, 2015 04:29
Encrypted programming
#!/bin/bash
echo -n "Passphrase:"
read -sr p
gpg -d --batch --passphrase "$p" "$0" | python
exit $?
-----BEGIN PGP MESSAGE-----
Version: GnuPG v2.0.19 (GNU/Linux)