new string method in 2.5 (partition)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Salerno

    new string method in 2.5 (partition)

    Forgive my excitement, especially if you are already aware of this, but
    this seems like the kind of feature that is easily overlooked (yet could
    be very useful):


    Both 8-bit and Unicode strings have new partition(sep) and
    rpartition(sep) methods that simplify a common use case.
    The find(S) method is often used to get an index which is then used to
    slice the string and obtain the pieces that are before and after the
    separator. partition(sep) condenses this pattern into a single method
    call that returns a 3-tuple containing the substring before the
    separator, the separator itself, and the substring after the separator.
    If the separator isn't found, the first element of the tuple is the
    entire string and the other two elements are empty. rpartition(sep) also
    returns a 3-tuple but starts searching from the end of the string; the
    "r" stands for 'reverse'.

    Some examples:

    >>('http://www.python.org' ).partition('://')
    ('http', '://', 'www.python.org ')
    >>('file:/usr/share/doc/index.html').pa rtition('://')
    ('file:/usr/share/doc/index.html', '', '')
    >>(u'Subject: a quick question').part ition(':')
    (u'Subject', u':', u' a quick question')
    >>'www.python.o rg'.rpartition( '.')
    ('www.python', '.', 'org')
    >>'www.python.o rg'.rpartition( ':')
    ('', '', 'www.python.org ')

    (Implemented by Fredrik Lundh following a suggestion by Raymond Hettinger.)
  • metaperl

    #2
    Re: new string method in 2.5 (partition)

    sweet thanks for the heads up.

    John Salerno wrote:
    Forgive my excitement, especially if you are already aware of this, but
    this seems like the kind of feature that is easily overlooked (yet could
    be very useful):
    >
    >
    Both 8-bit and Unicode strings have new partition(sep) and
    rpartition(sep) methods that simplify a common use case.
    The find(S) method is often used to get an index which is then used to
    slice the string and obtain the pieces that are before and after the
    separator. partition(sep) condenses this pattern into a single method
    call that returns a 3-tuple containing the substring before the
    separator, the separator itself, and the substring after the separator.
    If the separator isn't found, the first element of the tuple is the
    entire string and the other two elements are empty. rpartition(sep) also
    returns a 3-tuple but starts searching from the end of the string; the
    "r" stands for 'reverse'.
    >
    Some examples:
    >
    >
    >>('http://www.python.org' ).partition('://')
    ('http', '://', 'www.python.org ')
    >>('file:/usr/share/doc/index.html').pa rtition('://')
    ('file:/usr/share/doc/index.html', '', '')
    >>(u'Subject: a quick question').part ition(':')
    (u'Subject', u':', u' a quick question')
    >>'www.python.o rg'.rpartition( '.')
    ('www.python', '.', 'org')
    >>'www.python.o rg'.rpartition( ':')
    ('', '', 'www.python.org ')
    >
    (Implemented by Fredrik Lundh following a suggestion by Raymond Hettinger.)

    Comment

    • richard.charts@gmail.com

      #3
      Re: new string method in 2.5 (partition)

      I'm confused.
      What's the difference between this and string.split?

      John Salerno wrote:
      Forgive my excitement, especially if you are already aware of this, but
      this seems like the kind of feature that is easily overlooked (yet could
      be very useful):
      >
      >
      Both 8-bit and Unicode strings have new partition(sep) and
      rpartition(sep) methods that simplify a common use case.
      The find(S) method is often used to get an index which is then used to
      slice the string and obtain the pieces that are before and after the
      separator. partition(sep) condenses this pattern into a single method
      call that returns a 3-tuple containing the substring before the
      separator, the separator itself, and the substring after the separator.
      If the separator isn't found, the first element of the tuple is the
      entire string and the other two elements are empty. rpartition(sep) also
      returns a 3-tuple but starts searching from the end of the string; the
      "r" stands for 'reverse'.
      >
      Some examples:
      >
      >
      >>('http://www.python.org' ).partition('://')
      ('http', '://', 'www.python.org ')
      >>('file:/usr/share/doc/index.html').pa rtition('://')
      ('file:/usr/share/doc/index.html', '', '')
      >>(u'Subject: a quick question').part ition(':')
      (u'Subject', u':', u' a quick question')
      >>'www.python.o rg'.rpartition( '.')
      ('www.python', '.', 'org')
      >>'www.python.o rg'.rpartition( ':')
      ('', '', 'www.python.org ')
      >
      (Implemented by Fredrik Lundh following a suggestion by Raymond Hettinger.)

      Comment

      • Lawrence Oluyede

        #4
        Re: new string method in 2.5 (partition)

        richard.charts@ gmail.com <richard.charts @gmail.comwrote :
        What's the difference between this and string.split?
        >>('http://www.python.org' ).partition('://')
        ('http', '://', 'www.python.org ')
        >>('http://www.python.org' ).split('://')
        ['http', 'www.python.org ']



        --
        Lawrence - http://www.oluyede.org/blog
        "Nothing is more dangerous than an idea
        if it's the only one you have" - E. A. Chartier

        Comment

        • John Salerno

          #5
          Re: new string method in 2.5 (partition)

          richard.charts@ gmail.com wrote:
          I'm confused.
          What's the difference between this and string.split?
          >>s = 'hello, world'
          >>s.split(',' )
          ['hello', ' world']
          >>s.partition(' ,')
          ('hello', ',', ' world')


          split returns a list of the substrings on either side of the specified
          argument.

          partition returns a tuple of the substring on the left of the argument,
          the argument itself, and the substring on the right. rpartition reads
          from right to left.


          But you raise a good point. Notice this:
          >>s = 'hello, world, how are you'
          >>s.split(',' )
          ['hello', ' world', ' how are you']
          >>s.partition(' ,')
          ('hello', ',', ' world, how are you')

          split will return all substrings. partition (and rpartition) only return
          the substrings before and after the first occurrence of the argument.

          Comment

          • Bruno Desthuilliers

            #6
            Re: new string method in 2.5 (partition)

            John Salerno a écrit :
            Forgive my excitement, especially if you are already aware of this, but
            this seems like the kind of feature that is easily overlooked (yet could
            be very useful):
            >
            >
            Both 8-bit and Unicode strings have new partition(sep) and
            rpartition(sep) methods that simplify a common use case.
            The find(S) method is often used to get an index which is then used to
            slice the string and obtain the pieces that are before and after the
            separator.
            Err... is it me being dumb, or is it a perfect use case for str.split ?
            partition(sep) condenses this pattern into a single method
            call that returns a 3-tuple containing the substring before the
            separator, the separator itself, and the substring after the separator.
            If the separator isn't found, the first element of the tuple is the
            entire string and the other two elements are empty. rpartition(sep) also
            returns a 3-tuple but starts searching from the end of the string; the
            "r" stands for 'reverse'.
            >
            Some examples:
            >
            >
            >>('http://www.python.org' ).partition('://')
            ('http', '://', 'www.python.org ')
            >>('file:/usr/share/doc/index.html').pa rtition('://')
            ('file:/usr/share/doc/index.html', '', '')
            >>(u'Subject: a quick question').part ition(':')
            (u'Subject', u':', u' a quick question')
            >>'www.python.o rg'.rpartition( '.')
            ('www.python', '.', 'org')
            >>'www.python.o rg'.rpartition( ':')
            ('', '', 'www.python.org ')
            I must definitively be dumb, but so far I fail to see how it's better
            than split and rsplit:
            >>'http://www.python.org' .split('://')
            ['http', 'www.python.org ']
            >>'file:/usr/share/doc/index.html'.spl it('://')
            ['file:/usr/share/doc/index.html']
            >>u'Subject: a quick question'.split (': ')
            [u'Subject', u'a quick question']
            >>u'Subject: a quick question'.rspli t(': ')
            [u'Subject', u'a quick question']
            >>'www.python.o rg'.rsplit('.', 1)
            ['www.python', 'org']
            >>>
            There are IMVHO much exciting new features in 2.5 (enhanced generators,
            try/except/finally, ternary operator, with: statement etc...)

            Comment

            • Tim Chase

              #7
              Re: new string method in 2.5 (partition)

              >partition(se p) condenses this pattern into a single method
              >call that returns a 3-tuple containing the substring before
              >the separator, the separator itself, and the substring after
              >the separator. If the separator isn't found, the first
              >element of the tuple is the entire string and the other two
              >elements are empty. rpartition(sep) also returns a 3-tuple
              >but starts searching from the end of the string; the "r"
              >stands for 'reverse'.
              >
              I'm confused. What's the difference between this and
              string.split?
              (please don't top-post...I've inverted and trimmed for the sake
              of readability)

              I too am a bit confused but I can see uses for it, and there
              could be good underlying reason to do as much. Split doesn't
              return the separator. It's also guarnteed to return a 3-tuple. E.g.
              >>s1 = 'one'
              >>s2 = 'one|two'
              >>len(s1.split( '|', 1)
              1
              >>len(s2.split( '|', 1))
              2

              which could make a difference when doing tuple-assignment:
              >>v1, v2 = s2.split('|', 1)
              >># works fine
              >>v1, v2 = s1.split('|', 1)
              [traceback]

              whereas one could consistently do something like
              >>v1, _, v2 = s1.partition('| ')
              without fear of a traceback to deal with.

              Just a few thoughts...

              -tkc



              Comment

              • Tim Chase

                #8
                Re: new string method in 2.5 (partition)

                But you raise a good point. Notice this:
                >
                >>s = 'hello, world, how are you'
                >
                >>s.split(',' )
                ['hello', ' world', ' how are you']
                >
                >>s.partition(' ,')
                ('hello', ',', ' world, how are you')
                >
                split will return all substrings. partition (and rpartition) only return
                the substrings before and after the first occurrence of the argument.
                The split()/rsplit() functions do take an optional argument for
                the maximum number of splits to make, just FYI...
                >>help("".split )
                Help on built-in function split:

                split(...)
                S.split([sep [,maxsplit]]) -list of strings

                Return a list of the words in the string S, using sep as the
                delimiter string. If maxsplit is given, at most maxsplit
                splits are done. If sep is not specified or is None, any
                whitespace string is a separator.



                (as I use this on a regular basis when mashing up various text
                files in a data conversion process)

                -tkc




                Comment

                • John Salerno

                  #9
                  Re: new string method in 2.5 (partition)

                  Bruno Desthuilliers wrote:
                  Err... is it me being dumb, or is it a perfect use case for str.split ?
                  Hmm, I suppose you could get nearly the same functionality as using
                  split(':', 1), but with partition you also get the separator returned as
                  well.
                  There are IMVHO much exciting new features in 2.5 (enhanced generators,
                  try/except/finally, ternary operator, with: statement etc...)
                  I definitely agree, but I figure everyone knows about those already.
                  There are also the startswith() and endswith() string methods that are
                  new and seem neat as well.

                  Comment

                  • Thomas Heller

                    #10
                    Re: new string method in 2.5 (partition)

                    John Salerno schrieb:
                    Bruno Desthuilliers wrote:
                    >
                    >Err... is it me being dumb, or is it a perfect use case for str.split ?
                    >
                    Hmm, I suppose you could get nearly the same functionality as using
                    split(':', 1), but with partition you also get the separator returned as
                    well.
                    Well, x.split(":", 1) returns a list of one or two elements, depending on x,
                    while x.partition(":" ) always returns a three-tuple.

                    Thomas

                    Comment

                    • George Sakkis

                      #11
                      Re: new string method in 2.5 (partition)

                      Bruno Desthuilliers wrote:
                      I must definitively be dumb, but so far I fail to see how it's better
                      than split and rsplit:
                      I fail to see it too. What's the point of returning the separator since
                      the caller passes it anyway* ?

                      George

                      * unless the separator can be a regex, but I don't think so.

                      Comment

                      • Larry Bates

                        #12
                        Re: new string method in 2.5 (partition)

                        John Salerno wrote:
                        Bruno Desthuilliers wrote:
                        >
                        >Err... is it me being dumb, or is it a perfect use case for str.split ?
                        >
                        Hmm, I suppose you could get nearly the same functionality as using
                        split(':', 1), but with partition you also get the separator returned as
                        well.
                        >
                        >There are IMVHO much exciting new features in 2.5 (enhanced
                        >generators, try/except/finally, ternary operator, with: statement etc...)
                        >
                        I definitely agree, but I figure everyone knows about those already.
                        There are also the startswith() and endswith() string methods that are
                        new and seem neat as well.
                        FYI- .startswith() and .endswith() string methods aren't new in 2.5.
                        They have been around since at least 2.3.

                        Larry Bates

                        Comment

                        • John Salerno

                          #13
                          Re: new string method in 2.5 (partition)

                          Larry Bates wrote:
                          John Salerno wrote:
                          >Bruno Desthuilliers wrote:
                          >>
                          >>Err... is it me being dumb, or is it a perfect use case for str.split ?
                          >Hmm, I suppose you could get nearly the same functionality as using
                          >split(':', 1), but with partition you also get the separator returned as
                          >well.
                          >>
                          >>There are IMVHO much exciting new features in 2.5 (enhanced
                          >>generators, try/except/finally, ternary operator, with: statement etc...)
                          >I definitely agree, but I figure everyone knows about those already.
                          >There are also the startswith() and endswith() string methods that are
                          >new and seem neat as well.
                          >
                          FYI- .startswith() and .endswith() string methods aren't new in 2.5.
                          They have been around since at least 2.3.
                          >
                          Larry Bates
                          Oops, just a slight change in their functionality:

                          The startswith() and endswith() methods of string types now accept
                          tuples of strings to check for.

                          def is_image_file (filename):
                          return filename.endswi th(('.gif', '.jpg', '.tiff'))

                          (Implemented by Georg Brandl following a suggestion by Tom Lynn.)

                          Comment

                          • Jack Diederich

                            #14
                            Re: new string method in 2.5 (partition)

                            On Tue, Sep 19, 2006 at 07:23:50PM +0000, John Salerno wrote:
                            Bruno Desthuilliers wrote:
                            >
                            Err... is it me being dumb, or is it a perfect use case for str.split ?
                            >
                            Hmm, I suppose you could get nearly the same functionality as using
                            split(':', 1), but with partition you also get the separator returned as
                            well.
                            >
                            There are IMVHO much exciting new features in 2.5 (enhanced generators,
                            try/except/finally, ternary operator, with: statement etc...)
                            >
                            I definitely agree, but I figure everyone knows about those already.
                            There are also the startswith() and endswith() string methods that are
                            new and seem neat as well.
                            Partition is much, much nicer than index() or find() for many
                            (but not all) applications.

                            diff for cgi.py parsing "var=X"
                            - i = p.find('=')
                            - if i >= 0:
                            - name = p[:i]
                            - value = p[i+1:]
                            + (name, sep_found, value) = p.partition('=' )

                            Notice that preserving the seperator makes for a nice boolean
                            to test if the partition was successful. Partition raises an
                            error if you pass an empty seperator.

                            parition also has the very desirable feature of returning the orignal
                            string when the seperator isn't found

                            ex/

                            script = 'foo.cgi?a=7'
                            script, sep, params = script.partitio n('?')

                            "script" will be "foo.cgi" even if there are no params. With
                            find or index you have to slice the string by hand and with split
                            you would do something like.

                            try:
                            script, params = script.split('? ')
                            except ValueError: pass

                            or

                            parts = script.split('? ', 1)
                            script = parts[0]
                            params = ''.join(parts[1:])


                            Grep your source for index, find, and split and try rewriting
                            the code with partition. Not every instance will turn out cleaner
                            but many will.

                            Long-live-partition-ly,

                            -Jack

                            Comment

                            • Duncan Booth

                              #15
                              Re: new string method in 2.5 (partition)

                              "George Sakkis" <george.sakkis@ gmail.comwrote:
                              Bruno Desthuilliers wrote:
                              >
                              >I must definitively be dumb, but so far I fail to see how it's better
                              >than split and rsplit:
                              >
                              I fail to see it too. What's the point of returning the separator since
                              the caller passes it anyway* ?
                              >
                              The separator is only returned if it was found otherwise you get back an
                              empty string. Concatenating the elements of the tuple that is returned
                              always gives you the original string.

                              It is quite similar to using split(sep,1), but reduces the amount of
                              special case handling for cases where the separator isn't found.

                              Comment

                              Working...