<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><channel><description>A place for code snippets, keyboard shortcuts, dirty hacks and other fun tricks. Use at your own risk!</description><title>code.dump();</title><generator>Tumblr (3.0; @codedump)</generator><link>http://codedump.tumblr.com/</link><item><title>Extract &amp; mirror cache url's from google search pages</title><description>&lt;p&gt;Saved search pages go in, cache links come out.&lt;br/&gt;It’s handy for mirroring a dead site by using site:domain.com as the search parameter.&lt;/p&gt;
&lt;p&gt;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 be much lower though. The empty useragent is required for wget to work.&lt;/p&gt;
&lt;p&gt;To match the junk part of the filename (search?q=cache:4Ip_t8yQ-rL2:) use this regex: search\?q=cache:…………:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;pcregrep -ho http://\\d\(.+?\)\(?=[+]\(.+\)\"\&gt;Cached\\) search.html [search2.html etc.] &gt; cachelist.txt&lt;br/&gt;wget --wait 30s --random-wait --user-agent="" -i cachelist.txt&lt;/code&gt;&lt;code&gt;&lt;br/&gt;&lt;/code&gt;&lt;/pre&gt;</description><link>http://codedump.tumblr.com/post/178215352</link><guid>http://codedump.tumblr.com/post/178215352</guid><pubDate>Wed, 02 Sep 2009 22:02:00 +0100</pubDate><category>wget</category><category>regex</category><category>grep</category></item><item><title>Set mp3 date tag as YYYY-MM-DD based on mtime </title><description>&lt;p&gt;Particularly useful for &lt;a href="http://www.pri.org/fair-game.html"&gt;large, poorly tagged radio archives&lt;/a&gt; mirrored with wget.&lt;br/&gt;Processes files in the working directory matching the patters specified.&lt;/p&gt;
&lt;p&gt;Requires: &lt;a href="http://code.google.com/p/mutagen/"&gt;mutagen&lt;/a&gt; (for tagging)&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;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&lt;/code&gt;&lt;/pre&gt;</description><link>http://codedump.tumblr.com/post/149105627</link><guid>http://codedump.tumblr.com/post/149105627</guid><pubDate>Sat, 25 Jul 2009 23:36:00 +0100</pubDate><category>perl</category><category>bash</category><category>tagging</category></item><item><title>dirty</title><description>&lt;a href="http://code.google.com/p/dirty/"&gt;dirty&lt;/a&gt;: &lt;p&gt;“A simple and dirty HTML/XML template library for Python 3.”&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&gt;&gt;&gt; from dirty.html import *
&gt;&gt;&gt; page = xhtml(
...   head(
...     title("Dirty"),
...     meta(name="Author", content="Hong, MinHee &lt;minhee@dahlia.kr&gt;")
...   ),
...   body(
...     h1("Dirty"),
...     p("Dirty is a simple DSEL template library that...")
...   )
... )
&gt;&gt;&gt; print(page)
&lt;!DOCTYPE html PUBLIC
  "-//W3C//DTD XHTML 1.0 Strict//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml" /&gt;
  &lt;head&gt;
    &lt;title&gt;Dirty&lt;/title&gt;
    &lt;meta content="Hong, MinHee &lt;minhee@dahlia.kr&gt;" name="Author" /&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Dirty&lt;/h1&gt;
    &lt;p&gt;Dirty is a simple DSEL template library that...&lt;/p&gt;
  &lt;/body&gt;
&lt;/html&gt;&lt;/code&gt;&lt;/pre&gt;</description><link>http://codedump.tumblr.com/post/149091837</link><guid>http://codedump.tumblr.com/post/149091837</guid><pubDate>Sat, 25 Jul 2009 23:05:10 +0100</pubDate><category>python</category><category>html</category></item><item><title>Time formatting in Haskell</title><description>&lt;pre&gt;&lt;code&gt;showTime :: Int -&gt; Int -&gt; String
showTime hours minutes
    | hours == 0    = "12" ++ ":" ++ showMin ++ " am"
    | hours &lt;= 11   = (show hours) ++ ":" ++ showMin ++ " am"
    | hours == 12   = (show hours) ++ ":" ++ showMin ++ " pm"
    | otherwise     = (show (hours - 12)) ++ ":" ++ showMin ++ " pm"
    where
    showMin
        | minutes &lt; 10  = "0" ++ show minutes
        | otherwise     = show minutes&lt;/code&gt;&lt;/pre&gt;

