From: tim_one at email.msn.com (Tim Peters)
Date: Sun, 25 Apr 1999 13:13:26 -0400
Subject: converting perl to python - simple questions.
In-Reply-To: <7fvagp$8lm$1@nnrp1.dejanews.com>
Message-ID: <000001be8f3e$eea9c3c0$d39e2299@tim>
Content-Length: 2025
X-UID: 289                                                  

[sweeting at neuronet.com.my]
> ...
> Anyway, since I know that there are a few ex-perlmongers on the list,
> would somebody be so kind as to confirm whether I've translated
> the following code snippets correctly :
>
> a) Perl's "defined".
>    [perl]
>    if (defined($x{$token})
>
>    [python]
>    if (x.has_key(token) and x[token]!=None) :

If should be enough to do

    if x.has_key(token):

under the probably-correct theory that the Perl is just asking "does hash
'x' have key 'token'?"  "None" is a specific valid value, not at all
"undefined", so checking x[token] against None doesn't make sense unless
you've established your own consistent program-wide convention of using None
to *mean* something like undefined.  Which is dicey.  After e.g. "del
x[token]", a reference to x[token] doesn't yield None, it raises the
KeyError exception.

> b) RE's.
>    [perl]
>    if ($mytext !~ /^\s$/)
>
>    [python]
>    if not (re.match('^\s$'), mytext)

Hmm.  The Perl says "if mytext isn't a single whitespace character", which
is an odd thing to check!  If that's the intent, fine.  Python's "match"
already constrains the search to begin at the start of the string, so the
leading "^" isn't needed (use Python's "search" if don't want that
constraint).  So:

    if not re.match(r"\s$", mytext):

Get in the habit of using r-strings for writing regexps; they'll make your
backslash life much easier.

Another thing to note is that high-use regexps can be compiled, and if
they're always used in the same way (match vs search) you can capture that
choice too.  So this may be more appropriate:

is_single_whitespace = re.compile(r"\s$").match

while whatever:
    ...
    if not is_single_whitespace(mytext):
        ...
    ...

Hoisting the regexp compilation out of the loop can be a substantial win.

> Since I know neither perl nor chinese, it would be nice if somebody
> could help me remove one of the variables in my debugging.

native-speakers-of-both-say-chinese-is-easier-to-read<wink>-ly y'rs  - tim






