Archive

Archive for October, 2009

Django and The MVC pattern in web development

October 18, 2009 1 comment


django_logo1



Django is a web framework built in python, it enables you to build clean, feature-rich application in less amount of time.


Web development has made a lot of progress during the last few years. It began as a tedious task that involved CGI interfacing external programs with the web server. CGI applications used the standard I/O facilities available to the C programming language in order to get user input and produce the required page output. In addition to being difficult to work with, CGI required a seperate copy of the program to be launched for each requests , which quickly overloaded the servers.


Then, new scripting languages were introduced to the web development, and this inspired developers tocreate more efficient technologies. Languages such as Perl and PHP quickly made their way into the world of web development, and as a result common web tasks such as cooking handing, session management and text processing became easier. Although these scripting languages had the libraries, to deal with the day to day web tasks, they lacked unified frameworks, as libraries were fundamentally different in design, usage and conventions. Therefore, the need for cohesive frameworks arose.


Few Years Ago, The MVC (Model View Controller) was introduced to the world of web development for building of web based applications. This software engineering pattern seperates data(Model), user interface(View), and data handling logic(Controller), so that one can be changed without affecting the other. The Benefits are that designers can work on the interface without worrying about data storage and Management. And Developers can think and code on the logic of Data Handling without worrying about the presentation.

Now how does Python come into play ? Although python is used for build wide variety of applications, it is also very suited for web applications. It has a clean and elegant syntax and is supported by large library of modules which covers everything , from multi-threading to the zipping of files. Python laguage’s Object Oriented model is very well suited for the MVC style of web development.


As you can see performance is a very big concern with many of the web projects and it will always be with any of the new coming web projects. And in such situations, Python’s runtime environment does a very good job. as it is known to be fats and stable. Python supports wide range of web servers which include famous names like Apache, nginx, IIS, lighttpd, etc. Python also supports a wide range of Database servers, whch also includes some famous names like MySQL, PostGreSQL, etc. And if you are using Django framework, you don’t need to deal with the databases directly, Django provides a unified layer of access to all database engines.


Advantages of Python :-

Read more…

Apache: mod_python

October 18, 2009 Leave a comment


apache_logo



One of the most interesting ways to use python is the Apache’s mod_python module. The mod_python module actually embeds a fully functional python intrepreter inside the apache web server.
This is most frequently used as powerful means to generate dynamic web pages.


The most common method of generating dynamic web pages is the CGI script. A CGI page is invoked each time a given page is requested. It reads the request, generates a reply and then terminates. This mimics the operation of HTTP, which at its core, works with a single request at a time. The next time a request is received, the CGI script is again invoked from scratch. This design enables the CGI script to be both language and server neutral. indeed all popular web servers and programming languages support them.


However this compatibility comes at a price & performance, Starting up a CGI script is slow. There’s operating system overhead involved while creating a new process. There’s overhead from the python interpreter when initializing and loading the script. CGI script that connect to databases hit especially hard, since they must establish new database connection each time a page is displayed. For these reasons CGI scripts are not suitable for high traffic sites.


mod_python


The mod_python module is one answer to these problems. It actually embeds a full python interpreter inside the apache web server. your scripts are loaded only once per server process and only initialized then. Database connections can be established at the time of initialization and can be kept open throughout the life of web server process. whenever a page needs to be generated, a particular function is called, all data about the request is passed to it. This function has access to the environment created at the initialization time. so, for instance it can reuse the existing database connection.


while this scheme forces the use of apache web server, its advantage often outweigh its disadvantages, Read more…

python script: to change all the files extensions in a Directory

October 15, 2009 Leave a comment


a python script to change file extensions of all the files in a directory:


_____________________________________________________________________________________________


#!/usr/bin/env python
import os
import string

my_directory=’/home/ross’

for object in os.listdir(my_directory):
      if os.path.isfile(os.path.join(my_directory, object)):
              req_file=object
              if ‘.py’ in req_file:
                    ren_file=req_file.replace(‘.mp3′, ‘.mp4′)
                    old_file=os.path.join(my_directory, req_file)
                    new_file=os.path.join(my_directory, ren_file)
                    os.rename(old_file, new_file)


_____________________________________________________________________________________________


(Note:- Please take care of indentaion whenever writing a python script, The script posted above, may not be properly indented)

python script: to count the no. of lines in a file

October 15, 2009 Leave a comment


a simple script to count the no. of lines in a file:-


____________________________________________________________________________________________


#!/usr/bin/env python

read_file=open(‘/testfile.txt’, ‘rb’)

count_file=len(read_file.readlines())

print count_file


____________________________________________________________________________________________


(Note:- Please take care of indentaion whenever writing a python script, The script posted above, may not be properly indented)

Categories: Python Tags: , ,

python script: copy contents and replace a word

October 15, 2009 Leave a comment


a script that will copy the contents from one file to another and also replace the requested word with the other one.


_________________________________________________________________________________________________


#!/usr/bin/env python

the_word=raw_input(‘enter the word to replace –> ‘)
replace_word=raw_input(‘enter the word to replace with –> ‘)

to_file=open(‘/testfile.txt2′, ‘a’)

try:
      read_file=open(‘/testfile.txt’)

      for searches in read_file:
              searches=searches.replace(the_word, replace_word)

      to_file.write(searches)
finally:
      to_file.close()


_________________________________________________________________________________________________

(Note:- Please take care of indentaion whenever writing a python script, The script posted above, may not be properly indented)

Categories: Python Tags: , , ,

python script: to copy from one file to another

October 15, 2009 2 comments


A simple python script to copy a content of one file to another.


________________________________________________________________________________________________________


#!/usr/bin/env python

to_readfile=open(‘/testfile.txt’, ‘r’)
try:
      reading_file=to_readfile.read()

      writefile=open(‘/testfile2.txt’, ‘w’)
      try:
           writefile.write(reading_file)
      finally:
           writefile.close()

finally:
      to_readfile.close()




________________________________________________________________________________________________________


(Note:- Please take care of indentaion whenever writing a python script, The script posted above, may not be properly indented)

Tips for Object Oriented Design

October 1, 2009 Leave a comment

bp2_cover_small


While going through Magnus‘s book “Begining python from novice to professional“, I was on chapter 11, and being a system admin, mostly sys ads don’t care whether the scripts written are object oriented or procedural, we njust need the script to be working, as most the time the requirements are urgent.

The chapter 11in Magnus’s Book is well explained, its excellent, specially the polymorphism part, its beautifully explained. At the end of the chapter Magnus has written “Some thoughts on Object Oriented Design”, which i think are really practical and should be followed while writing the code. I am posting some of the tips here. Hope you find it useful.


    Tips for Objet Orientation :-

• Gather what belongs together. If a function manipulates a global variable, the two of them might be better off in a class, as an attribute and a method.

• Don’t let objects become too intimate. Methods should mainly be concerned with the attributes of their own instance. Let other instances manage their own state.

Read more…

Follow

Get every new post delivered to your Inbox.