Search This Blog

Sunday 26 October 2014

command line argument in Python using argparse

ArgParse:

     In Python we can do any programming either console program or GUI programming. we can also take inputs from command line directly without halting the execution of the program. taking the input from command line using argument is called "Command line argument".
     In Python we can achieve using "argparse" module.

How to install argparse?
       argparse module is a inbuilt module in every Python version. (Linux users please refer how to install argparse module.)
     
How to add command line argument using argparse?
      Adding argument to Python program is much more easier using argparse. before going to the actual explanation of the classes and functions, let us see one small example:

import argparse

class Parser( object ):

    def __init__ (self):
        self.parser = argparse.ArgumentParser( description = 'This is for Test to run program\nProcess some integers')
        self.setParseArg()
        pass

    def setParseArg(self):
        a = 1
        self.parser.add_argument('-s', action='store', dest='simple_value',
                    help='Store a simple value')

        self.parser.add_argument('-c','--const', action='store_const', dest='constant_value',
                            const='value-to-store',
                            help='Store a constant value')

         self.parser.add_argument('-v','--version', action='version', version='%(prog)s 1.0')

        results = self.parser.parse_args()

        print 'constant_value   =', results.constant_value
        print 'simple_value   =', results.simple_value
       
  
if __name__ == "__main__":
    parser = Parser()
    parser.parser.print_help()

In above example, we have created a parser called "self.parser" which is instance reference variable of ArgumentParser which is available in argparse module.

ArgumentParser is a class which is used to create a parser for reading command line argument with the help of options.
self.parser = argparse.ArgumentParser( description = 'This is for Test to run program\nProcess some integers')


In ArgumentParser class we have lot of parameter like 'description','help' etc. for more information, please refer https://docs.python.org/2.7/library/argparse.html

This argparser instance is used to add argument using "add_argument" method which is available in "ArgumentParser" class.

 self.parser.add_argument('-c','--const', action='store_const', dest='constant_value',
                            const='value-to-store',
                            help='Store a constant value')



Using add_argument we can add argument to command line as mentioned above.
 we can add '-c' as a short argument or '--const' as  long argument to command line. where,

     action         -- is to specify what it has to store in provided destination
     destination -- destination of the given argument where it has to store in program
     type            -- type of the variable ex: int, float
     help            -- help for the given argument or how to use this argument.

actions:
argparse has many actions:

store_const: which is used to store only constant  to given parameter from command line argument.
                     giving value which is not constant will give exception and exit the program with                                   warnings
store_true: It is used to store only true value
store_false: used to store false.
store: used to store any actions.
append - This stores a list, and appends each argument value to the list
version-  used to take version of the program and exit the program

let us consider that above program named as test.py
when you execute the program like
>>> python test.py -c 4
ouput will be:
constant_value   = 4
simple_value   = None


If you want to take more than one value to single argument as list, then use nargs and give input to argument with space 
for more information about nargs please refer official documents.

Friday 25 April 2014

Decorators

Hi,

Here we will learn what is decorator.
Before that, you may think about decorators are same as Annotations in Java which follows '@' symbol. but in java, Annotation is like telling something to JVM. But here, in Python, Decorators are more than what you are expecting. It is not only just a representation, but also a powerful tool to alter your class, function or any object's functionality on the fly at runtime.
    
    OK, now what is Decorator?
                    Decorator is function or class, which takes another function/class as argument and applies extra features to existing and returns new class/functions.

Simply, it is a process of changing object(everything in Python is Object) at run time.

first we will discuss about inbuilt decorators and then, we will learn how to create our own decorators in Python.

1. @staticmethod
2. @classmethod

1. @staticmethod: By using this decorator, we can call methods from class directly by using class name.
for this, we do not need to create instance of class.

>>>class MyStaticClass(object):
              def  __init__(self):                     
                      pass

              @staticmethod
               def print_value():
                     print 'this is static method'

>>>MyStaticClass.print_value()
this is static method 

In this example, we are not creating any instance of the MyStaticClass class, simply we are calling method direcly by using class name. note that, since it is not instance member, Python never pass instance reference to that method. that is the reason why we are not providing a first default parameter in method which are declared with @staticmethod decorator.

