Python Tutorial

Thursday, October 28, 2010

Python fetch URL

'''
    This code use python urllib2 syntax
    - This code use for download a page
'''

import urllib2
u="http://www.sust.edu"
f = urllib2.urlopen(u)
data=f.read()
f.close()
print data;

Python Threading Use A Thread Class

 '''
    This code use a thread class
    - Save this class named 'AnyName.py'
 - Run this code you need class 'ThreadClass.py'
'''
import ThreadClass

for i in range(1,4):
    threadObject=ThreadClass.ThreadClass(i)
    threadObject.start()



Output:
output:  output: 1output:  
 23

output:  1
output:  2output: 
 3
output:  1
output: output:   32

Python Threading

'''
    This code use python threading syntax
    - Save this class named 'ThreadClass.py'
'''
import threading
import time

class ThreadClass(threading.Thread):
    def __init__(self,data):
        threading.Thread.__init__(self)
        self.data=data
        
    def run(self):
        for i in range(0,3):
            print "output: ",self.data
            time.sleep(1);

Thursday, August 26, 2010

Python Unzip a tar file

'''
   This class use for Unzip a tar file
'''

import os
import tarfile
import sys

class UnZipFolder():
    def __init__(self, location):
        self.location=location
        # Set output directory here
        self.outputLocation="C:/"

    def decompress(self):
        try:
            if os.path.exists(self.location):
                decompressTar = tarfile.open(self.location)
                decompressTar.extractall(self.outputLocation)
                decompressTar.close()
                print "Extracted"
            else:
                print "No Such Folder"
        except:
            print str(sys.exc_info())
            
if __name__=='__main__':
    # Set input directory here
    location="C:/test.tar"
    unZipFolder=UnZipFolder(location)
    unZipFolder.decompress()

python zip a folder or file



Zip a folder or file is very easy in python. You need to set variable location by your file or folder location.

'''
    This class use for zip a folder
    - Create a folder named "testFolder" on C drive or reset the location
'''

import os
import tarfile
import sys

class ZipFolder():
    # Constructor of ZipFolder
    def __init__(self, location):
        self.location=location

    def makeCompress(self):
        try:
            if os.path.exists(self.location):
                compressTar = tarfile.open(self.location+".tar", "w:gz")
                compressTar.add(self.location)
                compressTar.close()
                print "Compress complete ",self.location
            else:
                print " (ZipFile)No Such Folder ",self.location 
        except:
            print str(sys.exc_info())

if __name__=='__main__':
    location="C:/testFolder"
    zipFolder=ZipFolder(location)
    zipFolder.makeCompress()

python multidimentional array

'''
   This code use python array syntax
'''
# declare a 2D array
array = [ [0 for col in range(3) ] for row in range(3)]

print array
# update array
array[1][1]=5
print array



Output:
[[0, 0, 0], [0, 0, 0], [0, 0, 0]]
[[0, 0, 0], [0, 5, 0], [0, 0, 0]]

python set

'''
   This code use python set syntax
   - Set always holds unique data/object
'''
data=[7,1,2,3,1,4]
setA=set(data)
# Unique numbers of setA
print "setA: ",setA

data=[9,0,5,3,2,9]
setB=set(data)
# Unique numbers of setB
print "SetB: ",setB

'''
   All elements either in setA or setB
'''
setC=setA | setB
print setC

'''
   All elements both in setA and setB
'''
setC=setA & setB
print setC

'''
   All elements in setA or setB, but not both
'''
setC=setA ^ setB
print setC

'''
   All elements in setA but not in setB
'''
setC=setA - setB
print setC



Output:
setA:  set([1, 2, 3, 4, 7])
SetB:  set([0, 9, 2, 3, 5])
set([0, 1, 2, 3, 4, 5, 7, 9])
set([2, 3])
set([0, 1, 4, 5, 7, 9])
set([1, 4, 7])