Getting started with Python
Easy to learn, easy to use: Python is worth checking out.
SummaryBy Cameron Laird and Kathryn Soraiz
ell 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 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.
Like such other modern, freely-available scripting technologies as Perl, Scheme, and Tcl, Python is versatile. You can:
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:
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
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:
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
#!/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 == '/': 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) root_of_altered_sources = root_file_reference(argv) 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
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
A longer production version of this utility handles more exceptions and is portable to MS-DOS.
Page 1 of 2, continued...
Advertisement: Support Unix Insider, click here!
|Resources and Related Links|
|Tell Us What You Thought of This Story|
If you have technical problems with this magazine, contact email@example.com
Last modified: Thursday, January 11, 2001