events and probability

1. is equally likely events
2. is mutually exclusive events
3. is find complement(A) event
4. is union of events (A or B or both)
5. is intersection of events (A and B)

python to download zipfile and unzip


#This simple script can use for download zip file from any url and unzip data. you need to have wget utility to download file.

import sys

import time
from urllib.request import FancyURLopener
import os

from datetime import datetime,date

import calendar
from os import path
import zipfile

def unzip(zf):
print("================unziping"+zf+"================")
zip = zipfile.ZipFile(zf,"r")
zip.extractall()


def runsystemcmd(cmd):
print("Cmd : " + cmd)
os.system(cmd)

class TestOpener(FancyURLopener, object):
    version = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; it; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.11',
            
def downloadFile(url):

starttime = time.time()
 testurlopen = TestOpener();
 response = testurlopen.open(url)

 meta = response.info()
 logging.error(meta)

 urlcontentlen = meta["Content-Length"]
 if urlcontentlen==None:
  urlcontentlen = 0
 outfile = url.split('/')[-1]

 f = open(outfile, "wb")
 read_bytes = response.read(1024*10)
 while read_bytes:
  f.write(read_bytes)
  read_bytes = response.read(1024*10)
 f.close()
 print("Elapsed Time: " +str(time.time() - starttime))




def unzipfile(zf):
if(os.path.exists(zf)):
unzip(zf)
os.remove(zf)
else:
print("File not exist " + zipfile)


if __name__ == '__main__':
    url="put the  zipfile url here"
    downloadFile(url)
    zf = url.split('/')[-1]
    unzipfile(zf)
    print("Finishing current downloads")



Shell script and Unix





check exit status of scripts or commands
commands/scripts exit status stored in $? variable

ret=$?
if [ $ret -eq 0 ];then
   echo "script/command success!"
else
   echo "script/command failed"
fi

read user input from console

read input
echo $input

check file exist or not
if[ -f filename]; then
echo "file exist"
fi

check file executable 
if[-x filename]; then
echo "file is executable"
fi

timestamp function 
datetime() { echo `date "+%Y-%m-%d %H:%M:%S"` ;}
echo "Starting process :" $(datetime)
echo "Ending process :" $(datetime)

date +%D:%T
get count seconds
date +%s like unix time stamp

Some Unix Commands

kill process of a user
pkill -u userid
pkill -u viru
kill a single process
kill -9 pid
find user process
ps -ef | grep userid
find cpu intencive process
top
list file and directory
ls
ll



referances 
Advanced Bash-Scripting Guide http://www.faqs.org/docs/abs/HTML/index.html


======================================================================

======================================================================