Question about raw string and regex

J

jlowery

I'm looking through the tools/scripts folder from the python install,
trying to get reacquanted with the language. Got a question on the
following classfix.py snippet:

# This expression doesn't catch *all* class definition headers,
# but it's pretty darn close.
classexpr = '^\([ \t]*class +[a-zA-Z0-9_]+\) *( *) *\(\(=.*\)?\):'
classprog = regex.compile(classexpr)

Since the classexpr isn't a raw string (not r prefix), doesn't the \t
get converted to a tab character before it gets to the regex compiler?
 
F

Felipe Almeida Lessa

Em Qui, 2006-03-23 às 17:11 -0800, (e-mail address removed) escreveu:
Since the classexpr isn't a raw string (not r prefix), doesn't the \t
get converted to a tab character before it gets to the regex compiler?
print '^\([ \t]*class +[a-zA-Z0-9_]+\) *( *) *\(\(=.*\)?\):'
^\([ ]*class +[a-zA-Z0-9_]+\) *( *) *\(\(=.*\)?\):

Yes... maybe that's the intention?
 
J

jlowery

I doubt it, although it might work anyway.

Here's another from the same program:

(a0, b0), (a1, b1), (a2, b2) = classprog.regs[:3]

Nothing in the Python lib reference on the regs attribute for regex
objects.
 
J

John Machin

I doubt it, although it might work anyway.

You could dispel all doubt in about 15 seconds flat were you to actually
try it out.
__main__:1: DeprecationWarning: the regex module is deprecated; please
use the re module
>>> regex.match(r"[ \t]", "\t") -1
>>> regex.match("[ \t]", "\t") 1
>>> import re
>>> re.match("[ \t]", "\t")
>>> re.match(r"[ \t]", "\t")
Here's another from the same program:

(a0, b0), (a1, b1), (a2, b2) = classprog.regs[:3]

Nothing in the Python lib reference on the regs attribute for regex
objects.

Dunno where you're looking, but my Python 1.5.2 has the regex docs,
which include a big fat note to the effect of the above
DeprecationWarning, plus documentation on the regs attribute.
 
J

jlowery

Gotta love the attitude of people on .lang newsgroups....


The unraw '\t' might work, but all the example in the tutorial use raw
strings, so why not be consistent in the example scripts?

1.5.2? Aren't we at 2.4.2 now? So the regs documentation was pulled,
yet the source code shipped with the installer still uses it? How are
people suppose to make heads or tails of a language when it ships with
deprecated, undocumented code?

sheesh.
 
J

John Machin

Gotta love the attitude of people on .lang newsgroups....


The unraw '\t' might work, but all the example in the tutorial use raw
strings, so why not be consistent in the example scripts?

classfix.py is not an *example* script. It is (was!) a *tool* script.
1.5.2? Aren't we at 2.4.2 now? So the regs documentation was pulled,
yet the source code shipped with the installer still uses it?

You are missing the point. The whole *regex* module was deprecated
around 1.something. It was replaced by the *re* module. At some stage
the whole regex docs were "pulled". 1.5.2 is the latest version that I
have on my machine that still provided the docs for regex. The regs
attribute is documented there. When you said "Nothing in the Python lib
reference on the regs attribute for regex objects" you must have been
referring to the *re* documentation.
How are
people suppose to make heads or tails of a language when it ships with
deprecated, undocumented code?

Here is line 3 of classfix.py:
# This script is obsolete -- it is kept for historical purposes only.

Sheesh vobiscum :)
 
F

Fredrik Lundh

1.5.2? Aren't we at 2.4.2 now? So the regs documentation was pulled,
yet the source code shipped with the installer still uses it? How are
people suppose to make heads or tails of a language when it ships with
deprecated, undocumented code?

$ more classfix.py

# This script is obsolete -- it is kept for historical purposes only.
#
# Fix Python source files to use the new class definition syntax, i.e.,
# the syntax used in Python versions before 0.9.8:
# class C() = base(), base(), ...: ...
# is changed to the current syntax:
# class C(base, base, ...): ...

....

(if you don't understand that comment, it says that this is a conversion
script for converting code written for Python 0.9.8 to the current syntax,
which is left in there for historical purposes only)
Gotta love the attitude of people on .lang newsgroups....

I don't think the attitude problem is where you think it is...

</F>
 
J

jlowery

classfix.py is not an *example* script. It is (was!) a *tool* script.

I see. 2.4.2 includes a tool for modifying 0.9.8 python classes to 1.1
somthing format using a now-defunct regex module. Oh, okay. Very
useful, I can see why it would still be included as part of the
distribution.

I was using 'regex' as the general term for regular expressions, not
the module. I hadn't realized the (old, decrepit) script was using
something other than 're' (r standing for 'regular', and e for
'expressions' [I'm guessing]).

Pylint (with everything switched on) doesn't flag anything as
deprecated, or at least I didn't notice that it did. Maybe it was
because the method for deprecation hadn't been devised yet? -- I don't
know, as I'm not a Python archivist.

I don't think I missed line 3, but just interpeted it to apply to the
purpose of the script, not the script itself.

And, BTW, people will look at the source that's distributed with Python
for purposes of writing their own code, be it in tools, libs, or
whatever. They (should) serve as examples of current practice, IM(H)O.
Old cruft should go in a source archive to dwell in the shadows
forevermore.
 
J

jlowery

I understood the comment, just misinterpreted the meaning of the first
statement.

And speaking of my attitude, it's just as bad as anyone else's here.
Double check the membership of the comp.lang.python set...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top