Friday, June 12, 2009

Oracle + Python



It's been a long time I have visited Oracle web site. As, now a days I am not really working on technical stuff and some how missed visiting the site. When I was Googling about how I can connect to database from Python, interestingly I found some Oracle links. I just glanced through and really felt happy to see something about Python on Oracle site. It didn't mean that Oracle started promoting Python but it definitely started considering one of the fastest growing language. One more exciting news is a new forum has been started on OTN network for Python. The forum has many questions regarding cx_Oracle (a Python extension module that allows access to Oracle, you will see more posts on it soon) as of now. Cheers database programmers, now you have something new to try. Enjoy the following links and keep looking the space for more updates.


Article: http://www.oracle.com/technology/pub/articles/devlin-python-oracle.html

Forum: http://forums.oracle.com/forums/forum.jspa?forumID=376

Feed: http://delicious.com/OracleTechnologyNetwork/python


Wednesday, June 10, 2009

Difference between Apache Server and Tomcat Server

I was trying to use python for web programming and found I need some web server to execute them. When I went into to more details, Apache is recommended. So, I just wanted to check my system before installing a new software and I found Apache Tomcat server is installed already in my computer. It is installed along with the IBM Eclipse in fact. Then, I just jumped and tried to configure the Tomcat server to run Python scripts. I could not do it as there were no mentioned files in the TomCat Server. Then I caught some web developers around my desk and asked what is the difference between Apache Server and Tomcat Server? and I was answered "Apache is an organization/company and Tomcat is a product", I got the same answer may be with different words. Then I went back and tried the setup again (I didn't want to install another software, if I had already something to work with). Oooops, it didn't work again. At last I wanted to contact my Google uncle and here is the answer what I am looking for probably you too:

Apache Server:

~The Apache Httpd Server commonly referred to as Apache is powerful, flexible, HTTP/1.1 compliant web server

~Apache is primarily used to serve both static content and dynamic Web pages on the World Wide Web.

~Implementation: C

Tomcat Server:

~Apache Tomcat is a servlet container developed by the Apache Software Foundation (ASF). Tomcat implements the Java Servlet and the JavaServer Pages (JSP) specifications from Sun Microsystems, and provides a "pure Java" HTTP web server environment for Java code to run.

~Implementation: Java

So, the answer from my friends was not wrong, but there are two different web servers with different features. Finally, I realized that I could not work with Tomcat and downloaded and installed Apache server to work with Python for web.

Tuesday, May 12, 2009

Commenting in Python
 
Single Line Comments:
 
"#" is used to  any line python programs.  Whenever Python finds a "#" in a line (except it is inside a string), the line from that character will be treated as comment. The comment ends with the line.  This is as same as "//" comment in CPP.
 
Examples:
>>> print "Hello" # Comment1
>>> # entire line is comment here.
>>>
 
 
Multi Line Comments: 
 
Multi line comments can be placed between """ (three single/double quotes).  See example below. The lines between these set of quotes are treated as comments.  
There is one more difference between single and multi line comments in Python.  That is multi line comments are not encrypted in the compiled/interpreted runtime file(.pyc).  So users can read them by simply opening the .pyc file.  But single line coments are not readable from .pyc files.  Thats why generally multi line comments are used for documentation/Help. 
Take care while using multi line comments, these should always start in a new line,  other wise this will be treated as strings.
 
Examples:
test.py
-------
""" This program demonstrates the multi line comment usage.
the code do nothing except assigning a value to a variable and printing back.
Run the code with following command
Python test.py"""
a = 10
print a
 
Summary:
~ Two types of commenting available in Python
~ Single Line with # or Multi Line with """ or '''
~ Multi Line comments are generally used for documentation or help because these are available even in runtime/compiled .pyc files.
~ Always start a multi line comment in a new line
 

Wednesday, May 06, 2009

Python Variables -- Declaring & Using
 
Variable is a name for a memory location where we store/keep some value. It is very easy to work with variables in Python.  Python does not need any "Declaration" of variables before using them.  Python is "not stongly typed".  I mean, in a languages like C/Jave we need to declare a vairiable specifing the type of the variable before using it.  In Python a variable is created whenever some value is assigned to it.  The type of variable is also dynamic.  We can assign any type of value (string/number/list/etc.) to the same variable in the same program.  This gives a great flexibility of using variables, but sometimes wrong values can be assigned to a wrong vairable.  Here are some points to note about variables:
 
~ No Declaration needed, assign value to create a variable
~ Any type of value can be assigned to a variable
~ Alphabets (a..z, A..Z), Digits(0..9) and underscore (_) can be used in the variable names.
~ Variable names should start with alphabets or underscore.
~ Don't know how long a variable name can be. 
~ Variable names are case sensitive
~ There is some special meaning of using underscores at the starting of a vairable name, we will discuss that in later posts.
~ Can not refer a variable without creating it.
 
Examples:
 
>>> a = 10
>>> print a
>>> a = 'Python'
>>> print a
>>> a = [1,'a',2,'b']
>>> print a

Sunday, May 03, 2009

Python Data Types

Now we will see what data types does Python supports, the list is here.   The 2nd part of the post talks about how to declare and use the variables.  Please read till the end if you want to do any experiments with different data types. 

1.     Numbers 

This data type is used to work with numbers. I don;t call this as Integer data type, as many books say.  The number data type supports both whole numbers (Integers) and also the real numbers. There are no different data types for integers and real number.  It is easy to define a number variable,  just assign the number as shown below
>>> i = 10
>>> r = 1.2
>>> print i*r

2.     Strings

This data type is used to work with text.  There is no special data type in python to declare characters.  Strings are enclosed with in single or double quotes both works in python.  But do not start with single quote and end with double or vice versa. Both these quotes can be clubbed and used to if a string contains a quote, for example "What's you name?" . 

~The same thing can be done by using escape character also.  \ (back slash) works as a escape character in Python.  For example "What\'s your name?",  In the same way below escape sequences can be used inside strings 
\n -- new line
\t -- tab
\' -- Single Quote
\" -- Double Quote
\\ -- Back Slash 

~Multiple lines also can be specified using triple quotes like below (it is easier if you don;t want to use \n)
"""1st line
   2nd line
   3rd line"""

~If you are working with very big string,  you can type the string in multiple lines with out breaking the string in different line like shown below by using \ at the end of the line.
>>> bigstring = "hello world!!! \
                      Python is a good language" 

~Raw Strings are used if you do not want to process a string for any escape characters.  A raw string holds the characters that you typed.  Raw strings are defined as below, an r plus quote. 
>>> rawstring = r"\n is used for new line characters"

~Unicode Strings are used if the string contains any special character sets like French, Hindi, Arabic, etc. Unicode strings are defined with u plus quotes. 
>>> unistring = u"This is unicode string."

We will talk about the string functions in coming posts.   

3.     Lists

This is a new data type for C/C++ programmers, but It works like a dynamic array. Dynamic means we can add/remove elements whenever required using it's built-in functions and you can add any type of values in the same list.  Find the below examples to see how to use lists.   Individual list elements can be accessed as same as array elements using the index value like ListVar[0].

Ex: >>> ListVar = ['abc', 'pqr', 'xyz', 123]
     >>> print ListVar

4.     Dictionaries

Dictionary type variable contains set of keys and their values.  It is like a phone book, name-number (of course one can have more numbers :) )  Find the below example how dictionaries can be defined. We can retrieve values by using by keys, but not in the other way.  

Ex: >>> DictVar = {"
India":"NewDelhi", "Sweden":"Stockholm", "Denmark":"Copenhagen"}
     >>> print DictVar["
India"]

~ Can not have duplicate keys. 
~ Can add new key-values at any time.
~ keys are case sensitive. 
~ these can be sorted on keys. 

5.     Tuples

Tuple is an immutable list.  This data type is almost same as List,  but it does not allow any addition/deletion/modification of tuple elements once a tuple is defined.  It is like const keyword in C/C++ language.  Tuple is defined also in the same way as List,  but instead of square braces normal braces should be used like below.   Tuples are faster than lists. 

Ex: >>> TupVar = ('xyz','pqr')
     >>> print TupVar

 

Tuesday, April 21, 2009

Install Python and Say Hello World.

Python is an open source programming language and available free to use.  Python's latest version is Python 3.0.  But for I recommend to start with Python 2.6 as many of the 3rd party libraries are still compatible with Python 2.6 

Python can be freely downloaded from http://python.org/download/ site. Click on the "Python 2.6.2 Windows installer", if you are using Windows OS.  Select other options if you are using a different operating systems.  Download the installer and just follow the simple wizard to install.

Say Hello World!!!

As we are always telling,  if Python can be learned in few days and increases productivity of developers, the basic program also should be simple, right?  
Yes. it is simple and one liner.   Follow the below steps to write your first program of Python.

1. Open Program files --> Python 2.6 --> Python IDLE (Python GUI).
2. You will see a prompt like ">>>"
3. type print "Hello World!!!"  and press enter. 
4. You see the output, "Hello World!!!". 

How can you write as a program? 

1. Open Notepad or any text editor
2. add  the following line to the text file 
      print "Hello World!!!"
3. Save the file as C:\hello.py (Python files are generally saved with .py extension)
4. Open Command Prompt using Start --> Run --> Cmd
5. Change directory to Python folder (in my case c:\python30)
6. Now type the following command at the command prompt
      Python C:\hello.py





That's it you will the "Hello World!!!" output as shown in the above image. :)

We will see how python reads the hello.py file, compiles, etc., in the coming posts.

Saturday, April 18, 2009

Python

Now a days I started learning Python for some project requirement.  I was able to acheive the project's requirement, but I like the language and it's features so thought of continuing learning it. So, lets learn Python.

What    
Python is a simple and powerful high level programming language. 
Officially "Python is an easy to learn, powerful programming language. It has efficient high-level data structures and a simple but effective approach to object-oriented programming. Python's elegant syntax and dynamic typing, together with its interpreted nature, make it an ideal language for scripting and rapid application development in many areas on most platforms."

Why:
There are many features why python is preferred, Here are few features of Python: Simple, Easy to Learn, Free and Open Source, High-level Language, Portable, Object Oriented, Extensible (Python libraries can be extended by C/C++ programs), Embeddable(Python can be embed with in C/C++ programs), Extensive Libraries...
 
Who:
Python is developed by Guido van Rossum, who is currently working at Google. 
 
Where
Python is often used as a scripting language for web applications and also used for stand alone application development.

When
Python is created in late 80s and the current version of Python is 3.0.  Python 3.0 is released in Dec' 08 after a long time of 2.0 with lot of testing.

References: wikipedia.com and "Byte of Python"