Tuesday 29 May 2012

Iterators


1)   for x in [1,4,5,10]:
...
print x


2) prices = { 'GOOG' : 490.10,
...
'AAPL' : 145.23,
...
'YHOO' : 21.71 }
...
>>> for key in prices:
...
print key


3) s = "Yow!"
>>> for c in s:
...
print c


In all the above for iteration , what is happening ?



items = [1, 4, 5]
>>> it = iter(items)
>>> it.next()
1
>>> it.next()
4
>>> it.next()
5
>>> it.next()
 
this is the underlying steps which are happening .

what does iter(items) do ..... it just returns iterator object ,,, which has inbuilt methods like next , stopiteration , etc ... after printing once , if the list is not empty , next () is exectued , so this process goes on and on , till list is empty , when the stopiteration is executed . 








Thursday 24 May 2012

Python Utilities : 

We can do a number of things that we do in bash in unix , even in python interpreter .
one thing we have to do in 

import os 

now to see what are all the operations we can do  

EX_CANTCREAT
EX_CONFIG
EX_DATAERR
EX_IOERR
EX_NOHOST
EX_NOINPUT
EX_NOPERM
EX_NOUSER
EX_OK
EX_OSERR
EX_OSFILE
EX_PROTOCOL
EX_SOFTWARE
EX_TEMPFAIL
EX_UNAVAILABLE
EX_USAGE
F_OK
NGROUPS_MAX
O_APPEND
O_ASYNC
O_CREAT
O_DIRECT
O_DIRECTORY
O_DSYNC
O_EXCL
O_LARGEFILE
O_NDELAY
O_NOATIME
O_NOCTTY
O_NOFOLLOW
O_NONBLOCK
O_RDONLY
O_RDWR
O_RSYNC
O_SYNC
O_TRUNC
O_WRONLY
P_NOWAIT
P_NOWAITO
P_WAIT
R_OK
SEEK_CUR
SEEK_END
SEEK_SET
ST_APPEND
ST_MANDLOCK
ST_NOATIME
ST_NODEV
ST_NODIRATIME
ST_NOEXEC
ST_NOSUID
ST_RDONLY
ST_RELATIME
ST_SYNCHRONOUS
ST_WRITE
TMP_MAX
UserDict
WCONTINUED
WCOREDUMP
WEXITSTATUS
WIFCONTINUED
WIFEXITED
WIFSIGNALED
WIFSTOPPED
WNOHANG
WSTOPSIG
WTERMSIG
WUNTRACED
W_OK
X_OK
_Environ
__all__
__builtins__
__doc__
__file__
__name__
__package__
_copy_reg
_execvpe
_exists
_exit
_get_exports_list
_make_stat_result
_make_statvfs_result
_pickle_stat_result
_pickle_statvfs_result
_spawnvef
abort
access
altsep
chdir
chmod
chown
chroot
close
closerange
confstr
confstr_names
ctermid
curdir
defpath
devnull
dup
dup2
environ
errno
error
execl
execle
execlp
execlpe
execv
execve
execvp
execvpe
extsep
fchdir
fchmod
fchown
fdatasync
fdopen
fork
forkpty
fpathconf
fstat
fstatvfs
fsync
ftruncate
getcwd
getcwdu
getegid
getenv
geteuid
getgid
getgroups
getloadavg
getlogin
getpgid
getpgrp
getpid
getppid
getresgid
getresuid
getsid
getuid
initgroups
isatty
kill
killpg
lchown
linesep
link
listdir
lseek
lstat
major
makedev
makedirs
minor
mkdir
mkfifo
mknod
name
nice
open
openpty
pardir
path
pathconf
pathconf_names
pathsep
pipe
popen
popen2
popen3
popen4
putenv
read
readlink
remove
removedirs
rename
renames
rmdir
sep
setegid
seteuid
setgid
setgroups
setpgid
setpgrp
setregid
setresgid
setresuid
setreuid
setsid
setuid
spawnl
spawnle
spawnlp
spawnlpe
spawnv
spawnve
spawnvp
spawnvpe
stat
stat_float_times
stat_result
statvfs
statvfs_result
strerror
symlink
sys
sysconf
sysconf_names
system
tcgetpgrp
tcsetpgrp
tempnam
times
tmpfile
tmpnam
ttyname
umask
uname
unlink
unsetenv
urandom
utime
wait
wait3
wait4
waitpid
walk
write

Now we shall see some examples 

