“Command name is 25% fewer characters to type! Save days of free-time! Heck, it’s 50% shorter compared to grep -r.”
>>> a = 500
>>> b = 500
>>> a is b
False
>>> c = 200
>>> d = 200
>>> c is d
True
“Can you surmise why this inconsistency happens?”
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
“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]
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):
trimmed = [lines[0].strip()]
if indent < sys.maxint:
for line in lines[1:]:
trimmed.append(line[indent:].rstrip())
# Strip off trailing and leading blank lines:
while trimmed and not trimmed[-1]:
trimmed.pop()
while trimmed and not trimmed[0]:
trimmed.pop(0)
return '\n'.join(trimmed)
With this algorithm, an indented documentation string such as this…
def foo():
"""
A multi-line
docstring.
"""
…is converted to: "A multi-line\ndocstring."
$ 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
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]