&lt;pre&gt;&lt;code&gt;Main&gt; showTime 13 37
"1:37 pm"&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;From &lt;a href="http://www.haskell.org/~pairwise/intro/intro.html"&gt;Haskell for C Programmers&lt;/a&gt;.&lt;/p&gt;</description><link>http://codedump.tumblr.com/post/141434179</link><guid>http://codedump.tumblr.com/post/141434179</guid><pubDate>Tue, 14 Jul 2009 13:58:30 +0100</pubDate><category>haskell</category></item><item><title>"Command name is 25% fewer characters to type! Save days of free-time! Heck, it’s 50% shorter..."</title><description>“Command name is 25% fewer characters to type! Save days of free-time! Heck, it’s 50% shorter compared to &lt;code&gt;grep -r&lt;/code&gt;.”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;&lt;a href="http://betterthangrep.com/"&gt;Why use ack?&lt;/a&gt; (via &lt;a href="http://lloyda2.tumblr.com/"&gt;lloyda2&lt;/a&gt;)&lt;/em&gt;</description><link>http://codedump.tumblr.com/post/137042888</link><guid>http://codedump.tumblr.com/post/137042888</guid><pubDate>Tue, 07 Jul 2009 13:21:00 +0100</pubDate><category>perl</category><category>grep</category></item><item><title>Python integer gotcha</title><description>&lt;a href="http://distilledb.com/blog/archives/date/2009/06/18/python-gotcha-integer-equality.page"&gt;Python integer gotcha&lt;/a&gt;: &lt;pre&gt;&lt;code&gt;&gt;&gt;&gt; a = 500
&gt;&gt;&gt; b = 500
&gt;&gt;&gt; a is b
False&lt;br/&gt;
&gt;&gt;&gt; c = 200
&gt;&gt;&gt; d = 200
&gt;&gt;&gt; c is d
True&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;“Can you surmise why this inconsistency happens?”&lt;/p&gt;</description><link>http://codedump.tumblr.com/post/128336349</link><guid>http://codedump.tumblr.com/post/128336349</guid><pubDate>Mon, 22 Jun 2009 23:04:00 +0100</pubDate><category>python</category></item><item><title>Disable the new look in Safari 4</title><description>&lt;p&gt;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:&#13;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;defaults write com.apple.Safari DebugSafari4TabBarIsOnTop -bool NO&#13;
defaults write com.apple.Safari DebugSafari4IncludeToolbarRedesign -bool NO&#13;
defaults write com.apple.Safari DebugSafari4LoadProgressStyle -bool NO&lt;/code&gt;&lt;/pre&gt;</description><link>http://codedump.tumblr.com/post/118913305</link><guid>http://codedump.tumblr.com/post/118913305</guid><pubDate>Sat, 06 Jun 2009 12:02:00 +0100</pubDate><category>os x</category></item><item><title>Python: cached generator</title><description>&lt;a href="http://natesoares.com/writing/python-fractals-4/"&gt;Python: cached generator&lt;/a&gt;: &lt;p&gt;“A generator and a list used like a cache.”&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;b&gt;class&lt;/b&gt; GeneratorList(object):

    &lt;b&gt;def&lt;/b&gt; __init__(self, generator):
        self.__generator = generator
        self.__list = []

    &lt;b&gt;def&lt;/b&gt; __getitem__(self, index):
        &lt;b&gt;for&lt;/b&gt; _ &lt;b&gt;in&lt;/b&gt; range(index - len(self.__list) + 1):
            self.__list.append(self.__generator.next())
        &lt;b&gt;return&lt;/b&gt; self.__list[index]&lt;/code&gt;&lt;/pre&gt;</description><link>http://codedump.tumblr.com/post/100079538</link><guid>http://codedump.tumblr.com/post/100079538</guid><pubDate>Sat, 25 Apr 2009 20:36:00 +0100</pubDate><category>python</category></item><item><title>Handling Python Docstring Indentation </title><description>&lt;a href="http://www.python.org/dev/peps/pep-0257/#handling-docstring-indentation"&gt;Handling Python Docstring Indentation &lt;/a&gt;: &lt;pre&gt;&lt;code&gt;&lt;b&gt;def&lt;/b&gt; trim(docstring):
    &lt;b&gt;if not&lt;/b&gt; docstring:
        &lt;b&gt;return&lt;/b&gt; ''
    lines = docstring.expandtabs().splitlines()

    &lt;em&gt;# Determine minimum indentation (first line doesn't count):&lt;/em&gt;
    indent = sys.maxint
    &lt;b&gt;for&lt;/b&gt; line &lt;b&gt;in&lt;/b&gt; lines[1:]:
        stripped = line.lstrip()
        &lt;b&gt;if&lt;/b&gt; stripped:
            indent = min(indent, len(line) - len(stripped))

    &lt;em&gt;# Remove indentation (first line is special):&lt;/em&gt;
    trimmed = [lines[0].strip()]
    &lt;b&gt;if&lt;/b&gt; indent &lt; sys.maxint:
        &lt;b&gt;for&lt;/b&gt; line &lt;b&gt;in&lt;/b&gt; lines[1:]:
            trimmed.append(line[indent:].rstrip())

    &lt;em&gt;# Strip off trailing and leading blank lines:&lt;/em&gt;
    &lt;b&gt;while&lt;/b&gt; trimmed &lt;b&gt;and not&lt;/b&gt; trimmed[-1]:
        trimmed.pop()
    &lt;b&gt;while&lt;/b&gt; trimmed &lt;b&gt;and not&lt;/b&gt; trimmed[0]:
        trimmed.pop(0)
    &lt;b&gt;return&lt;/b&gt; '&lt;b&gt;\n&lt;/b&gt;'.join(trimmed)&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;With this algorithm, an indented documentation string such as this…&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;b&gt;def&lt;/b&gt; foo():
    &lt;em&gt;"""
    A multi-line
    docstring.
    """&lt;/em&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;…is converted to: &lt;code&gt;"A multi-line&lt;b&gt;\n&lt;/b&gt;docstring."&lt;/code&gt;&lt;/p&gt;</description><link>http://codedump.tumblr.com/post/94712647</link><guid>http://codedump.tumblr.com/post/94712647</guid><pubDate>Fri, 10 Apr 2009 03:05:53 +0100</pubDate><category>python</category></item><item><title>MacPorts garbage collection</title><description>&lt;a href="http://gist.github.com/75745"&gt;MacPorts garbage collection&lt;/a&gt;: &lt;p&gt;&lt;pre&gt;&lt;code&gt;$ sudo su -
