find . -type d -empty -exec rmdir {} \;
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()))
if ar[2]!="client": # license: http://creativecommons.org/licenses/by-nc-sa/2.0
myU,prs,srv = ("http://"+ar[3]+":"+ar[4], ar[5:],lambda x:x.serve_forever())
def pr(x=[]): return ([(y in prs) or prs.append(y) for y in x] or 1) and prs
def c(n): return ((lambda f: (f.read(), f.close()))(file(n)))[0]
f=lambda p,n,a:(p==pw(myU))and(((n==0)and pr(a))or((n==1)and [ls(a)])or c(a))
def aug(u): return ((u==myU) and pr()) or pr(pxy(u).f(pw(u),0,pr([myU])))
pr() and [aug(s) for s in aug(pr()[0])]
(lambda sv:sv.register_function(f,"f") or srv(sv))(xs((ar[3],int(ar[4]))))
for url in pxy(ar[3]).f(pw(ar[3]),0,[]):
for fn in filter(lambda n:not n in ls(), (pxy(url).f(pw(url),1,ar[4]))[0]):
(lambda fi:fi.write(pxy(url).f(pw(url),2,fn)) or fi.close())(file(fn,"wc"))
>>> 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]}
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]
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
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 interested, you can also read about why it works.
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$
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.