Sep 7, 2010

Python librarys

OS library - This will provide many number of functons which will interact with the operating system.
>>>import os # import the os library.
>>>dir(os) # can get a list of all module
# functions in the os module
>>>help(os) # will give the man page of os # module

Some important things from os module.

os.chdir('path') - change the current directory
os.chmod('path', 0777) - change the mode
os.chown('path',user_id,group_id) - change owner'path', flag, 0755) open or create file
this will return a file descripter.
os.close(fd) - close the opend file according to the file descripter.
os.execl('file', *args) - excecute a file
os.fork() - make a chiled process
os.fstat(fd) - get stat of the file descripter
os.mkdir('path', 0644) - create a directiory
os.pipe(read,write) - pipe,buffersize)
os.remove('path') - remove file
os.rename(old , new) - rename files

shutil module - THis will do the file management tasks.

import shutil
shutil.move('source','destination') - recursivly move file or directory to another location
shutil.copytree(src,dst,symlinks=False) - recursivly copy a directory
shutil.rmtree(path, ignore_errors=False,onerror=None) -
shutil.recursivly remove files in a direstory

glob - provide wildcard search
import glob

sys.argv - give the command line arguments
sys.exit - directly exit from a script

re module - this provide reguler expresion for python
math module - this provide mathematical functioons defined by the C standards.
random module - this provide randome selection

Sep 6, 2010

Python classes

This is a basic python class.

class MyClass:
def f(self)
return 'Hello world'

Now you can use this as like this.
MyClass.i &
These are now valid atributes and functions.

As well as this you can create an instance and ues the class llike this

x.i &
Are also now a valid atribute and a method.

Writing an __init__() to a class

print x.r x.i

There are no private classes in python

Sep 2, 2010

Python error and exceptions

while True:
x=int(raw_input(Please enter a number))
except ValueError
print Please enter an integer value

This will handle the error if the user inputs an non-integer value.
In a program first the try clause runs. The exception clause runs only if try clause mane an exception.
If an exception occurs two things can be happend. first thing the exception will mach the exception clause and will excecute the code belongs to that thing. The other thing is an exception is not matching our names in the exception clause.

Try except and else
"else" part is a optional.But;

for arg in sys.argv[1:]:
except IOError:
print 'Can not open the file',arg
print agr,'has'len(f.lreadlines()), 'lines'

In a case like this, it is very useful. If the try clause excecute normaly it will go to the else part and excecute that code. This will not create any exceptions

Finally clause
If an exception occures or not, this clause will excecute.

In python it supports to create your own exceptions also.

Python classes

This is the way to write a simple class;

class MyClass:
def f(self):
return 'Hello World'
Now MyClass.i and MyClass.f are valid attributes.

Create an instance of an class.
And now we can use it as;

Git - get some ones project and edit it.

We can download a git project edit that thing. To do that we must;
$git clone remort_URL Our_destinatioon_folder
bob$git clone git:// ~/vlc_clone

This will save a clone of vlc in the directory called vlc_clone.

Now you can edit that clone and commit.
If some other person is also involving on editing this clone he/she can get the Bob's edited git by using;

alise$git pull /home/bob/vlc_clone master

This will merges the changes of Bob's Master branch with Alise's current branch.

alise$git fetch /home/bob/vlc_clone master
alise$git log -p HEAD..FETCH_HEAD
This is more safer than just pulling if alise has not committed her editings.
By typing;
alise$gitk HEAD..FETCH_HEAD
Alise can see what are the changes Bob had done for the original clone.

alise$git remort add bob /home/bob/vlc_clone
By this Alice can add bob's clone without merging with her own branch.

alise$git fetch bob
In this it will simply create an extra branch without merging them with any of Alise's branchers.
In that case we must use:
alise$git log -p master..bob/master
in order to see the changers which Bob had make. Then if Alise need to merg them she can use;
$git pull . remortes/bob/master

Sep 1, 2010

Sed & awk

We can use sed to search for some string patterns. By using sed we can easily modify a string.

Search and replace
The basic command is like this:
sed 's/day/night/' output_fie
This code will search for the keyword 'day'in the input_file and replace it by night then save the changed file in output_file.

sed 's/\(day\).*/\1' output_file
This will search for the day keyword in the input file and it will delete other characters behind the keyword, then it save the data in output_file.

We can use awk for report writing and filtering. Mainly it is a good table creator.

A bacsic structure of a awk command
BEGIN { print "START" }
{ print }
END { print "STOP" }

Try this out;
#!/bin/csh -f
awk '
BEGIN {print "FILE\tOWNER" } \
{ print $8 "\t" $3 }\
END { print "--DONE--"}\ '

This will give the output as

Note :
In awk "$2" is not a variable. It always reffer to a column number.
There are three types of awk.awk, nawk and gawk.

If you want to use a variable use it as,
and in the code;

Python modules

We can create a module file and can use it in some other files. TO do that we must create the module file in the same directory and in our coding we must import it. If the module file is "".
>>>import module_file
Then we can get the variables and methods by;
>>>module_file.variable #or

We can view the thing in a module by doing
>>>import module
This will give all related details about the module.

Inputs and outputs
We can pass parameters when we are executing the python script.
$python argument1 argument2

in the script we can use these arguments by using
if __name__="__main__":
import sys
function(sys.arg[1], sys.arg[2])

Open file
we can use this to open a externel file.
mode can be,
w - write
r - read
r+ - read and write
a - append - will give the size - will read the file
f.readline - will read a line
f.write(string) - will write string to file
f.close() - will close the file

Format an output
for x in range(1,11)
print repr(x).rjust(2), repr(x*x).rjust(3),

this will give the output like this
1 1 1
2 4 8
3 9 27
4 16 64 ....