A "Best of the Best Practices" (BOBP) guide to developing in Python.
- "Build tools for others that you want to be built for you." - Kenneth Reitz
- "Simplicity is alway better than functionality." - Pieter Hintjens
#!/usr/bin/env python | |
"""Problem Description: | |
There is an array consisting of N elements, all the elements are integer numbers (can be positive, zero or negative). | |
A transformation step is to be applied to the array, which is detailed as follows: | |
Each element has to be incremented or decremented by 1 in a single step |
<!DOCTYPE html> | |
<html> | |
<head> | |
<script data-main="usage" src="http://requirejs.org/docs/release/1.0.8/comments/require.js"></script> | |
</head> | |
<body> | |
<p>Check your JavaScript console for output!</p> | |
</body> | |
</head> |
public class DisjSets | |
{ | |
public DisjSets( int numElements ) | |
{ | |
s = new int [ numElements ]; | |
for ( int i = 0; i < s.length; ++i ) | |
s[i] = -1; | |
} | |
#!/usr/bin/env python | |
def classtree(cls, indent=0): | |
print '.' * indent, cls.__name__ | |
for subcls in cls.__subclasses__(): | |
classtree(subcls, indent + 3) | |
classtree(BaseException) |
Latency Comparison Numbers | |
-------------------------- | |
L1 cache reference 0.5 ns | |
Branch mispredict 5 ns | |
L2 cache reference 7 ns 14x L1 cache | |
Mutex lock/unlock 25 ns | |
Main memory reference 100 ns 20x L2 cache, 200x L1 cache | |
Compress 1K bytes with Zippy 3,000 ns 3 us | |
Send 1K bytes over 1 Gbps network 10,000 ns 10 us | |
Read 4K randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD |
#!/usr/bin/env python | |
# name binding always creates a name in the local scope | |
ng = 'a global' | |
def f(): | |
# without the following commented out line, UnboundLocalError will throw | |
# global ng | |
nl = 2 | |
print ng, nl | |
ng = 3 |
#!/bin/bash | |
export FOO=100 | |
python - <<END | |
import os | |
print "foo:", os.environ['FOO'] | |
END |
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
from __future__ import print_function | |
import errno | |
import os | |
def mkdir_p(path): | |
try: |