[Tutor] A few comparative perl/python questions
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Sun, 28 Jul 2002 17:41:02 -0700 (PDT)
On Sun, 28 Jul 2002, Kyle Babich wrote:
> I just have a few Perl-to-Python questions:
>
> Does python have anything like Perl's || die, flock(), or seek()
> functions? (What are they?)
[Aside on Perl]
Perl has pretty nice documentation in the 'perldoc' utility: here's an
example of finding out information on the die() function:
###
dyoo@coffeetable:~$ perldoc -f die
die LIST
Outside an "eval", prints the value of LIST to "STDERR" and
exits with the current value of "$!" (errno). If "$!" is "0",
exits with the value of "($? >> 8)" (backtick `command` sta-
tus). If "($? >> 8)" is "0", exits with "255". Inside an
"eval()," the error message is stuffed into "$@" and the "eval"
is terminated with the undefined value. This makes "die" the
way to raise an exception.
###
So if you're working with perl, 'perldoc' is your friend. In Python, we
have the help() function, which does similar things:
###
>>> help(file.seek)
Help on method_descriptor:
seek(...)
seek(offset[, whence]) -> None. Move to new file position.
Argument offset is a byte count. Optional argument whence defaults to
0 (offset from start of file, offset should be >= 0); other values are
1 (move relative to current position, positive or negative), and 2
(move relative to end of file, usually negative, although many platforms
allow seeking beyond the end of a file).
Note that not all file objects are seekable.
###
Simply speaking, die() causes the program to exit immediately with an
error message. This is useful if something "bad" happens. "Bad" things
might include things like:
Trying to open a file that doesn't exist.
Trying to connect to a database with an incorrect password.
Trying to divide by zero.
In Python, an equivalent way to make a program die is to "raise" an
exception, like this:
###
>>> def mydivide(a, b):
... if b == 0:
... raise ZeroDivisionError, "You bad person!"
... return a / b
...
>>>
>>> mydivide(12, 4)
3
>>> mydivide(12, 0)
Traceback (most recent call last):
File "<stdin>", line 1, in ?
File "<stdin>", line 3, in mydivide
ZeroDivisionError: You bad person!
###
But actually, division raises a ZeroDivisionError anyway:
###
>>> 12/0
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ZeroDivisionError: integer division or modulo by zero
###
so the example above is sorta Useless. Still, it shows how we might be
able to make a program try to die quickly if we believe it should. If
you're interested in exceptions, you may find:
http://www.python.org/doc/tut/node10.html
a "deep-end-of-the-pool" introduction to exception handling.
Alan Gauld's "Learning to Program" has a much gentler introduction to
error handling here:
http://www.freenetpages.co.uk/hp/alan.gauld/tuterrors.htm
> If Python doesn't have something like flock(), what happens what
> multiple scripts are trying to write to one file at the same time?
Python does have flock(), but it's in the somewhat unpronouncable 'fcntl'
"file control" module:
http://www.python.org/doc/lib/module-fcntl.html
The only problem is that this appears to be Unix-specific. Hmmm... Ah!
The Python Cookbook includes a module to make this file locking work on
Win32 platforms too:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/65203
Hope this helps!