It is usual to check for an item in a list, tuple, or dict using 'in' operator.
List=[ 'l' , 'i' , 's' , 't']if 's' in List: print "success"
for string we usually go with 'find' function. Its quite easier to go with 'in' operator for string too.
This file contains bidirectional Unicode text that...
Educating yourself does not mean that you were stupid in the first place; it means that you are intelligent enough to know that there is plenty left to 'learn'. -Melanie Joy
Sunday, 23 December 2012
Saturday, 22 December 2012
Fancy Formatting using Python
Its quite easy in python to pretty print the output...
Here are two ways to write a table of squares and cubes:
for x in range(1, 6):
... print repr(x).rjust(2), repr(x*x).rjust(3),repr(x*x*x).rjust(4)
...
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
>>> for x in range(1,6):
......
Some Optimization Tips/Tricks in Python
Looping:
Use xrange for looping across long ranges; it uses much less memory than range, and may save time as well. Both versions are likely to be faster than a while loop:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in...
Friday, 21 December 2012
Best way to open files in Python
C like syntax is as follows,
f = open("file", 'r')
line = f.readline( )
print line
close(f)
some one may forget to close f. this leads to leak, which is considerable in large scale programs. in order to avoid that we can make use of 'with' or 'for' keywords.
with open("file", 'r') as f:
line = f.readline()
print line
or
for...
Thursday, 4 October 2012
Timeout - timed wait in shell scripting
TMOUT is the shell built in variable to set the timeout .
TMOUT is used in three different ways: by the read builtin command, by the select builtin, and by the interactive bash shell. If it is unset, or equal to zero, then it is ignored. If it has any positive value, then these three commands which make use of it will timeout after $TMOUT seconds.
Example:
$> cat timeout.sh
...
PIPESTATUS and its Alternative
$> cat /etc/hosts | grep 000.000
$> $?
1
$> cat /etc/hosts | grep 000.000 | uniq
$> $?
0
Why is it returning success (0) though it fails during grep ??
the return code of a pipeline will be that of the return status of the rightmost command .
How to resolve this ??
use inbuilt PIPESTATUS variable.
PIPESTATUS is a array variable which contain the exit status of each command...
Saturday, 18 August 2012
Auto Correct Directory Names / Shell Options
"shopt" can be used to correct typos in cd command.
you can use "cdspell" which will auto correct the directory name you have entered.
Usage: shopt -s cdspell
( -s : set
-u : unset)
$ cd /usr/local/sben will results in bash: cd: /usr/local/sbEN: No such file or directory
but once you have enabled cdspell option,
$ cd /usr/local/sben ...
Tuesday, 5 June 2012
vim/gvim-Tips and Tricks
Tip 1 :
Use markers to set a place where you want to go back quickly or to delete some block of code
ma - mark current position as 'a' [ can use a-z in same file, or can use A-Z between files]
'a - go to mark a
d'a - delete current position to mark a [ can use 'p' after this to paste that block ]
Tip 2:
Do you want to reverse the entire file or a block...
Monday, 4 June 2012
Creating A List Using vim
Some time you may need to insert a list of ascending numbers in a file. We can do this using vim very easily.
Method 1 :
:put =range(11,15)
This will create a series of increasing numbers after the current line including 11 and 15.
output will be-
11
12
13
14
15
Method 2 :
Some times if you want to add a constant predefined string before the number series, then you...
`Tar`- An Ultimate Archive Utility
How to use tar
tar [options] [name of tar file to be created] [list of files and directories to be included]
1. Creating an archive using tar
$ tar -cvf target.tar file1 file2 dir1
$ tar -cvzf target.tar.gz dirname/ [ creates gzipped tar...
'cat' can be harmful..
'cat' can be harmful at times. some people might have lost the data because of improper usage of cat command like cat > filename.txt, or it can do over write some existing files data without prompting the user.
to get rid out of these situations
1. open the terminal
2. execute " set -o noclobber "
that's it. now next time when cat tries to over write the existing file it will prompt the us...