# du -sh /opt
6.4G  /opt
# port clean -f --all installed
---&gt; Cleaning apr
---&gt; Cleaning apr-util
...
# du -sh /opt
6.3G  /opt
# port -f uninstall inactive
---&gt;  Uninstalling autoconf @2.62_0
---&gt;  Uninstalling automake @1.10.1_0
...
# du -sh /opt
5.4G  /opt&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;</description><link>http://codedump.tumblr.com/post/85536345</link><guid>http://codedump.tumblr.com/post/85536345</guid><pubDate>Wed, 11 Mar 2009 15:15:34 +0000</pubDate><category>macports</category><category>os x</category></item><item><title>Remove empty directories</title><description>&lt;a href="http://www.shell-fu.org/lister.php?id=496"&gt;Remove empty directories&lt;/a&gt;: &lt;p&gt;&lt;pre&gt;&lt;code&gt;find . -type d -empty -exec rmdir {} \;&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;</description><link>http://codedump.tumblr.com/post/73886915</link><guid>http://codedump.tumblr.com/post/73886915</guid><pubDate>Wed, 28 Jan 2009 23:33:00 +0000</pubDate><category>bash</category></item><item><title>TinyP2P: A 15 line long Python P2P Application</title><description>&lt;p&gt;Via &lt;a href="http://nosmo.tumblr.com/post/73482434/tinyp2p-a-15-line-long-python-p2p-application"&gt;nosmo&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;# tinyp2p.py 1.0 (documentation at &lt;a href="http://freedom-to-tinker.com/tinyp2p.html"&gt;&lt;a href="http://freedom-to-tinker.com/tinyp2p.html"&gt;http://freedom-to-tinker.com/tinyp2p.html&lt;/a&gt;&lt;/a&gt;)&lt;br/&gt;import sys, os, SimpleXMLRPCServer, xmlrpclib, re, hmac # (C) 2004, E.W. Felten&lt;br/&gt;ar,pw,res = (sys.argv,lambda u:hmac.new(sys.argv[1],u).hexdigest(),re.search)&lt;br/&gt;pxy,xs = (xmlrpclib.ServerProxy,SimpleXMLRPCServer.SimpleXMLRPCServer)&lt;br/&gt;def ls(p=""):return filter(lambda n:(p=="")or res(p,n),os.listdir(os.getcwd()))&lt;br/&gt;if ar[2]!="client": # license: &lt;a href="http://creativecommons.org/licenses/by-nc-sa/2.0"&gt;&lt;a href="http://creativecommons.org/licenses/by-nc-sa/2.0"&gt;http://creativecommons.org/licenses/by-nc-sa/2.0&lt;/a&gt;&lt;/a&gt;&lt;br/&gt;  myU,prs,srv = ("http://"+ar[3]+":"+ar[4], ar[5:],lambda x:x.serve_forever())&lt;br/&gt;  def pr(x=[]): return ([(y in prs) or prs.append(y) for y in x] or 1) and prs&lt;br/&gt;  def c(n): return ((lambda f: (f.read(), f.close()))(file(n)))[0]&lt;br/&gt;  f=lambda p,n,a:(p==pw(myU))and(((n==0)and pr(a))or((n==1)and [ls(a)])or c(a))&lt;br/&gt;  def aug(u): return ((u==myU) and pr()) or pr(pxy(u).f(pw(u),0,pr([myU])))&lt;br/&gt;  pr() and [aug(s) for s in aug(pr()[0])]&lt;br/&gt;  (lambda sv:sv.register_function(f,"f") or srv(sv))(xs((ar[3],int(ar[4]))))&lt;br/&gt;for url in pxy(ar[3]).f(pw(ar[3]),0,[]):&lt;br/&gt;  for fn in filter(lambda n:not n in ls(), (pxy(url).f(pw(url),1,ar[4]))[0]):&lt;br/&gt;    (lambda fi:fi.write(pxy(url).f(pw(url),2,fn)) or fi.close())(file(fn,"wc"))&lt;/code&gt;&lt;/pre&gt;</description><link>http://codedump.tumblr.com/post/73537885</link><guid>http://codedump.tumblr.com/post/73537885</guid><pubDate>Tue, 27 Jan 2009 17:39:04 +0000</pubDate><category>python</category></item><item><title>pickling: converting a Python object into a byte stream</title><description>&lt;a href="http://docs.python.org/library/pickle.html"&gt;pickling: converting a Python object into a byte stream&lt;/a&gt;: &lt;p&gt;&lt;pre&gt;&lt;code&gt;&gt;&gt;&gt; stuff = {
...   'hello': 'world',
...   'numbers': range(3),
... }

