March 2011
1 post
4 tags
Insufficiently known POSIX shell features →
I’ve seen several articles in the past with titles like “Top 10 things you didn’t know about bash programming.” These articles are disappointing on two levels: first of all, the tricks are almost always things I already knew. And secondly, if you want to write portable programs, you can’t depend on bash features (not every platform has bash!). POSIX-like shells,...
Mar 7th
18 notes
September 2009
1 post
5 tags
Extract & mirror cache url's from google search...
Saved search pages go in, cache links come out. It’s handy for mirroring a dead site by using site:domain.com as the search parameter. Notes: Without rate limiting I was blocked after request #169. However, there were no issues when using the limits below. The wait time can probably go much lower though. The empty user-agent is required for wget to work. pcregrep -hoM...
Sep 2nd
18 notes
July 2009
4 posts
3 tags
Set mp3 date tag as YYYY-MM-DD based on mtime
Particularly useful for large, poorly tagged radio archives mirrored with wget. Processes files in the working directory matching the patters specified. Requires: mutagen (for tagging) for FILE in *.mp3; do mid3v2 --date=$(perl -e '@d=localtime ((stat(shift))[9]); printf "%4d-%02d-%02d", $d[5]+1900,$d[4]+1,$d[3]' $FILE) $FILE; done
Jul 25th
8 notes
2 tags
dirty →
“A simple and dirty HTML/XML template library for Python 3.” >>> from dirty.html import * >>> page = xhtml( ... head( ... title("Dirty"), ... meta(name="Author", content="Hong, MinHee <minhee@dahlia.kr>") ... ), ... body( ... h1("Dirty"), ... p("Dirty is a simple DSEL template library that...") ... ) ... ) >>>...
Jul 25th
2 notes
1 tag
Time formatting in Haskell
showTime :: Int -> Int -> String showTime hours minutes | hours == 0 = "12" ++ ":" ++ showMin ++ " am" | hours <= 11 = (show hours) ++ ":" ++ showMin ++ " am" | hours == 12 = (show hours) ++ ":" ++ showMin ++ " pm" | otherwise = (show (hours - 12)) ++ ":" ++ showMin ++ " pm" where showMin | minutes < 10 = "0" ++ show minutes |...
Jul 14th
1 note
2 tags
“Command name is 25% fewer characters to type! Save days of free-time! Heck, it’s...”
– Why use ack? (via lloyda2)
Jul 7th
5 notes
June 2009
2 posts
1 tag
Python integer gotcha →
>>> a = 500 >>> b = 500 >>> a is b False >>> c = 200 >>> d = 200 >>> c is d True “Can you surmise why this inconsistency happens?”
Jun 22nd
1 tag
Disable the new look in Safari 4
The new tabs in Safari 4 are nice, but I prefer the old classic look. If you’re like me, like I know I am, use these commands in the terminal to disable the new look: defaults write com.apple.Safari DebugSafari4TabBarIsOnTop -bool NO defaults write com.apple.Safari DebugSafari4IncludeToolbarRedesign -bool NO defaults write com.apple.Safari DebugSafari4LoadProgressStyle -bool NO
Jun 6th
1 note
April 2009
2 posts
1 tag
Python: cached generator →
“A generator and a list used like a cache.” class GeneratorList(object): def __init__(self, generator): self.__generator = generator self.__list = [] def __getitem__(self, index): for _ in range(index - len(self.__list) + 1): self.__list.append(self.__generator.next()) return self.__list[index]
Apr 25th
1 note
1 tag
Handling Python Docstring Indentation  →
def trim(docstring): if not docstring: return '' lines = docstring.expandtabs().splitlines() # Determine minimum indentation (first line doesn't count): indent = sys.maxint for line in lines[1:]: stripped = line.lstrip() if stripped: indent = min(indent, len(line) - len(stripped)) # Remove indentation (first line is special): ...
Apr 9th
March 2009
1 post
2 tags
MacPorts garbage collection →
$ sudo su - # du -sh /opt 6.4G /opt # port clean -f --all installed ---> Cleaning apr ---> Cleaning apr-util ... # du -sh /opt 6.3G /opt # port -f uninstall inactive ---> Uninstalling autoconf @2.62_0 ---> Uninstalling automake @1.10.1_0 ... # du -sh /opt 5.4G /opt
Mar 11th
January 2009
8 posts
1 tag
Remove empty directories →
find . -type d -empty -exec rmdir {} \;
Jan 28th
3 notes
1 tag
TinyP2P: A 15 line long Python P2P Application
Via nosmo: # tinyp2p.py 1.0 (documentation at http://freedom-to-tinker.com/tinyp2p.html) import sys, os, SimpleXMLRPCServer, xmlrpclib, re, hmac # (C) 2004, E.W. Felten ar,pw,res = (sys.argv,lambda u:hmac.new(sys.argv[1],u).hexdigest(),re.search) pxy,xs = (xmlrpclib.ServerProxy,SimpleXMLRPCServer.SimpleXMLRPCServer) def ls(p=""):return filter(lambda n:(p=="")or res(p,n),os.listdir(os.getcwd()))...
Jan 27th
6 notes
1 tag
pickling: converting a Python object into a byte... →
>>> stuff = { ... 'hello': 'world', ... 'numbers': range(3), ... } >>> import pickle >>> data = pickle.dumps(stuff) >>> len(data) 63 >>> print data (dp0 S'hello' p1 S'world' p2 sS'numbers' p3 (lp4 I0 aI1 aI2 as. >>> pickle.loads(data) {'hello': 'world', 'numbers': [0, 1, 2]}
Jan 25th
1 tag
Extended slices in Python
To access even-indexed elements of a list, use the extended slice feature of Python. >>> range(20)[::2] [0, 2, 4, 6, 8, 10, 12, 14, 16, 18] This can be used to access every third element by using ::3 and so on. To reverse a list, use the extended slice with an argument of -1. >>> range(8)[::-1] [7, 6, 5, 4, 3, 2, 1, 0]
Jan 13th
1 tag
Disable the 3D-dock in Leopard
If you hate the fancy 3D-dock in Leopard, or just think that it doesn’t match your current wallpaper (this happens for me a lot), open a Terminal and type these: defaults write com.apple.dock no-glass -boolean YES killall Dock
Jan 13th
1 tag
Quake3's Fast Inverse Square Root Function
Oh my: float InvSqrt (float x){ float xhalf = 0.5f*x; int i = *(int*)&x; i = 0x5f3759df - (i>>1); x = *(float*)&i; x = x*(1.5f - xhalf*x*x); return x; } According to one of the guys supposedly responsible for this (arguably) legendary piece of code, “it’s just Newton-Raphson iteration with a very clever first approx.” And if you’re...
Jan 13th
1 tag
Getting the current system time in milliseconds... →
Jan 13th
1 tag
bash: mkdir + cd with one command
function mkcd() { [ -n "$1" ] && mkdir -p "$@" && cd "$1"; } Example: user@host:~$ cd work user@host:~/work$ mkcd website.com/{html,src,stuff} user@host:~/work/website.com/html$
Jan 3rd
December 2008
12 posts
1 tag
Set Dock switching mode to 'hide others' →
To enable this switching mode, open Terminal and type these commands: $ defaults write com.apple.dock single-app -bool TRUE $ killall Dock From now on, clicking on an application in the Dock will hide all other open apps while switching to the selected application. You will not see this behavior if you use Command-Tab to switch, or click directly on another application’s windows.
Dec 31st
1 tag
The C Standard Library →
Short and sweet.
Dec 29th
1 tag
re-using SSH connections →
The ssh ControlMaster setting “allows you to re-use an existing SSH connection whenever you connect to a host you are already connected to.” In your ~/.ssh/config file, add: Host * ControlMaster auto ControlPath /tmp/%r@%h:%p Whenever you connect to server.example.com as user joeuser, SSH will create a named pipe at /tmp/joeuser@server.example.com:22. If you open another...
Dec 29th
2 tags
sweet ass-regex
My tribute to xkcd #37 — this regular expression changes “sweet-ass x” to “sweet ass-x”. s/(\bsweet)[- ](ass)\s(\w)/$1 $2-$3/i
Dec 28th
11 notes
1 tag
retry the previous command with sudo →
$ shutdown -c shutdown: Need to be root $ sudo !! shutdown: Shutdown cancelled
Dec 28th
3 tags
> cd to ... →
For OS X. Open a Terminal window from Finder.
Dec 28th
2 notes
2 tags
Breakdown of the 'scpr' alias
As a follow-up to ‘making rsync easy’, here’s a breakdown of what the options do. -P — Shows the progress of the transfer, and keeps partially transferred files. Useful if you’re on an unstable connection, as you can just run the same command again if the transfer fails. -h — Outputs numbers in a human-readable format. -a — ‘Archive mode’. -v — Verbose...
Dec 28th
4 tags
Making rsync easy
rsync is an invaluable tool for copying or uploading files. From the manual: It can copy locally, to/from another host over any remote shell, or to/from a remote rsync daemon. … It is famous for its delta-transfer algorithm, which reduces the amount of data sent over the network by sending only the differences between the source files and the existing files in the destination. Rsync is...
Dec 28th
1 note
1 tag
Disable the OS X dashboard
$ defaults write com.apple.dashboard mcx-disabled -bool YES $ killall Dock
Dec 28th
2 tags
⌘C ⌘V
⌘   cmd ⌥   option/alt ⇧   shift ⌃   ctrl ⌫   delete ⎋   esc ↑ ↓ ← → ⇥   tab ⏎ ↩   return ⏏   eject ⇪   caps lock
Dec 28th
2 tags
timestamped ZFS snapshots
function zshot() { [ -n "$1" ] \ && zfs snapshot "$1@`date +%Y%m%d-%H%M`" \ || zfs list -t snapshot; } 21:44 $ zshot mypool/mydata/stuff # make a snapshot 21:45 $ zshot # list snapshots NAME USED AVAIL REFER MOUNTPOINT mypool/mydata/stuff@20081228-2144 0 - 10.2G -
Dec 28th
2 tags
bash aliases for ZFS
alias zlist='zfs list -t filesystem' alias zim='zpool import' zex='zpool export -f'
Dec 28th