2. @classmethod : This is also predefined decorator which is useful to bind method to class instead of instance. we can call these methods by using class name or using instance or both.

>>>class MyClassMethod(object):
               val = 5
               def __init__(self):
                     pass
               
               @classmethod
               def print_value(cls):
                     print 'given value is '+str(cls.val)

>>>mycls = MyClassMethod()
>>>mycls.print_value()
given value is 5
>>>MyClassMethod.print_value()
given value is 5

Here, we can call class method either by using class name, or by using instance.
This kind of usage is very helpful to create instance on the fly in the middle of program excution.

How to create our own Decorators ?

       In Python, it is possible to create our own decorators to alter object on the fly.

We can create decorators in two ways:
            1. class decorators
            2. function decorators

1. class decorators:  In this case, we can use our own class as a decorator to the another functions.
when compiler goes to the function compilation which is being decorated, it compiles the function first and sends the function object to the class decorator for producing substitution of the function.

       Only constraint on decorators object return is it should be a function. That is it should be callable. for these we need to override __call__() method in our decorator class as follows:

>>> class MyDecorator(object):
          def __init__(self,func):
              print "inside __init__() method"
              func()
             
          def __call__(self):
              print "inside __call__ () method"

>>>@MyDecorator
   def decFunction():
       print "inside function"

output:
inside __init__() method
inside function
inside __call__()

@MyDecorator
def decFunction():
      ......

is equal to 

defFunction = MyDecorator(defFunction)

simpley, it is a process of sending function object through another function or class and assign the result to the original function


2. function decorators:   when you are using decorators, only constrain is it should be callable. i.e. every decorator class should have to override __call__() method.

     Instead of overriding __call__() special function, we can rewrite decorator as follows:

>>>def entryExit(func):
       def new_f():
           print "entering function decorator"
           func()
           print "exit function decorator"
       retunr new_f

>>>@entryExit
   def function():
       print "inside function"

output:
entering function decorator
inside function
exit function decorator

Note that new_f() is a closure because it is capturing the func() method properties

behind the scene, Python compiles function method and sends to entryExit decorator as a parameter. it will executes as closure and output will come as new modified function execution.

By using decorator, we can create an instance which will acts as a another decorator for another class.
simply, decorators are very easy and very complex to use. 
a simple decorators are like "function acts as decorator to another function"
a complex decorators like "a decorator which is a decorator of decorator's decorator and returns decorator's decorator"

 simply, decorators are very powerful in Python.

 

Thursday 10 April 2014

How to install Python2.7 in linux machines ?

Installing Python in windows is just one click matter. Since we do not have any binaries for Linux machines. it is complicated than windows installation.
for this, we have some sort of process which is easy way to install in Linux machines irrespective of flavours.

Before processing with installation process, please download Python tarball  file from official website. if you do not found link, please click here.

Once you got the tarball file, please untar the file by using following command:

$ tar xzf Python-2.7.2.tgz
$ cd python-2.7.2


Before starting installation process, we have configure our Python with system as per our system dependences.
for these, type the following command

$./confure 

By this, Python will configure all the required system dependencies to the machine.

By using 'make' command, we have to compile the source code of Python but it will compile python for /usr/local/ which requires root permission. To install in our selected directory, please follow the following command:

$make altinstall prefix=~/usr/local exec-prefix=~/usr/local

By this command, python source file will be compiled to <home directory>/usr/local folder. at this moment, user may get the following warning which is causing a break point of your installation process




To avoid these "clock skew detected " warnings, simply retype the above "make" command untill you get modified time to 0.00

there is an another process to avoid these kind of warnings.
to remove these warnings, in Linux, we have "touch" command which simply changes the time stamp of the provided file to system time

$ touch <filename>

or else, you can change system date using following command :
$ sudo date <mmddhhmnyyyy>

where mm-month in 01-12 format
          dd-date of the month
          hh-hour of the current time, which should be 24 hours format
          mn-minutes
          yyyy- year

Another way to install property and remove above error is,
Just 'cd' into the directory where the files need a time-stamp update.
             Next use the following command which will update the time-stamps of all the files in the   directory:
      
          $find . -exec touch {} \;