&gt;&gt;&gt; import pickle

&gt;&gt;&gt; data = pickle.dumps(stuff)

&gt;&gt;&gt; len(data)
63

&gt;&gt;&gt; print data
(dp0
S'hello'
p1
S'world'
p2
sS'numbers'
p3
(lp4
I0
aI1
aI2
as.

&gt;&gt;&gt; pickle.loads(data)
{'hello': 'world', 'numbers': [0, 1, 2]}&lt;/code&gt;&lt;/pre&gt;&lt;/p&gt;</description><link>http://codedump.tumblr.com/post/73093919</link><guid>http://codedump.tumblr.com/post/73093919</guid><pubDate>Sun, 25 Jan 2009 23:48:00 +0000</pubDate><category>python</category></item><item><title>Extended slices in Python</title><description>&lt;p&gt;To access even-indexed elements of a list, use the extended slice feature of Python.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&gt;&gt;&gt; range(20)[::2]&lt;br/&gt;[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&#13;&lt;/p&gt;

&lt;p&gt;This can be used to access every third element by using ::3 and so on.&lt;/p&gt;



&lt;p&gt;To reverse a list, use the extended slice with an argument of -1.&lt;/p&gt;



&lt;pre&gt;&lt;code&gt;&gt;&gt;&gt; range(8)[::-1]&lt;br/&gt;[7, 6, 5, 4, 3, 2, 1, 0]&lt;/code&gt;&lt;/pre&gt;</description><link>http://codedump.tumblr.com/post/70269370</link><guid>http://codedump.tumblr.com/post/70269370</guid><pubDate>Tue, 13 Jan 2009 20:54:00 +0000</pubDate><category>python</category></item><item><title>Disable the 3D-dock in Leopard</title><description>&lt;p&gt;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:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;defaults write com.apple.dock no-glass -boolean YES &#13;
killall Dock &lt;/code&gt;&lt;/pre&gt;</description><link>http://codedump.tumblr.com/post/70258619</link><guid>http://codedump.tumblr.com/post/70258619</guid><pubDate>Tue, 13 Jan 2009 19:52:00 +0000</pubDate><category>os x</category></item><item><title>Quake3's Fast Inverse Square Root Function</title><description>&lt;p&gt;Oh my:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;float InvSqrt (float x){
    float xhalf = 0.5f*x;
    int i = *(int*)&amp;x;
    i = 0x5f3759df - (i&gt;&gt;1);
    x = *(float*)&amp;i;
    x = x*(1.5f - xhalf*x*x);
    return x;
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;According to one of the guys &lt;a href="http://www.beyond3d.com/content/articles/8/"&gt;supposedly responsible&lt;/a&gt; for this (arguably) legendary piece of code, “it’s just Newton-Raphson iteration with a very
clever first approx.” And if you’re interested, you can also read about &lt;a href="http://betterexplained.com/articles/understanding-quakes-fast-inverse-square-root/"&gt;why it works&lt;/a&gt;.&lt;/p&gt;</description><link>http://codedump.tumblr.com/post/70257672</link><guid>http://codedump.tumblr.com/post/70257672</guid><pubDate>Tue, 13 Jan 2009 19:48:01 +0000</pubDate><category>c</category></item><item><title>Getting the current system time in milliseconds with C++</title><description>&lt;a href="http://brian.pontarelli.com/2009/01/05/getting-the-current-system-time-in-milliseconds-with-c/"&gt;Getting the current system time in milliseconds with C++&lt;/a&gt;</description><link>http://codedump.tumblr.com/post/70246018</link><guid>http://codedump.tumblr.com/post/70246018</guid><pubDate>Tue, 13 Jan 2009 18:45:00 +0000</pubDate><category>c</category></item><item><title>bash: mkdir + cd with one command</title><description>&lt;pre&gt;&lt;code&gt;function mkcd() {
  [ -n "$1" ] &amp;&amp; mkdir -p "$@" &amp;&amp; cd "$1";
}&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Example:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;user@host:~$ cd work
user@host:~/work$ mkcd website.com/{html,src,stuff}
user@host:~/work/website.com/html$&lt;/code&gt;&lt;/pre&gt;</description><link>http://codedump.tumblr.com/post/68215495</link><guid>http://codedump.tumblr.com/post/68215495</guid><pubDate>Sun, 04 Jan 2009 02:08:00 +0000</pubDate><category>bash</category></item><item><title>Set Dock switching mode to 'hide others'</title><description>&lt;a href="http://www.macosxhints.com/article.php?story=20081229062620506"&gt;Set Dock switching mode to 'hide others'&lt;/a&gt;: &lt;blockquote&gt;
&lt;p&gt;To enable this switching mode, open Terminal and type these commands:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ defaults write com.apple.dock single-app -bool TRUE&lt;br/&gt;$ killall Dock&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt; 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.&lt;/p&gt;
&lt;/blockquote&gt;</description><link>http://codedump.tumblr.com/post/67665145</link><guid>http://codedump.tumblr.com/post/67665145</guid><pubDate>Wed, 31 Dec 2008 14:30:00 +0000</pubDate><category>os x</category></item><item><title>The C Standard Library</title><description>&lt;a href="http://www.utas.edu.au/infosys/info/documentation/C/CStdLib.html"&gt;The C Standard Library&lt;/a&gt;: &lt;p&gt;Short and sweet.&lt;/p&gt;</description><link>http://codedump.tumblr.com/post/67320150</link><guid>http://codedump.tumblr.com/post/67320150</guid><pubDate>Mon, 29 Dec 2008 16:01:31 +0000</pubDate><category>c</category></item></channel></rss>
