Translating a Perl script into Python

Sheila King sheila at thinkspot.net
Sat Jan 20 11:21:08 EST 2001


On Sat, 20 Jan 2001 14:01:11 +0100, "Alex Martelli" <aleaxit at yahoo.com> wrote
in comp.lang.python in article <94c2as0nei at news2.newsguy.com>:

:"Sheila King" <sheila at thinkspot.net> wrote in message
:news:vo9i6t04nhurgv2avmgoss4mg86ofcuhtv at 4ax.com...
:    [snip]
:> qmail = ["SENDER", "NEWSENDER", "RECIPIENT", "USER", "HOME", "HOST",
:"LOCAL",
:>     "EXT", "EXT2", "EXT3", "EXT4", "DTLINE", "RPLINE", "UFLINE"]
:
:Excellent, but perhaps closer to the original Perl might be
:
:qmail = 'SENDER NEWSENDER RECIPIENT USER HOME HOST LOCAL EXT EXT2 EXT3 \
:    EXT4 DTLINE RPLINE UFLINE'.split()

Someone else sent me a similar suggestion in e-mail. Where is this documented?
Believe me, I have been going over the docs THOROUGHLY and often! Is this a
Python 2.0 feature? Or does it go back further? (I am using 2.0 right now,
however, my web host only has 1.5.1 installed for everyone's use. I want to
write a script, hopefully, that will run for everyone on the server.)

:> try:
:> PROC = open("proc.test","w")
:> except:
:> sys.stderr << "Couldn't open file for write\n"
:> raise
:
:Perhaps better:
:
:try: PROC = open('proc.test', 'w')
:except: sys.exit(
:    "Couldn't open file for write: %s" %
:    traceback.format_exception_only(
:    *sys.exc_info()[:2]))
:
:or the "couldn't open" message could be lost in
:the traceback; I don't think that message is all
:that informative, so I'd just code
:    PROC = open('proc.test', 'w')
:and let normal exception processing tell me if
:and why it couldn't open the file -- but if it IS
:important to restate this fact, then the traceback
:may then be undesirable.  Debatable, I guess.

So, it is not usual in Python to put some sort of "just in case" piece of code
when opening files? Because I understand that it is very common in Perl.
Perhaps Perl doesn't handle it the same without the "die" statement? I'll tell
you, the exception handling part of this code is the most confusing to me.
There are very few examples in the standard Python docs, and everyone who is
making suggestions, suggests something different.

:_excellent_ code, simple and effective; my comments
:above are all about very marginal issues, because I
:could not find anything substantial to criticize!-)

Thanks,

--
Sheila King
http://www.thinkspot.net/sheila/
http://www.k12groups.org/





More information about the Python-list mailing list