The ITworld.com Network  
Network Search ¦ Sites ¦ Services ¦ ITcareers


Advertisement: Support Unix Insider, click here!

Home Home E-mail this article E-mail this article Printer-friendly version Printer-friendly version Tell us what you think Tell us what you think
Spacer
Spacer SEARCH
put phrases in quotes
Spacer
Spacer
Spacer
Spacer
Spacer NAVIGATE Spacer
Spacer Subscribe -- It's Free Spacer
Spacer Topical Index Spacer
Spacer Back Issues Spacer
Spacer UnixWHERE Spacer
Spacer Events Calendar Spacer
Spacer Community Discussions
Spacer
Spacer TECHNICAL FAQs Spacer
Spacer Solaris Security Spacer
Spacer Secure Programming Spacer
Spacer Performance Q&A Spacer
Spacer SE Toolkit Spacer
Spacer
Spacer ADVERTISEMENT Spacer
Spacer
Spacer

Spacer
Spacer
Spacer
Spacer UNIX INSIDER PARTNERS
Spacer
Spacer Spacer
Spacer ITcareers.com Spacer
Spacer Expert Advice Spacer
Spacer RFP Center Spacer
Spacer Training Center Spacer
Spacer Research Spacer
Spacer Vendor Content Spacer
Spacer
Spacer ABOUT UNIX INSIDER Spacer
Spacer Advertising Info Spacer
Spacer Unix Insider FAQ Spacer
Spacer Unix Insider Editors Spacer
Spacer Masthead Spacer
Spacer Editorial Calendar Spacer
Spacer Writers' Guidelines Spacer
Spacer Privacy Policy Spacer
Spacer Link to Us! Spacer
Spacer Copyright Spacer
Spacer BACK TO TOP Spacer
Spacer

Getting started with Python

Easy to learn, easy to use: Python is worth checking out.

Summary
Do you work in C, C++, or Java on large projects? We'll show how Python will give you results quicker and more reliably. Are you already scripting in a language other than Python? You'll want to see how Python's careful engineering offers an easy-to-use, interactive, clean, object-oriented, extensible, scalable language. Whatever programming you're doing now, Python can improve it. (3,000 words)
By Cameron Laird and Kathryn Soraiz


Tell me more about Python.

That was the most common request from readers of our recent story on scripting languages. And the readers are right. Python deserves more attention.

Python, like BASIC, originated in an educational environment. Guido van Rossum created Python over the 1989/1990 winter holidays while working as a researcher in Amsterdam. In the 1980s he had co-authored the second implementation of a scripting language, ABC, to teach computer concepts. ABC never gained the popularity he felt it merited. Van Rossum reworked it in early 1990, joining ABC's elegance and ease of learning with deliberate openness to "foreign" components. He also removed a few idiosyncrasies that distracted experienced users and boosted performance. The result was Python (named, by the way, after the British comedy troup, Monty Python).

A language without hang-ups
Python is a remarkable synthesis. It handles large object-oriented programming tasks with greater ease than systems languages such as C, C++, or Java. And, because it is a scripting language, learning Python is a snap. Unlike C or C++, written to make life easier for the computer, Python was designed to be easy on the programmer.

Python is a pure object-oriented systems development language. This is unusual for scripting languages because they generally include object orientation only as an afterthought, and are at their best in small projects. Python, in contrast, scales well. Its scoping rules and clean design make it readable even amongst multiprogrammer teams, and its package and module facilities effectively structure big jobs. Python is a general-purpose language that is also a good prototyping language.


Python incorporates the best of scripting and systems languages

Like such other modern, freely-available scripting technologies as Perl, Scheme, and Tcl, Python is versatile. You can:

  • Launch Python as an interpreter for small, one-time-only needs.
  • Build large applications from Python scripts.
  • "Freeze" a Python application into a standalone executable that's easy and secure to distribute.
  • Embed the interpreter into one of your own applications.
  • "Wrap up" existing (difficult) applications inside an easy-to-use Python shell.
  • Run Python from within a Java virtual machine (JVM).
  • Build Web applications with applets written for the Grail browser, or server-side common gateway interface (CGI) or embedded scripting.

