Posts

Showing posts from 2013

Random generator from 1-7 from a given random generator 1-5

INPUT: import random def rand5(): return random.randint(1, 5) def rand7(): """ Generating 25 random numbers uniformly between 1-25 and picking up modulo 7 from first 21 of them 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 - - - - """ while True: # x is uniformly generated between 1-25 x = 5 * (rand5() - 1) + rand5() if (x <= 21): # Ignore >21 numbers break # Result is now uniformly random between 1-7 return x % 7 + 1 def distribution(foo): d = {} for _ in xrange(10000): x = foo() if x not in d: d[x] = 0 d[x] += 1 return d print distribution(rand7) print distribution(rand5) OUTPUT: {1: 1446, 2: 1422, 3: 1417, 4: 1442, 5: 1459, 6: 1403, 7: 1411} {1: 1995, 2: 2016, 3: 1999, 4: 1992, 5: 1998}

Invalid positonal args cannot be passed over as kwargs using **kwargs

Input: def a(x, y=None):    print x, y def b(x, **kw):     a(x, **kw) def c(x, y):     print x, y c(1, y=1) b(1, y=2) b(1, 3) Output: 1 1 1 2 Traceback (most recent call last):   File "a.py", line 12, in <module>     b(1, 3) TypeError: b() takes exactly 1 argument (2 given)

Try/Except vs Exception handling

try:   foo() except:   pass or try:   foo() except Exception:   pass Difference is that the first will catch everything including OsError, KeboardInterrupt, SystemExit which are derived from BaseException instead of Exception class. More documentation for details: try statement — http://docs.python.org/reference/compound_stmts.html#try exceptions — http://docs.python.org/library/exceptions

Most common mistakes made in patches

Moving code around and modifying the code blocks at the same time Changing interfaces/method signatures and not modifying existing method calls Duplicating methods that do same functionality

Empty strings "" and zero 0 are treated as False in python

>>> any ([0, 0]) False >>> any ([1, 0]) True >>> any ([None, 0, ""]) False >>> bool(0) False >>> bool("") False >>> bool(1) True

Python code: TypeError: 'str' object is not callable

Missing % can cause chaos with string expansions, Sometime the print/logging statement become too huge to verify where the string is coming from. For example in this case you might be looking for string that is used as callable forever, if you dont look at the actual string thats being expanded. Input: def foo1(x):     return str(x) foo2 = lambda x: None if x is None else str(x) def myprint(x):     print x x = 5 myprint("x=%s, foo1(x)=%s, foo2(x)=%s" %         (str(x), foo1(x), foo2(x))) myprint("x=%s, foo1(x)=%s, foo2(x)=%s"         (str(x), foo1(x), foo2(x))) Output: x=5, foo1(x)=5, foo2(x)=5 Traceback (most recent call last):   File "a.py", line 15, in <module>     (x, foo1(x), foo2(x))) TypeError: 'str' object is not callable

FD column in linux command lsof means a lot ....

lsof gives a ton of information about the files and devices that are open on a system. FD has always wondered me with some wierd numbers/characters. Atlast I bothered to look at it for knowing whether tcpdump has started or not to sniff the device and realized it gives details about the what kind of device/file is opened and the permissions etc. From the man page,        FD         is the File Descriptor number of the file or:                        cwd  current working directory;                        Lnn  library references (AIX);                        err  FD information error (see NAME column);                        jld  jail directory (FreeBSD);                        ltx  shared library text (code and data);                        Mxx  hex memory-mapped type number xx.                        m86  DOS Merge mapped file;                        mem  memory-mapped file;                        mmap memory-mapped device;                        pd   parent directory;    

Want a fast and temporary file system on linux?

Just use a tmpfs/ramfs and a cron job continously backing up data from  this to a non-volatile storage. mount -t tmpfs -o size=1024m tmpfs /tmp Generally, I use this for logs on virtual machines, as sometimes the  disk is very slow due to multiple VMs running off the same disk. Also,  this has the advantage of not wearing out the disk, which might have  more important/persistent data.

Fighting the zombies out of your linux system ...

I had tough time to figure out that zombies are invading my linux box, here's the powerful one-liner to kill them all :) kill -9 `ps -ef | grep defunct | cut -d ' ' -f 3`

Enable valgrind on Xenserver hosts with openvswitch installations

To install valgrind on xenservers, yum install valgrind --enablerepo=base To enable valgrind for openvswitch, you could add it to the OVS_CTL_OPTS in the file /etc/sysconfig/openvswitch OVS_CTL_OPTS=--ovs-vswitchd-wrapper=valgrind

Python madness in comparisons with None

In python, everything is greater than None, >>> 0 > None True >>> 0 < None False >>> 1 > None True >>> 1 < None False >>> -1 > None True >>> -1 < None False >>> 'a' > None True >>> 'a' < None False >>> 1.0 > None True >>> 1.0 < None False

Overriding class variables and destroying overriden references in python classes

Input: class foo(object):     ref = None     def setup(self):         self.ref = 1234     def clear(self):         del self.ref obj = foo() print obj.ref obj.setup() print obj.ref obj.clear() print obj.ref O utput: None 1234 None

Tip to organize photos by date taken as folder name

exiftool -d "./%Y-%m-%d/%%f.%%e" "-filename<createdate" *.JPG If you don't like the default file names which are mostly like IMG_0001.JPG in the DSLR cameras, you can just use the time stamp using the following command, exiftool -d "./%Y-%m-%d/IMG_%Y%m%d_%H%M%S_%%-c.%%e" "-filename<createdate" *.JPG %%-c will kick in the counter for your sub-second shots.

Exceptions and Finally in python

>>> Input def foo():    try:        print "I am in foo"        return    except:        raise    finally:        print "finally of foo" def foo_exec():    try:        print "I am in foo_exec"        raise Exception("Exception in foo_exec")    except:        raise    finally:        print "finally of foo_exec" foo() foo_exec() >>> Output I am in foo finally of foo I am in foo_exec finally of foo_exec Traceback (most recent call last):   File "a.py", line 20, in <module>     foo_exec()   File "a.py", line 13, in foo_exec     raise Exception("Exception in foo_exec") Exception: Exception in foo_exec