Posts

How to avoid adding duplicate keys to dictionary by mistake ...

There would be cases where large dictionaries are being embedded in the code (multiple pages in length) and users might append something to end of the dictionary, just assuming that its not already in the dictionary.  This could be easily avoided by following dict() instead of {} notation for dictionaries. > > > a = { . . . 'a' : 1 , . . . 'b' : 2 , . . . 'c' : 3 , . . . 'd' : 'nextpage' , . . . 'a' : 0 . . . } > > > print a { 'a' : 0 , 'c' : 3 , 'b' : 2 , 'd' : 'nextpage' } > > > a = dict ( . . . a = 1 , . . . b = 2 , . . . c = 3 , . . . d = 'nextpage' , . . . a = 0 . . . ) File " <stdin> " , line 6 SyntaxError : keyword argument repeated

Multiple repeat error when the regex has multiple wildcards in python re module

When the regex being compiled in python which has multiple wild cards like plus or asterisks sequentially repeated, you will need to escape or else run into "multiple repeat error". This can happen with any of re module methods like search and sub etc, where the regex is compiled. > > > import re > > > re . compile ( " r++ " ) Traceback ( most recent call last ) : File " <stdin> " , line 1 , in < module > File " /usr/lib/python2.7/re.py " , line 190 , in compile return _compile ( pattern , flags ) File " /usr/lib/python2.7/re.py " , line 242 , in _compile raise error , v # invalid expression sre_constants . error : multiple repeat > > > re . compile ( " r** " ) Traceback ( most recent call last ) : File " <stdin> " , line 1 , in < module > File " /usr/lib/python2.7/re.py " , line 190 , in compile return _...

Splitting python path using os.path module

Avoid using "/" at the end of the directory paths as they would generate empty filename when used with functions like os.path.split() > > > import os > > > os . path . split ( '/tmp/foo' ) ( '/tmp' , 'foo' ) > > > os . path . split ( '/tmp/foo/' ) ( '/tmp/foo' , '' ) Reference: https://docs.python.org/2/library/os.path.html

Class/Global variables vs Instance Variables ...

Class/Global variables are like CoW, only when the instances assign them values they have their own memory location allocated. For example in this case a.bar is assigned a value and hence it has different memory location allocated, but the same is not the case with b.bar >> > class Foo ( object ) : bar = None >> > repr ( Foo . bar ) 'None' >> > a = Foo ( ) >> > a . bar = 1 >> > repr ( Foo . bar ) 'None' >> > a . bar 1 >> > b = Foo ( ) >> > b . bar >> > repr ( b . bar ) 'None' >> > a . bar 1 >> > id ( Foo . bar ) 135796184 >> > id ( a . bar ) 151257264 >> > id ( b . bar ) 135796184

To speed up accessing files on nfs shares on a ubuntu machine ...

To enable nfs caching on ubuntu, sudo apt-get install cachefilesd sudo sed -i 's/nfs rw/nfs fsc,rw/' /etc/fstab service restart cachefilesd To use tmpfs instead of disk cache use this after making sure the runner has atleast 12G of memory, mount | grep fscache || mount -t tmpfs -o size=8192m tmpfs /fscache ls /fscache/disk0 || dd if=/dev/zero of=/fscache/disk0 bs=1M count=8191 sleep 15 ls /fscache/disk0 && echo 'y' | mkfs.ext4 /fscache/disk0 mount /fscache/disk0 /var/cache/fscache -t ext4 service cachefilesd restart

Weakref proxy is for instance only ...

Proxy cannot be created for basic datatypes like list, dict, str and int. Proxy can be create for class instances only. Also proxy cannot be created for proxy of another instance. >>> import weakref >>> a = 'str' >>> b = weakref.proxy(a) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: cannot create weak reference to 'str' object >>> a = 1 >>> b = weakref.proxy(a) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: cannot create weak reference to 'int' object >>> a = [] >>> b = weakref.proxy(a) Traceback (most recent call last):   File "<stdin>", line 1, in <module> TypeError: cannot create weak reference to 'list' object >>> a = {} >>> b = weakref.proxy(a) Traceback (most recent call last):   File "<stdin>",...

Raising vs Reraising exceptions in python

>>> def foo(x): ...    raise Exception('%s is invalid input' % x) ... >>> try: ...     foo(1) ... except Exception, e: ...     raise e                          # RE-RAISING EXCEPTIONS ... Traceback (most recent call last):   File "<stdin>", line 4, in <module>    # LOSES foo Exception: 1 is invalid input >>> try: ...     foo(1) ... except Exception: ...     raise                             # RAISING EXCEPTIONS ... Traceback (most recent call last):   File "<stdin>", line 2, in <module>   File "<stdin>", line 2, in foo          # STORES foo too Exception: 1 is invalid input >>>