Monday, April 2, 2012

python scripting basics

#Python
Dynamic mature language... Unix/Linux based scripting language... Indentation is important in python programming language... Python encourages neat programming... Python is Cross platform...Python is amazing programming language... It's supports Object Oriented Programming Language...
High data manipulation capability

- which python
- python
Eg: python 1 + 1, 1 * 3,2 ** 16(expo), 4 -2 ,"Hello World","Hello World\n","Hello\tWorld\n\n\n",'Hello\tworld\n\n'
1 tab = 8 spaces

Press Cntr + D to come out of the Interpretor
help()
- python is great at text parsing... it is loosely typed
- Python is a modular language ... modules are available... import the modules... Then the modules code will be available in your code
- Indentation of branches is very important

name = "gyani pillala"
print name
### SheBang header #!/usr/bin/python
message = "Hello World"
print "\n\n", message ,"\n\n"

#(string,int,float,lists,tuples,dictionary)
Product = "Linux"
print Product
type(Product)

id(Price),Price2 = Price, id(Price2)... The values are same... It will just act like a Symbolic link.

##STDIN
print "first" , "second"  o/p first second
print "first" + "second" o/p firstsecond
print "hello"*3 o/p hellohellohello
message = raw_input("What is your message?") o/p What is your message? Hello world
print message

Eg:
print "------------------- Life Expectancy ---------------------------"
name = raw_input("What is your name? ") //(After printing gives you a new line)
print name",",   // (After printing doesn't go to new line)
age= input("What is your age? ") // Integer Input

# String 1
Inherent ability to manipulate the string
Eg;
len(message)
print message[4]
print message[2:5] //slice operator to print different perspective of a String

>>> for letter in message:
...    print letter

>>> if message == message2:
...     print "They Match!"

 >>> import string
...     string.upper(message) //String is a module... Upper is a Function/Method
...     string.lower(message)
...     string.capitalize(message)  // New string
...     string.capwords(message)  // New String
...     string.split(message) // ['new', 'string']

# String 3
import string

message = "new string"
message2 = "new strings2"
print message
print message, "contains", len(message), "characters"
print "The first Character in", message, "is: " , message
print "Example of slicing", message, message[0:4]
for letter in message:
            print letter
if message == message2:
            print "They match!"

Difference Bet Interpreter and Scripts
Interpreter echo's the values ...whereas script do not... by default

# Lists
numlist = [1,2,3,4,5]
print numlist
numlist.reverse()
numlist2 = [6,7,9]
numlist.append(numlist2)
print numlist // [1,2,3,4,5,[6,7,9]]
print numlist[0]
print numlist[5][0] // Two dimensional list
numlist.pop() // pop up the last element
numlist.pop(3) // pop up the 3rd element
numlist.extend(numlist2)
print numlist  //[1,2,3,4,5,6,7]
numlist.insert(Indexnum,value) // numlist.insert(1,2)
 
range(3) // [0,1,2]
numlist3 = range(10)
range(1,3 ) // [1,2]
range(0,102,3) // Incremented by 3
stringlist = ["GyaniPillala","scripting","Edition"]
stringlist.reverse()
stringlist.append(stringlist2)
stringlist.pop()
stringlist.extend(stringlist2)
stringlist.insert(i,value)

logfile = " 200450050 10.10.10.2 192.168.222.233"
type(logfile) // result says --- str
import string
string.split(logfile)
logfile2 = string.split(logfile) // Contains List
type(logfile2) // result says --- list
logfile3 = string.join(logfile2)
print logfile3
type (logfile3) // type str

print range(10) # returns all values excluding boundary (10)
print range(1,11) # retutrns 1 -10
stinglist = ["l", "b", "c"]
stringlist2 = ["p","q"]
print stringlist
stringlist.append(stringlist2)
print stringlist
print stringlist[3][:]
stringlist.extend(stinglist2) // Just one flat list

