You are currently browsing the Harish's Weblog blog archives for December, 2008


Why I am using Python….?

 I thought, it would be better to include a post in My Blog,  as many of my friends  asked me this question.  So I am here with some reasons Why I am using Pyhton, as a main Programming Language for the development(both system as well as application side).

One and half year ago, I  Fell in LOVE with Python.  It was “Love in first Program” 🙂  I wrote famous program “Hello World”. It was amazing… I included only a line to do that.. yes “print ‘Hello World’.  The program gave the output on the screen “Hello World”.

Now coming back to the title of the post, Why I am using Python. There are lot of reasons for this question. Python helped in my day-today coding  activities. I solved lot of problems of programming within the short span of time. For strart-up companies like ours, python is God’s gift.

Besides the Python Features,  I would like to share my views on Python.

 

Best for Application/System Programming:

Being an interpreted, interactive, object-oriented programming language that combines remarkable power with very clear syntax. Python can be used to solve the many issues related with System Administrations. Challenges like, Release Management of the Application Software. Taking the Back-up of the Disk, Managing Users, can be programmed in Python without any difficulties. Now coming to Application Side Programming, lot of tools, web frameworks, libraries are available in Python which can used for the Application development. I use a web framework called Django, (Webframework developed using Pyhton)in most of the Web application. The other python web framework are PylonsTurboGears,  Zope,  web2py

A huge collection of Libraries

Unlike the Perl’s CPAN, Python’s standard library is very extensive. The library contains built-in modules (written in C) that provide access to system functionality such as file I/O that would otherwise be inaccessible to Python programmers, as well as modules written in Python that provide standardized solutions for many problems that occur in everyday programming. Some of these modules are explicitly designed to encourage and enhance the portability of Python programs by abstracting away platform-specifics into platform-neutral APIs.In addition to the standard library, there is a growing collection of several thousand components (from individual programs and modules to packages and entire application development frameworks),

Interfaces

Python provides interfaces to many commercial as well as open source databases. It also has interface for COM, DCOM and ODBC. The programs written in Python can be used in C, C++ and Java. Python is extremely portable with UNIX, Windows, Mac OS/2, BeOS and Cray. It is also feature of Automatic Memory Management.

Supports High-Level Data Types

Along with Standard data types, Python supports high level data types such as List Tuple and Dictionaries. The List, Tuple and Dictionaries are highly useful data types in Python and it is very easy to handle. The powerful feature of Python is Regular Expression. It is one of the awesome feature of Python. The other data types are binary data, introspections. It is also possible to define the new ‘built-in’ datatypes.

Supports Meta-programming

According to Wikipedia Metaprogramming is the writing of computer programs that write or manipulate other programs (or themselves) as their data, or that do part of the work at runtime that would otherwise be done at compile time. Not all meta-programming involves generative programming. If programs are modifiable at runtime or if an incremental compilation is available then techniques can be used to perform meta-programming without actually generating source code. It is possible to meta-program in python using Python Decorators. I don’t have much knowledge in Meta-programming .

These are my thoughts on Python Programming Language.

Django – Validator to check the emptyness of file field

The other day My Team had some problem in the validation of the file field, the requirement was simple, they had to check whether the file field is empty or not. The problem sounds somewhat simple. I said just go with a simple ‘if’ and that’s the end of the problem.

But they were unable to do that.. and finally I had to sit with them to solve the problem. It wasn’t that simple as I had realized. We are using a custom validator, which takes two argument- a ‘field_data’ and ‘all_data’. The ‘all_data’ is python dictionary which includes all the fields of the form.

The problem I faced was, non-availability of the file field in the ‘all_data’ dictionary. After fighting with the code, I found a solution. The code looks like this (I am pasting only custom validator code snippet):

Let’s assume that the file field name (model name ) is ‘pdfFile’. We can use python dictionary’s method called ‘has_key ‘. The output of the ‘has_key’ method is boolean. (True or False)


def validatorsName(field_data, all_data):
if all_data.has_key('pdfFile') and all_data['pdfFile'] != "" :
pass
elif not all_data.has_key("pdfFile_file"):
raise validators.ValidationError("The pdf file field is empty")

Let me explain above python code (sorry about the indentation ). The first ‘if’ statement is to check whether the file field already has link or not. If the file field is not empty, ‘all_data’ dictionary includes one key called ‘pdfFile_file’ (‘pdfFile’ key includes the existing link and ‘pdfFile_file’ includes the currently entered link). If the second condition fails it means that the file field is empty.