Skip to content

Instantly share code, notes, and snippets.

def notify(title, subtitle=None):
"""Display a NSUserNotification on Mac OS X >= 10.8"""
from objc import lookUpClass
NSUserNotification = lookUpClass('NSUserNotification')
NSUserNotificationCenter = lookUpClass('NSUserNotificationCenter')
if not NSUserNotification or not NSUserNotificationCenter:
return
notification = NSUserNotification.alloc().init()
notification.setTitle_(str(title))
@rcaldwel
rcaldwel / gist:3829664
Created October 3, 2012 20:31
for curt
#!/usr/bin/env python
import os
dirs = ['/Users/rocaldwe/temp',
'/Users/rocaldwe/python',
'/Users/rocaldwe/bash']
found = False
@rcaldwel
rcaldwel / cache2ram.sh
Created August 9, 2012 00:28
bash: ram disk and trap shutdown
#!/bin/bash
# Auto-filled variables
USER_NAME=$(/usr/bin/who am i | /usr/bin/awk '{print $1}')
SELF=$(/usr/bin/basename $0)
USER_ID=$(id | sed 's/uid=\([0-9]*\).*/\1/')
OUR_PID=$(ps -ef | grep ${SELF} | grep -v 'grep' | awk -v uid="$USER_ID" '$1==uid {print $2; exit;}') # Only first occurence from the current user.
# Constants
@rcaldwel
rcaldwel / multiprocessing.py
Created August 8, 2012 14:50
python: multiprocessing example
#!/usr/bin/env python
import multiprocessing
import os
import requests
########################################################################
class MultiProcDownloader(object):
"""
Downloads urls with Python's multiprocessing module
@rcaldwel
rcaldwel / gist:3213143
Created July 31, 2012 02:59
bash: unix: applescript: set a bash var to the output of a scpt
the_app=$(
osascript 2>/dev/null <<EOF
tell application "System Events"
name of first item of (every process whose frontmost is true)
end tell
EOF
@rcaldwel
rcaldwel / gist:3171677
Created July 24, 2012 18:28
python: find a key in a dict
def find_key(dic, val):
"""return the key of dictionary dic given the value"""
return [k for k, v in dic.iteritems() if v == val][0]
@rcaldwel
rcaldwel / gist:3171123
Created July 24, 2012 16:47
python: search for a value in a dictionary
def find_key(dic, val):
"""return the key of dictionary dic given the value"""
return [k for k, v in symbol_dic.iteritems() if v == val][0]
{
1: '128.0.0.0', 2: '192.0.0.0',
3: '224.0.0.0', 4: '240.0.0.0',
5: '248.0.0.0', 6: '252.0.0.0',
7: '254.0.0.0', 8: '255.0.0.0',
9: '255.128.0.0', 10: '255.192.0.0',
11: '255.224.0.0', 12: '255.240.0.0',
@rcaldwel
rcaldwel / gist:3143631
Created July 19, 2012 12:57
unix: awk: awk getline example
dig unix.stackexchange.com | awk '/^;; ANSWER SECTION:$/ { getline ; print $5 }'
@rcaldwel
rcaldwel / isalive
Created July 18, 2012 00:30
unix: take host list from stdin and check ping
#!/bin/bash
# take a list of hosts by stdin and ping them
while read line; do
RESULT=`ping -c1 $line >/dev/null 2>&1 || echo 'fail'`
if [ "$RESULT" = "" ]; then
RESULT="pass"
fi
echo "$line -> $RESULT"
done
@rcaldwel
rcaldwel / gist:3132687
Created July 17, 2012 22:54
unix: ping test
ping -c1 ens-sj1 >/dev/null 2>&1 || echo 'fail'