logfile2 = string.split(logfile)
logfile3 = sting.join(logfile3)  // Convert the list to string
print type(logfile4)

#Tuples // Are Immutable ... Basically it is a list which is immutable...
product = ["d","f"]
product[1] = "change"
product2 = ('GyaniPillala','Scripting','redhat')
type(product2) // tuple  read-only  tuple doesn't except any changes to the values

# Dictionary
test = {'script':395,'redhat':595}
test['script'] // 395
test['redhat'] // 595
test['debian'] = 400
test // Will print all the contents
test.keys()
test.values()
del test['redhat'] // key to del
for k,v in test.iteritems(): // Nothing significant about k and v.. key & val
     print k,v // print all the values & keys
suiteprice = [333.444] // list
test['suite'] = suiteprice
test // print all the {'suite':[333,444],'scripting':667...

# Conditional |
6 operators...
<,<=,>,>=,==,!=,<>
min,max = 8,9
>>>if min < max:
....   print min, " is less than", max

Note: since python is loosely typed... We don't have to close the brackets.
if min > max:
 print min, " is less than ", max
else:
print "no luck"

if answer != timeleft:
     print "Sorry", name, "That is incorrect"
else:
    print " hai "

#Conditional
import sys
print sys.argv  // Will print the file name itself
print len(sys.argv)
if len(sys.argv) < threshold:
          print " blah.... blah... blah..."
elif len(sys.argv) >=8 :
         print "hello"

# For Loops
for var in list:
Eg;
string1 = "GyaniPillala"
for i in string1:
        print i
list = ["x","u"]
for i in list:
      print i

# While loop
eg;
count = 0
while count <= 10:
   print count
   count = count + 1

while 1:
   print ""
   count = count + 1

while answer != timeleft:
    print " blah blah"
    sys.exit()

#File Input/output |
# open accepts 2 arguments: filename and mode
# modes include: r,rb(read-binary),wb(write-binary),a(append),r+(read-write)
#variable object
hadle = open("data1","r")
print hadle // < open file 'data1', mode 'r' at 0xf5fd520{Memory Location} >  // all the file variable goes under memory...
# readline reads into a string the first line up to \n
print hadle.readline()
temp = hadle.readline()
type(temp)  // string
temp = hadle.read() // will read the entire file
temp = hadle.read(50) // will read only 50 lines
#read - reads entire file into a string, unless num of chars specified
#readlines - will read one line per list element

#File Input/output ||
Note: python by default gives a new line character when we print some thing using "print"

E.g:
han1 = open("data1","r")
han2 = open("data2","w") // If the file exist it will overwrite... If the file is not there will create a new file
tempread1 = han1.readlines()
for i in tempread1:
            han2.write(i)
or // han2.writelines(tempread1)
han1.close()
han2.close() // close the files important... flush the buffers