And you can do all this from most operating systems (including Amiga, MacOS, MSDOS, OS/2, Unix, VMS, and Windows).

What's the best part of Python? It excels at team projects. We think of Python first for any job that involves several people. There are several reasons:

  • it is easy for others to read
  • its pieces can be worked on independently
  • it's easy to assemble into a final form

In short, even though it's a scripting language, Python competes with conventional programming languages and deserves serious consideration by anyone who needs the strength of a conventional compiled language for a workgroup-sized job.

Starting off right
Let's get Python working so you can see these benefits for yourself. With a copy of the interpreter on your desktop you have the opportunity to practice Python as you read along.

Setting up Python is easier than the typical installation of a Java or C compiler. Even with a slow connection, it takes less than an hour to retrieve an installable form of a fully functioning interpreter from the binary repository (see Resources below) and install your own copy of Python.

When you launch the interpreter (either by clicking on an executable icon or invoking "python" at the command line) you'll see a ">>>" prompt. In a pinch, you could almost teach yourself Python by playing within the interpreter. The syntax and keywords will be familiar: print "Hello, World." and a = 7; print 9 * a do just what you expect. The distribution includes an extensive and well-written reference manual. However, if you're new to Python, you're best off with one of the fine books or online tutorials that you'll find in listed at the Python home page.

For system administration, we need a utility to compare a source tree to an archive from which it had been modified. This is the traditional province in Unix environments of shell or Perl programming. Python's object orientation and metaprogramming (programs that manipulate programs) don't matter in this simple case. However, what compare_archive.py does demonstrate is Python's readability. This was one of our first efforts in Python:

#!/usr/contrib/newest/bin/python

from sys import *
from os import *
from posix import *

# Define a function which "roots" a path reference, if
#     it's not already rooted.
def root_file_reference(file_reference):
        if file_reference[0] == '/':
                return file_reference
        else:
                return getcwd() + "/" + file_reference

def usage():
        print "Usage:  compare_archive TARFILE DIRECTORY"
        exit(1)

if len(argv) != 3:
        usage()

source_archive = root_file_reference(argv[1])
root_of_altered_sources = root_file_reference(argv[2])

tempdir = "/tmp/comparison." + str(getpid())

report = ""
count = 0
mkdir(tempdir)
chdir(tempdir)
system("tar xf " + source_archive)
for file in popen("find . -type f -print").readlines():
                # Chop off the terminal newline.
        file = file[:-1]
        difference = popen("diff %s %s/./%s" %
                        (file, root_of_altered_sources, file)).read()
        if difference != "":
                report = report + "-------------------------------\n"
                report = report + "Modifications in " + file
                report = report + ":\n" + difference
                count = count + 1

system("rm -rf " + tempdir)

print "A total of %s sources have been changed." % count
print report

If you want to try compare_archive.py for yourself, simply install Python on a Unix host, save the source into a file named compare_archive.py, set its execution bit, and launch it from the command line. You'll see a result that looks something like:


A total of 2 sources have been changed.
-------------------------------
Modifications in ./src/first:
1a2
> This is a new line.
-------------------------------
Modifications in ./lib/ar/mine.c:
2a3,5
> #include 
> #include 
> #include 

A longer production version of this utility handles more exceptions and is portable to MS-DOS.

Page 1 of 2, continued...


Page 1.
Page 2. Python vs. other scripting languages
Printer-friendly (all-in-one) version

Home | Mail this Story | Printer-Friendly Version | Comment on this Story | Resources and Related Links

Advertisement: Support Unix Insider, click here!

Resources and Related Links
   

Tell Us What You Thought of This Story
 
-Very worth reading
-Worth reading
-Not worth reading
-Too long
-Just right
-Too short
-Too technical
-Just right
-Not technical enough
 
 
 
    
 

(c) Copyright 1998 Web Publishing Inc., and IDG Communication company

If you have technical problems with this magazine, contact webmaster@unixinsider.com

URL: http://www.unixinsider.com/swol-02-1998/swol-02-python.html
Last modified: Thursday, January 11, 2001