From: sweeting at neuronet.com.my (sweeting at neuronet.com.my)
Date: Sun, 25 Apr 1999 20:11:31 GMT
Subject: converting perl to python - simple questions.
References: <000001be8f3e$eea9c3c0$d39e2299@tim>
Message-ID: <7fvstg$nqo$1@nnrp1.dejanews.com>
Content-Length: 3248
X-UID: 15                                                 


> > 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.

For years, I've been thinking of "None" in Python as "null" in javascript,
meaning "no value set" and so it was actually quite interesting to see that
Perl has "exists" and "defined"  functions for dictionaries.... I had
translated "exists($dictionary{$token})" into  "dictionary.has_key(token)"
and  hence went overboard when I translated "defined(...)"

Anyway, from testing it does appear that both defined() and exists()
can be simply replaced with dico.has_key(token) in my scripts.


> > 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.

Yes, loads of double-byte character processing ...

> 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).

aaaah - subtle. Thanks.

>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.

Thank you for pointing that out - the perl stuff's been screwing
with my head and making me confused, \s being ok in that language.

> 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):
>         ...
>     ...

Thank you very much - I'd read the excellent howto on python.org and that
described this too. I chose not to compile just for clarity since I'm still
trying to work out if I've translated the code from perl to python
correctly. But I will optimise later...

> 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

after today, i'd be inclined to agree :)

chas

-----------== Posted via Deja News, The Discussion Network ==----------
http://www.dejanews.com/       Search, Read, Discuss, or Start Your Own    




