Search This Blog

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" :)