Question about raw string and regex

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jlowery@blarg.net

    #1

    Question about raw string and regex

    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(c lassexpr)

    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?

  • Felipe Almeida Lessa

    #2
    Re: Question about raw string and regex

    Em Qui, 2006-03-23 às 17:11 -0800, jlowery@blarg.n et escreveu:[color=blue]
    > 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?[/color]
    [color=blue][color=green][color=darkred]
    >>> print '^\([ \t]*class +[a-zA-Z0-9_]+\) *( *) *\(\(=.*\)?\):'[/color][/color][/color]
    ^\([ ]*class +[a-zA-Z0-9_]+\) *( *) *\(\(=.*\)?\):

    Yes... maybe that's the intention?

    --
    Felipe.

    Comment

    • jlowery@blarg.net

      #3
      Re: Question about raw string and regex

      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.

      Comment

      • John Machin

        #4
        Re: Question about raw string and regex

        On 24/03/2006 12:36 PM, jlowery@blarg.n et wrote:[color=blue]
        > I doubt it, although it might work anyway.[/color]

        You could dispel all doubt in about 15 seconds flat were you to actually
        try it out.
        [color=blue][color=green][color=darkred]
        >>> import regex[/color][/color][/color]
        __main__:1: DeprecationWarn ing: the regex module is deprecated; please
        use the re module[color=blue][color=green][color=darkred]
        >>> regex.match(r"[ \t]", "\t")[/color][/color][/color]
        -1[color=blue][color=green][color=darkred]
        >>> regex.match("[ \t]", "\t")[/color][/color][/color]
        1[color=blue][color=green][color=darkred]
        >>> import re
        >>> re.match("[ \t]", "\t")[/color][/color][/color]
        <_sre.SRE_Mat ch object at 0x00AE9058>[color=blue][color=green][color=darkred]
        >>> re.match(r"[ \t]", "\t")[/color][/color][/color]
        <_sre.SRE_Mat ch object at 0x00AE9918>[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]
        [color=blue]
        >
        > 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.
        >[/color]

        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
        DeprecationWarn ing, plus documentation on the regs attribute.

        Comment

        • jlowery@blarg.net

          #5
          Re: Question about raw string and regex

          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.

          Comment

          • John Machin

            #6
            Re: Question about raw string and regex

            On 24/03/2006 2:57 PM, jlowery@blarg.n et wrote:[color=blue]
            > 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?[/color]

            classfix.py is not an *example* script. It is (was!) a *tool* script.
            [color=blue]
            >
            > 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?[/color]

            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.
            [color=blue]
            > How are
            > people suppose to make heads or tails of a language when it ships with
            > deprecated, undocumented code?[/color]

            Here is line 3 of classfix.py:
            # This script is obsolete -- it is kept for historical purposes only.
            [color=blue]
            > sheesh.[/color]

            Sheesh vobiscum :-)

            Comment

            • Fredrik Lundh

              #7
              Re: Question about raw string and regex

              jlowery@blarg.n et wrote:
              [color=blue]
              > 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?[/color]

              $ 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)
              [color=blue]
              > Gotta love the attitude of people on .lang newsgroups....[/color]

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

              </F>



              Comment

              • jlowery@blarg.net

                #8
                Re: Question about raw string and regex

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

                Comment

                • jlowery@blarg.net

                  #9
                  Re: Question about raw string and regex

                  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.pytho n set...

                  Comment

                  Working...