han1 = open(f1,"r") // f1 = "data1" // f1 = raw_input()
han2 = open(f2,"r" // f2 = "data2" // f2 = raw_input()
han2 = open(f2,"a") // Appending is always a good habit.. If the file doesn't exist ... It will create it...
han1 = open(f1,"r+") // Both reading and writing

# File Input/Output |V
han1 = open("data1","w")
productname = "linux"
productcost = 395
count = 1

# Here the intergers cannot be written file,,, It has to be converted to Strings...
# % Operator when applied to strings performs formatting...
# %s -strings, %d - integers digits, %f - floats
han1.wite("%s %d %d\n\n" % (productname,productcost,count))
han1.close()
while count <= 100:
            han1.write("%s %.2f %d\n\n" % (productname,productcost,count))
            productcost = productcost + 1
            count = count + 1

## Exceptions |
f1 = raw_input("Please specify a filename for processing:")
try:
            han1 = open(f1,"r")
except:
            print "Problems opening file",f1
print  "we 've moved on"

while 1:
 f1 = raw_input("Please specify a filename for processing:")
 try:
  han1 = open(f1,"r")
  break  // to break the never ending loop
 except:
  print "Problems opening file", f1

# import sys
  import string
count = 0
while 1:
 if count == 3
  answer = raw_input("")
  if answer == "yes":
   sys.exit()
  else
   count = 0
f1 = raw_input(" please specify a filename for processing:")
try:
 string.lower(f1)
 han1 = open(f1,"r")
 break
except:
 print "Problems opening file", f1
 count = count + 1

// It's always good to verbalized the code...
# Functions
Encapsulates the code
perform repetitive tasks

python >>> help
>>> keywords

def name():
      Statements
Note: Function has to be called after the definition
Eg:
def hworld():
      print "hello World"
hworld()

def lifeexpect(e,a): // function definition
      timeleft = e - a
      return timeleft  // function return values
 timeleft = lifeexpect(expect,age) // function call

## Modules
python
>>> import sys
>>> dir (sys)
>>> print sys.ps1
>>> print sys.path
>>> import os
>>> dir (os)
>>> print sys.path ( Default location where python searches for modules ) This is a list which can be modified...
cd /usr/lib/python ; ls -l os.p*
os.py - python text file ... Ascii text contents... (ASCII English text )
os.pyc - byte code compiled file for (os.py) file ... When the module is first compiled the byte code will be in os.pyc .
             All the function will executing form pyc file since it is faster .. (data file)  It is very important file will be consulted by the interpreter
os.pyo - Another form of file ...  (data file )

Note: Python is a high level language... It compiles the program into byte code for subsequent execution... Similar to java
Note: Importing methodology...
eg: import sys... Instead of import sys which includes all the function inside the module sys
We can specifically call the functions inside the module ... Which decreases the overhead load...
Eg; from sys import func1 funct2 funct3 ... functn 
why to import all the function.. I am interested in only particular functions...

# SHUTIL
 import shutil
s = shutil
s.copy2("data1","data2")
s.move("data1","temp/data2")
s.copytree(srcdir,dstdir) // copytree will copy dir recursively... copytree will expect that destination dir not exist...
s.copytree(srcdir,dstdir,1) // last argument... creating sybolic link.... 1 - to create a symblic link.. 0 - not to create...
s. rmtree(srcdir)

# Regular Expression
Regular Expression I
- For parsing strings,specific character, group of character
import re
dir(re)
reg1 = re.compile('itsamatch')
reg1.match('itsamatch')

reg1 = re.compile('itsamatch',re.IGNORECASE)
match1 = reg1.match('itsamatch')
print match1.group()

searchstring = "This is a test"
reg1 = re.compile('^T')
match1 = reg1.match(searchstring)
print match1
print match1.group()
reg1 = re.compile('.*')   { . - wildcards begining letter, * continues )
metacharacters - ^,*,.,+,?

reg1 = re.compile('[a-z]')
match1 = reg1.match(searchstring)  // searchstring = " this is test "
print match1.group()  // o.p - t

reg1 = re.compile('[a-z]+')
match1 = reg1.match(searchstring)
print match1.group() // o.p : this

reg1 = re.compile('[a-z]+.*')  // . - space , * - continue the string 
match1 = reg1.match(searchstring)
print match1.group() // o.p: this is a test

# Python escape sequence
reg1 = re.compile('[a-z]+\s') - // for space

# script example
import re
#reg1 = re.compile('hello',re.IGNORECASE) // search for string hello
#reg1 = re.compile('\d+', re.IGNORECASE) # matches verbatim // searches for digits only
reg1 = re.compile('\d+\s+\w+', re.IGNORECASE) # matches verbatim // searches for digits + strings
searchstring = raw_input("Please give us a search string:")
match1 = reg1.match(searchstring)
if match1:
 print match1.group()
else:
 print "NO match"

# script Example
reg1 = re.compile('\d+\s+\w+',re.IGNORECASE) # matches verbatim // matching digits/space/words
c = 1
t = 3
print "Okay, you've got", t, "chances to make a match !"
while c <= t
 searchstring = raw_input("Please give us a search string:")
 match1 = reg1.match(searchstring)
 if match1:
            print match1.group()
            else:
            print "No match"
          c = c + 1

# username @ domain
reg1 = re.compile('\w+@.*')  w=[a-zA-Z0-9] but skipes the . - gyani.pillala
reg1 = re.compile('.*@.*')
reg1 = re.compile('\s+\w+@.*') // Any number of strings,words before @

# substituting digits
reg1 = re.compile('\d+', re.IGNORECASE) # matches verbatim
filename = "data3"
f1 = open(filename, "r")
f2 = open(filename2, "w")
for searchstring in f1.readlines():
 print reg1.sub("2000", searchstring) // substituing...
 nvalue = reg1.sub("2000",searchstring, count=10) // substituing... 10 times in a string... When it has multiple digits...
 f2.writelines(nvalue)

# Syslog Integration
illustrate logging via python
import logging

# definition and instantiation of logger object
logger = logging.getLogger() // object Instantiating

#definition of the handler
# handler .. responsible to deliver the message to the destination
han = logging.FileHandler('log1.log')
#han = logging.FileHandler('log1.log','a')
#definition of the formatted string
format = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
han.setFormatter(format)

#add handler to logger
logger.addHandler(han)

# Model - logger ---> handler ------>
destination(File,Syslog,SMTP,TCP/UDP Sockets, etc )

#invocation of logger
logmessage = "testing logger in Python"
logger.error(logmessage)

# syslog logging
import logging
import logging.handlers

# definition and instantiation of logger object
logger = logging.getLogger() // object Instantiating
# logger ----> han1   ( They lead to different destination )
            ----> han2
#definition of the handler
# handler .. responsible to deliver the message to the destination
han = logging.FileHandler('log1.log')\
han2 = logging.handlers.SyslogHandler() # handler for Syslog takes arguments as well

#han = logging.FileHandler('log1.log','a')
#definition of the formatted string
format = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
han.setFormatter(format)
han2.setFormatter(format)

#add handler to logger
logger.addHandler(han)
logger.addHandler(han2)

# Model - logger ---> handler ------>
destination(File,Syslog,SMTP,TCP/UDP Sockets, etc )

#invocation of logger
logmessage = "testing logger in Python"
logger.error(logmessage)

## netstat -anu
    Syslog by default accepts the udp packets on port number 514

### CGI with Python ###
- Common Gateway Interface
- perl is one of the first dynamic web development on the webserver including python,asp, php(Entirely for the web)
- perl lacks certain securites..
cd /var/www/cgi-bin/[ the location where the scripts has to be kept ]
httpd service appended to /cgi-bin/ /var/www/cgi-bin/   # Subject to Change for different directories
 Eg: http://192.168.1.2/cgi-bin/helloworld.py
Mimetype is something which translate the type with the application... then the browser will know about it.
Eg:
print "Content-Type: text/html"
print


# Globbing
Python Specifes Quick aproaches for searching files/directories, globbing is a nice Quick way to scan the directory
Globbing typically means searching paths,files using different wild cards

import glob
dir(glob) // absolute and relavtive path (absolute - starts from the top of the tree, Relative - start from the current directory )
search1 = glob.glob('*.py') // with in our given Directory
print search1
len(search1) // no. of files
search1 = glob.glob('./temp/*.py') // search in current sub-directory
search1 = glob.glob('fileio?.py') // ? - any digit/character
search1 = glob.glob('fileio[1-2].py')

# script
import glob
query1 = raw_input("Please enter a directory or file Query:")
search1 = glob.glob(query1)
if search1:
for i in search1:
print i
print "Your search contains", len(search1), "result!"
else:
print "Sorry, no matches"


No comments: