PyBash

Identation, is what important in Python.

Python can be used for both CGI and Scripts

Python scripts are usually stored in file with .py extension. In linux there is no significance with any extension. It is just naming convention to understand that the file contains python script.

In python script – mention the python shebang header – this will tell the  environment to use the Python Interpreter.
Example – #!/usr/bin/python
The usage of shebang header is useful to create a standalone executable script like :

./myscript

Else we have to mention the python interpreter:

#python myscript

It is also common in a linux environment to have multiple versions of the python interpreter.

The Print Statement
-We can give arithemetic operations
-We have to use ” ” or ‘ ‘  to print strings

Help
-On python interpreter enter “help”
-Example:
>>>help
help>keywords (To list out keywords)

Data types

  1. string
  2. int
  3. float
  4. lists
  5. tuples
  6. dictionary

To find the type of value in a variable use Type command

ExampleVairable = "Unni"
type(ExampleVariable)
<type 'str'>

Arthimetic Precedence

PEMDAS – Parethesis Exponent Multiplication Addition Subtraction

Strings:
1.Concatenation using  +

Example I - #print "first" "second"
first second
Example II #print "first" + "second"
firstsecond

2.String Multiplication

#print "Hello"*3
output - HelloHelloHello
#print "Hello\n"*3
Hello
Hello
Hello
#print "Hello\t"*3
Hello Hello Hello

Keyboard Inputs
Python uses 2 funtions for this purpose
1.input()     : used to collect integers
2.raw_input() : used to collect any data types

Example

message = raw_input("What is ur message? ")
var = input("Enter an integer ?")

String Manipulation
#message = “new string”
1.len()

#len(message)
10

2.string extraction

#print message[0]
n

A range of string can printed using “:”
NOTE: Strings are immutable ie once assigned they cannot be changed.You can create a new variable with a variation of string but you cannot change the string.
example : message[0] = m – will throw err.

Importing a String Module

import string

1.Change all to upper case

#string.upper(message)
NEW STRING

2.Capitalize

#string.capitalize(message)
New string

3.Capitalize each words

string.capwords(message)
o/p = New String

4.Split & Join

#string.split(message)
['new' 'string']

#string.join(message) NOTE:the list is converted to string provided the list pure list not nested list.
'new string'

This is important to create a list and store them.(useful for log analysis)

Last Note example – Python Script

#!/usr/bin/python
import string
message = "new string"
print message, "contains", len(message), "characters"
print "The first character in message is " message[0]
print "Example of slicing" , message, message[0:4]
for letter in message:
print letter

Lists

List allow store values – string , integer etc in separate delimited by ‘,’
List is used in conjunction with string – by splitting strings

Define a empty list :

numlist = []

Define a List with values:

#numlist = [1,2,3,4,5]

#print numlist
[1,2,3,4,5]

There are some methods that can operate on List.
1.Reverse Method

#numlist.reverse()
#print numlist
[5,4,3,2,1]
numlist2= [6,7,8,9,10]

2.Append Method

#numlist.append(numlist2)
#print numlist
[1,2,3,4,5,[6,7,8,9,10]]

Now numlist is a nested list.

#print numlist[0]
1

But at index 5 is a list not integer , hence when we enter

print numlist[5]
[6,7,8,9,10]

Now to address each element in the nested list we have to call using 2 dimension arrays such as :

#print numlist[5][0]
6

3.Pop Method of List
pop function will remove the last value in last in first out fashion. This will treat the list as a stack

#numlist.pop()
[6,7,8,9,10] is removed

#print numlist
[1,2,3,4,5]

4.Extend Function
Used to increase an existing list – one flat list

#numlist.extend(numlist2)
#print numlist
[1,2,3,4,5,6,7,8,9,10]

5.Insert Function
Used to insert a value.
Syntax – insert(index,value)

print numlist
[3,4,5,6]
numlist.insert(0,1)
print numlist
[1,3,4,5,6]

How all of these used normally is in logs – they read in using python, split using string funciton, convert them into list, then use list function to manipulate. Like dont want the 1st column and wants the 5th column and the form a modified version of the log.

Range Function
Range is used to create a sequence of numbers starting from 0

numlist3 = range(11)
print numlist3
[0,1,2,3,4,5,6,7,8,9,10]

or

numlist3 = range(1,11) - to mention range in range function.
print numlist3
[1,2,3,4,5,6,7,8,9,10]

or

numlist3 = range (0,100,40) - to mention the increment value of 40
print numlist3
[0,40,80]

List Using Strings

stringlist = ["Unni","SR"]
print stringlist
['Unni','SR']

All list methods used above also applies here.

stringlist.reverse()
print stringlist
['SR', 'Unni']

stringlist2 = ["abc", "def"]
stringlist.append(stringlist2)
print stringlist
['Unni', 'SR', ['abc', 'def']]

print stringlist[0]
Unni

print stringlist[2][0]
abc

print stringlist.pop()
['Unni','SR']

print stringlist.extend(stringlist2)
['Unni', 'SR', 'abc', 'def']

stringlist.insert(2,"Hi")
print stringlist
['Unni', 'SR', 'Hi', 'abc', 'def']

An Example Scenario :
Pass a line of apache log to a variable, say if it is :

logfile = "20040122 192.168.1.1 80 GET index.html"
import string
logfile2 = string.split(logfile) # this will convert the value of logfile into a List data type
print logfile2
['20040122', '192.168.1.1', '80', 'GET', 'index.html']

Now we can check the data type of logfile and logfile2

type(logfile)

type(logfile2)

This is important as now in logfile2 all list methods are available like appending, poping, etc.

All of the values of the list variable can now be accessed using the index variable.

print logfile2[0]
20040122

print logfile2[0:2] or logfile3 = logfile2[0:2]
['20040122', '192.168.1.1', '80']

Finally we have to convert back to string

logfile4 = string.join(logfile3)
print logfile4
20040122 192.168.1.1 80

type(logfile4)

DICTONARY

Tuples are immutable compared to Lists. Its like a read-only version of Lists.

Examples:

#products = ["doll", "bag", "cake"]
#products2 = ("doll", "bag", "cake")

#print products
['doll', 'bag', 'cake'] = These are Lists.

#print products2
('doll', 'bag', 'cake') = These are Tuple which are immutable

#type (products2)

These tuples are used in Dictonaries, because Dictonaries requires immutable data types for its key-value construction.

Example for Dictonaries

#items = {'Apple':85, 'Orange':50}

NOTE : Keys has to be unique.

#print items
{'Apple':85, 'Orange':50}

To add a new Key-Value pair

#items['Grape'] = 55

To change values of existing key

#items['Apple']=90

#print items

{'Apple':90, 'Orange':50, 'Grape':55}

items is an object of Dictionaries in the Object Oriented world of Python, Therefore there are methods that can be used for Dictonaries.

To list all keys in a dictionary object

#items.keys()
['Apple','Orange','Grape']

To list all values in a dictionary object

#items.values()
[90,50,55]

Delete a key

#del items['Apple']
#items.keys()
['Orange', 'Grape']

OR

#print items
{'Orange':50,'Grape':55}

To iterate through all key-value pairs in Dict using a for loop

for k,v in items.iteritems():
print k,v

Output:
Orange 50
Grape 55

Now the value in key-value pair of Dictonary can also be a LIST ie

#suiteprice = [100,200]
print suiteprice
[100,200]

items['Suite'] = suiteprice
print items
{'Suite':[100,200], 'Orange':50, 'Grape':55}

Iterate using For loop

for k,v in items.iteritems():
print k,v

Output
Suite [100, 200]
Orange 50
Grape 55

CONDITIONAL TESTING

IF Loop syntax

if min < max: #conditions can be extended using 'and' 'or' etc. Ex : min < max and min = max
print abc
else:
print def

if min = max:
print abc
elif min < max:
print def

sys.argv

Usage I
for i in sys.argv:
print i

Usage II
arglen = len(sys.argv)

Usage III
print sys.argv[0] #this is print the script file name on screen.

Combining Numbers and Strings

Take a look at this program, and see if you can figure out what it’s supposed to do.

print ("Please give me a number: ",)
number = raw_input()

plusTen = number + 10
print ("If we add 10 to your number, we get " + plusTen)
This program should take a number from the user, add 10 to it, and print out the result. But if you try running it, it won't work! You'll get an error that looks like this:
Traceback (most recent call last):
File "test.py", line 7, in
print "If we add 10 to your number, we get " + plusTen
TypeError: cannot concatenate 'str' and 'int' objects

What’s going on here? Python is telling us that there is a TypeError, which means there is a problem with the types of information being used. Specifically, Python can’t figure out how to reconcile the two types of data that are being used simultaneously: integers and strings. For example, Python thinks that the number variable is holding a string, instead of a number. If the user enters 15, then number will contain a string that is two characters long: a 1, followed by a 5. So how can we tell Python that 15 should be a number, instead of a string?

Also, when printing out the answer, we are telling Python to concatenate together a string (“If we add 10 to your number, we get “) and a number (plusTen). Python doesn’t know how to do that — it can only concatenate strings together. How do we tell Python to treat a number as a string, so that we can print it out with another string?
Luckily, there are two functions that are perfect solutions for these problems. The int() function will take a string and turn it into an integer, while the str() function will take an integer and turn it into a string. In both cases, we put what we want to change inside the parentheses. Therefore, our modified program will look like this:

print ("Please give me a number:",)
response = raw_input()

number = int(response)
plusTen = number + 10

print ("If we add 10 to your number, we get " + str(plusTen))

Note
Another way of doing the same is to add a comma after the string part and then the number variable, like this:

print ("If we add 10 to your number, we get ", plusTen)

or use special print formatting like this:

print ("If we add 10 to your number, we get %s" % plusTen)

or use format()

print ("If we add 10 to your number, we get {0}".format(plusTen))

That’s all you need to know about strings and variables! .

Note:
input and raw_input function accept a string as parameter. This string will be displayed on the prompt while waiting for the user input.
The difference between the two is that raw_input accepts the data coming from the input device as a raw string, while input accepts the data and evaluates it into python code. This is why using input as a way to get a user string value returns an error because the user needs to enter strings with quotes.
It is recommended to use raw_input at all times and use the int function to convert the raw string into an integer. This way we do not have to bother with error messages until the error handling chapter and will not make a security vulnerability in your code.

List is like a clay. You can put it into any form.
String is like a stone. You cannot modify it but create temporary stuff for visuals.

More on SYS.ARGV

It is used to collect the arguments that are entered while executing the script
Example I

#!/usr/bin/bash
import sys
#To check the number of arguments
if len(sys.argv) < 3
print "At least 2 arguments is required"
#To list out all arguments
for i in sys.argv:
print i
#To print the name of the script
print sys.argv[0]
#END

FOR LOOPS

#To print all characters in a string

name = unni
for var in name:
  print var

#To print all values in a List

list = ["apple",33,"orange",55]
for var in list:
  print var

#To form list dynamically from a log file

logfile = ["a b c d e f g", "1 2 3 4 5 6 7"]
logfile2 = []
for i in logfile:
logfile2.extend(string.split(i))
print logfile2

Similarly sys.exit() function can be used to exit a program.

WHILE LOOP

Example

count = 0
while count <= 10:
  print count
  count = count + 1

Example II – Indefinite Loop

while 1:
  print "Indefinite Loop"

FILE I/O

open function is used to open a file. There are several modes in which a file is being opened.
They are – r(read), rb(read binary), w(write), wb(write binary), a(append), r+(read write)
The output open() is stored in a variable, as the variable is considered as an object of open()
Example :

han1 = open("data1", "r")
print han1

Now han1 is a fileobject which has functions of its own like:
1.readline() – reads into a string the first line upto \n
print han1.readline()
2.read() – will read the entire file and store into one string unless the number of characters specified
print han1.read()
print han1.read(50) – it will print upto 50th character.
3.readlines() – will read one line per list element.
print han1.readlines()

File Handlers
han1 = open(“data1”, “r”)
han2 = open(“data2”, “w”)

We can combine both file handlers though. Here data2 file while be created if it doesnt exist or overwrite if it already exist.

Example Script

#2 File handler - han1=read , han2=write
han1 = open("data1", "r")
han2 = open("data2", "w")
tmpread1 = han1.readlines()
for i in tempread1:
  han2.write(i)
han1.close()
han2.close()

Functions for write object handler:
1. write() = can write strings.
2. writelines() = can write multiple lines into a file. No need for a FOR LOOP.

When we want to write to file from varialbes, write() functions only accepts strings. So some formatting is required.
% operator when applied to strings performs formatting
%s – string , %d – integer, %f – float

Example Program

#!/usr/bin/python
han1 = open("file2.txt","w")
product = "Apple"
cost = 332
count = 1
han1.write("%s %d %d\n" % (product,cost,count))
han1.close()

After the % what we have mentioned is a tuple.
Running Linux command in Python

OS SYSTEM will allow to use linux commands

import os
os.system("echo Hi")

COMMANDS GETOUTPUT will allow to store linux command output to variable

import commands
x = commands.getoutput("echo Hi")
print x

PASS means do nothing

print "Hi Man"
while 1:
  pass

The above python program will execute indefinitely until it gets a keyboard interrupt.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s