Skip to content

Instantly share code, notes, and snippets.

@erynofwales
erynofwales / gist:933248
Created April 20, 2011 22:39
Get absolute path of directory for running script
BASE=$(cd "$(dirname "$0")" && pwd)
@erynofwales
erynofwales / brew doctor
Created August 8, 2011 20:10
Homebrew ledger install error
2236 13:08:37 ~ % brew doctor
Your OS X is ripe for brewing.
Any troubles you may be experiencing are likely purely psychosomatic.
@erynofwales
erynofwales / rotates.c
Created September 8, 2011 06:43
Do bit rotations with bit shifts in C
/* Do bit rotations. x is the data to rotate, c is the number of bits to rotate */
#define ROTATE_R(x,c) (x) = ((x) >> c) | ((x) << (sizeof(x) - c))
#define ROTATE_L(x,c) (x) = ((x) << c) | ((x) >> (sizeof(x) - c))
@erynofwales
erynofwales / gist:1204469
Created September 8, 2011 19:46
Tell branch foo to track upstream/foo
git branch --set-upstream foo upstream/foo
@erynofwales
erynofwales / gist:1207439
Created September 9, 2011 21:56
Pipe output of a command, newline separated, through a loop. This only works on KSH derivatives, including ZSH.
command | while read x; do [some stuff on $x]; done
@erynofwales
erynofwales / gist:1207898
Created September 10, 2011 03:58
Creating temporary directories in Cocoa/Objective-C
// Create a unique temporary direction
// This procedure is safe from a number of potential problems including
// concurrency, file permission, security, and persistence issues
// Taken from http://cocoawithlove.com/2009/07/temporary-files-and-folders-in-cocoa.html
NSString *tmpdirTemplate = [NSTemporaryDirectory()
stringByAppendingPathComponent:@"app.XXXXXX"];
const char *tmpdirTemplateCStr = [tmpdirTemplate fileSystemRepresentation];
char *tmpdirCStr = (char *) malloc(strlen(tmpdirTemplateCStr) + 1);
strcpy(tmpdirCStr, tmpdirTemplateCStr);
@erynofwales
erynofwales / gist:1233449
Created September 21, 2011 22:00
PowerShell: Get list of commands in a particular module
$module = 'name.of.module'
Get-Command | Where { $_.ModuleName -Eq $module }
@erynofwales
erynofwales / gist:1340439
Created November 4, 2011 20:45
Get all directories in current directory (except .)
find . -maxdepth 1 -type d -print | sed '/^.$/d'
@erynofwales
erynofwales / gist:1424249
Created December 2, 2011 18:17
Quick setup of the root logger for Python's logging module
loghandler = logging.StreamHandler(sys.stdout)
loghandler.setFormatter(logging.Formatter(
fmt='%(asctime)s:%(name)s:%(levelname)s: %(message)s',
datefmt='%Y-%m-%d %H:%M:%S'))
rootlog = logging.getLogger()
rootlog.addHandler(loghandler)
rootlog.setLevel(logging.DEBUG)
@erynofwales
erynofwales / xmldict.py
Created April 14, 2012 02:59
Small class to convert XML data to a dictionary
from io import StringIO
from xml.etree.ElementTree import ElementTree, Element, parse, ParseError
class XMLDict(dict):
'''A dictionary-like object to represent XML data.'''
def __init__(self, xml=None):
'''Expects a string containing XML data.'''
try:
self.populate(parse(StringIO(unicode(xml))).getroot())