once you have enter this command, it will try to modify entire files time stamp in provided folder. after this, once again run 'make' command. this time it will run successfully without errors warnings.

Ok, till now went good, bu as you already know, every Linux flavor comes with inbuilt python version either 2.5 or 2.6 version of python. if you have installed latest version, by entering 'python ' in terminal, it should launch your python rather than in built python. for these, we need to  link installed python to available python for making a copy for new version

$ ln -s python2.7 python

now, we need to create alias for installed python as follows:

 $alias python = '~/usr/local/bin/python'

now enter python in terminal and see the version. it will give latest version which is installed just now.
to make alias permanent, enter above alias line in .bashrc file and restart the computer for effect of configuration changes.
$vi .bashrc # enter alias in this file

Friday 31 January 2014

Oracle connection in Python

This post briefly explains about how python can able to connect Oracle database.

unlike other object oriented programing languages, python has better way to connect Oracle database with simple steps. this includes:

        1. JPype 
        2.JayDeBeApi


Since Oracle connection is much more easy in Java than other OOP languages, direct connection will be little bit expensive. So, for this, JPype is the best third party module which can be available for free to download.
before going into the actual details of the Oracle connection, we will learn what is JPype and Jaydebeapi.

JPype:
         JPype is a third party module, which will be useful for integration of Java to Python. By using JPype, we can access full Java library including odbc and other database connections. 
        Yes, you may have one doubt that why we are interesting in JPype , why not JPython (formally known as Jython) ? 
        As we know that, Jython is always  suffers from a large number of drawbacks, i.e. it always lags behind CPython, it is slow and it does not allow access to most Python extensions. this is the reason, we are moving to JPype.

 JayDeBeApi:
          This is also third party module which is useful to connect database using java JDBC. It provides a Python DB-API v2.0 to that database. By using this module, we can connect to oracle database and it is possible to get connection object for future use.

By using these two modules we can make Oracle database connection very easily. 

After downloading and installing these two modules., 

1. Import modules
2. set path environmental variable for JRE
3. start JVM (Java Virtual Machine) by using JPype
4. make connection using JayDeBeApi
5. Get cursor object and do queries on database.

following is the abstract code in Python which is useful to make connection to Oracle database.

     import jpype, jaydebeapi
     import os
     os.environ['JAVA_HOME'] = "<path to jre>"
     jpype.startJVM("<path to jvm.dll>","<path to ojdbc6.jar>")
     conn = jaydebeapi.connect('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:    <username>/<password>@<server IP>:<port>/<SID>')
    cur = conn.cursor()
    cur.execute("<your query to database>")
    conn.commit() #for commiting queries
    

And remenber that ulike SQLIte3 and PostgreSQL, Oracle database connection's cursor object can not be iterate. for this, we need to fetch data by using fetchall() or fetchone() methods, and insert into list and process the data

 Ex:
        data = cur.fetchall()
        for items in data:
             <your operations>



By this, we can make connection to Oracle database with Python.

Iterators and Iterable


Hi ,

here I am going to tell about iterators and iterables in Python. Most of the Python developers are getting confuse what is iterator, iterable and when to use it.

Here  I am explaining in brief which may helpful to get basic knowledge of both.


Iterable

"An object capable of return its members one at a time"

Let me explain more clear.,

If any object is returning its item at a time either by iterating or by returning, it is called Iterable.
for example:: list, str, tuple ----> sequence types
                    dict, file ---------> non-sequence types. For more information about data structures, please click here

One can do this, by implementing __iter__()  method for returning items.


Iterators 

"It is an object which is representation of stream of data."
Confused...!

basically, It can be a user defined or pre-defined which will be having data and can able to send those data by using next() method for taking items. Iterator works till the end of the data. If their is no item, then it will raise stopInteration exception. Once the stopIteration exception came, then the loop will exit.

      When we are talking about Iterators, everyone will think about the iterator's data. but consider a situation, where we require iterator object itself. then, how you will pass object itself?

You can do this, by implementing __iter__() method  which will return iterator itself. As mentioned above, if any object is implementing __iter__() method, it is called  "Iterable". i.e., "every iterator is iterable" :)