to list contents of dirctory 

os.listdir(directoryname)
list filenames in the directory 

os.path.exists( ) 
to see is the path exists

to do file copy 

import shutil
shutil.copy(source,dest) 

both strings source and dest denote filenames with path 

we cannot do it for copying entire folder content , this can be done by iterating over files inside the folder
Ok these are ways to simulate shell commands in python

Is there a way to run shell commands directly in python and show output 

Ya

import commands 
cmd = "ls -l ."
(status,output) = commands.getstatusoutput(cmd)
print output

this executes ls -l command on directory .  gives the output as a tuple having 2 things ., status (something like exit code, if 0 it means the command executed without error , if command execution had error it exits by sending 1 or even some other error code like 512 ) and the output of the command 


Interesting feature in python :

List comprehension 

if we have a list a = [1, 2 ,3,4]
then you want to get a new list which has the squares of these numbers ..
what we normally do is iterate through the list then get elements and then put it into the new list 

python has a easy way of doing this

b = [num*num      for num in a]

to put additional qualities like removing elements < 2 

b=[num*num     for num in a     if num>2]

Wednesday 23 May 2012

Python

Dictionary in python :


Dictionary is basically key value binding .
As String is enclosed within " "
As List is enclosed within    [  ]
As tuple is enclosed within  (  )

Here dictionary ,it is enclosed within  {  }

In String , list tuple , also we have this same idea ,,, but there key is always an integer ... so we call it index

So  Stringname or listname or tuplename [1] means , item at index 1

Dictionary gives scope for having having even non integer keys

To declare a Dictionary :   d = { }

>>d['a'] = 'alpha'
>>d['o']='omega'
>>d['g']='gamma'

this is the way to assign value in a dictionary .

Now,  to retrieve a value in String or list or tuple , what will u do ,?  name[index]

similarly here  >>d['a']  displays   'alpha'

The advantage of having key and corresponding value storage is that , it is fast at retrieval using key ,,
this is the advantage of hashtable

But don't know why it is faster in this way .........
 
If you give d['x'] , it flings an error , as the key x is not there in dictionary d .

So , what can be the way to bypass , this even if the key is not there , but still , not flag an error ,,
this can be used in future to check if a key exists in a dictionary ..

Ya, we have a 2 solutions

1) d.get['x]  returns value null if key x is not found and returns the actual corresponding value if the key exists

>>d.get['a']  returns  'alpha'
2) As , we do in lists is ,a =[1,2,3]
to find if 2 is a value inside it ,, we say  2 in a   , returns true if 2 value is there  (note that here we r searching by value , so this time is longer )
Here also same thing

'a' in d  returns true if key 'a' exists , false if it doesn't exist

To see the list of keys at once in a dictionary 

d.keys()

To see the list of values at once in a dictionary

d.values()

To get values ,,, just loop over

for k in d.keys() :
    print d[k]

to have it ordered

for k in sorted(d.keys()):
   print d[k]


To see list of key value pair .

d.items() ...............returns

[('a','alpha') , ('o','omega'),('g','gamma')]

looping over  ? ya , easy
for tuple in d.items() :
    print tuple


File operation in Python : 

f = open(filename , 'r')

the second argument indicates , whether we r opening file for reading or 'w' denotes 'writing .. similary we have 'rU'

f is here the file handler ,,,  by this line we just open the file and give it to f  ,, from f we can read it
now we can iterate each line ,,as soon as it sees a new line in f  ( which is a reflection of file filename which is open ), it stops ,reading ,
like this we iterate over every line

for line in f:
    print line

prints each line but with double line space between

this is because print itself has a new line and the new line in file adds to it ,,
so to eliminate it replace print statement by
print line,

the trailing comma removes this problem as it removes the new line in the print

finally , we have to close the open file ,,as a open file unnecessarily draws memory

f.close()

if u omit this line , when the process ends the file is closed automatical
but in case of softwares which r continuously running , the process also runs , so here if we don't close, it remains open the whole period of time , when software runs ,and that too unnecessarily
what is the advantage of having this concept of reading line by line ? why not read at once

when we read , big files say 90 Gb ,,  reading at once gives problem of locking heavy memory

when we read line by line only a small part of memory is locked for its work ,

f.readlines()

gives o/p as a list of lines

['line1' ,'line2','line3'...]

it even prints it in the same way as shown above

f.read()

reads entire file into a single string

so has same view as we open a file in editor in gui