Re: can it be shorter?

Discussion in 'Python' started by kj, Jun 6, 2009.

  1. kj

    kj Guest

    In <h0e0oi$1es2$> "tsangpo" <> writes:

    >I want to ensure that the url ends with a '/', now I have to do thisa like
    >below.
    >url = url + '' if url[-1] == '/' else '/'


    >Is there a better way?


    It's a pity that in python regexes are an "extra", as it were.
    Otherwise I'd propose:

    url = re.sub("/?$", "/", url)

    kynn (lowest-of-the-low python noob)
    kj, Jun 6, 2009
    #1
    1. Advertising

  2. On Sat, 06 Jun 2009 15:59:37 +0000, kj wrote:

    > In <h0e0oi$1es2$> "tsangpo"
    > <> writes:
    >
    >>I want to ensure that the url ends with a '/', now I have to do thisa
    >>like below.
    >>url = url + '' if url[-1] == '/' else '/'

    >
    >>Is there a better way?

    >
    > It's a pity that in python regexes are an "extra", as it were. Otherwise
    > I'd propose:
    >
    > url = re.sub("/?$", "/", url)



    Thank goodness regexs are an "extra" in Python, because it discourages
    noobs from pulling out the 80 pound sledgehammer of the regex engine to
    crack the peanut of a test-and-concatenate:


    >>> from timeit import Timer
    >>> min(Timer(

    .... "if not s.endswith('/'): s += '/'", "s = 'abcd/efgh'").repeat())
    0.70030903816223145
    >>> min(Timer(

    .... "sub('/?$', '/', s)", "from re import sub; s = 'abcd/efgh'").repeat())
    7.6922709941864014


    That's more than ten times slower. Really, a regex is massive overkill
    for a task that simple.



    --
    Steven
    Steven D'Aprano, Jun 6, 2009
    #2
    1. Advertising

  3. On Sun, 07 Jun 2009 00:21:45 +0800, tsangpo wrote:

    > "kj" <> å†™å…¥æ¶ˆæ¯ news:h0e3p9$85t

    $...
    >> In <h0e0oi$1es2$> "tsangpo"
    >> <> writes:
    >>
    >>>I want to ensure that the url ends with a '/', now I have to do thisa
    >>>like below.
    >>>url = url + '' if url[-1] == '/' else '/'

    >>
    >>>Is there a better way?

    >>
    >> It's a pity that in python regexes are an "extra", as it were.
    >> Otherwise I'd propose:
    >>
    >> url = re.sub("/?$", "/", url)
    >>
    >> kynn (lowest-of-the-low python noob)

    >
    > look nice, but:
    >
    >>>> re.sub('/?$/', '/', 'aaabbb')

    > 'aaabbb'
    >
    > has no effect. what a pity.



    That's because you're doing it wrong. You've added an extra slash: you're
    asking the regex engine to match a backslash *after* the end of the
    string. Obviously that will never match.

    >>> re.sub('/?$/', '/', 'aaabbb') # extra /

    'aaabbb'
    >>> re.sub('/?$', '/', 'aaabbb') # no extra /

    'aaabbb/'



    But before you get too excited, see my previous post: the regex solution
    is more than ten times slower than test-and-concatenate.



    --
    Steven
    Steven D'Aprano, Jun 6, 2009
    #3
  4. kj

    kj Guest

    In <023a8d04$0$20636$> Steven D'Aprano <> writes:

    >On Sat, 06 Jun 2009 15:59:37 +0000, kj wrote:


    >> In <h0e0oi$1es2$> "tsangpo"
    >> <> writes:
    >>
    >>>I want to ensure that the url ends with a '/', now I have to do thisa
    >>>like below.
    >>>url = url + '' if url[-1] == '/' else '/'

    >>
    >>>Is there a better way?

    >>
    >> It's a pity that in python regexes are an "extra", as it were. Otherwise
    >> I'd propose:
    >>
    >> url = re.sub("/?$", "/", url)



    >Thank goodness regexs are an "extra" in Python, because it discourages
    >noobs from pulling out the 80 pound sledgehammer of the regex engine to
    >crack the peanut of a test-and-concatenate:


    I was just responding to the OP's subject line. Whatever else one
    may say about my proposal, it *is* shorter.

    But thanks for the tip with timeit. That looks like a good module
    to know.

    kynn
    kj, Jun 6, 2009
    #4
  5. kj

    kj Guest

    In <h0e9q8$ni7$> kj <> writes:

    >In <023a8d04$0$20636$> Steven D'Aprano <> writes:


    >>On Sat, 06 Jun 2009 15:59:37 +0000, kj wrote:


    >>> In <h0e0oi$1es2$> "tsangpo"
    >>> <> writes:
    >>>
    >>>>I want to ensure that the url ends with a '/', now I have to do thisa
    >>>>like below.
    >>>>url = url + '' if url[-1] == '/' else '/'
    >>>
    >>>>Is there a better way?
    >>>
    >>> It's a pity that in python regexes are an "extra", as it were. Otherwise
    >>> I'd propose:
    >>>
    >>> url = re.sub("/?$", "/", url)



    >>Thank goodness regexs are an "extra" in Python, because it discourages
    >>noobs from pulling out the 80 pound sledgehammer of the regex engine to
    >>crack the peanut of a test-and-concatenate:


    >I was just responding to the OP's subject line. Whatever else one
    >may say about my proposal, it *is* shorter.


    >But thanks for the tip with timeit. That looks like a good module
    >to know.




    And actually, if speed is the criterion, then one should also avoid endswith:

    >>> from timeit import Timer
    >>> min(Timer("if s[-1] != '/': s += '/'", "s = 'abcd/efgh'").repeat())

    0.18654584884643555
    >>> min(Timer("if not s.endswith('/'): s += '/'", "s = 'abcd/efgh'").repeat())

    0.43395113945007324

    kynn
    kj, Jun 6, 2009
    #5
  6. kj

    MRAB Guest

    kj wrote:
    > In <h0e9q8$ni7$> kj <> writes:
    >
    >> In <023a8d04$0$20636$> Steven D'Aprano <> writes:

    >
    >>> On Sat, 06 Jun 2009 15:59:37 +0000, kj wrote:

    >
    >>>> In <h0e0oi$1es2$> "tsangpo"
    >>>> <> writes:
    >>>>
    >>>>> I want to ensure that the url ends with a '/', now I have to do thisa
    >>>>> like below.
    >>>>> url = url + '' if url[-1] == '/' else '/'
    >>>>> Is there a better way?
    >>>> It's a pity that in python regexes are an "extra", as it were. Otherwise
    >>>> I'd propose:
    >>>>
    >>>> url = re.sub("/?$", "/", url)

    >
    >
    >>> Thank goodness regexs are an "extra" in Python, because it discourages
    >>> noobs from pulling out the 80 pound sledgehammer of the regex engine to
    >>> crack the peanut of a test-and-concatenate:

    >
    >> I was just responding to the OP's subject line. Whatever else one
    >> may say about my proposal, it *is* shorter.

    >
    >> But thanks for the tip with timeit. That looks like a good module
    >> to know.

    >
    >
    >
    > And actually, if speed is the criterion, then one should also avoid endswith:
    >
    >>>> from timeit import Timer
    >>>> min(Timer("if s[-1] != '/': s += '/'", "s = 'abcd/efgh'").repeat())

    > 0.18654584884643555
    >>>> min(Timer("if not s.endswith('/'): s += '/'", "s = 'abcd/efgh'").repeat())

    > 0.43395113945007324
    >

    If there's any chance that the string could be empty (len(s) == 0) then
    use:

    if s[-1 : ] != '/'
    s += '/'
    MRAB, Jun 6, 2009
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. chenyu
    Replies:
    1
    Views:
    269
    Bob Gailer
    Nov 12, 2003
  2. Lowell Kirsh

    how can I make this script shorter?

    Lowell Kirsh, Feb 22, 2005, in forum: Python
    Replies:
    7
    Views:
    273
    Christos TZOTZIOY Georgiou
    Feb 25, 2005
  3. trvlDev
    Replies:
    3
    Views:
    258
    trvlDev
    Apr 6, 2007
  4. Aaron Brady

    Re: can it be shorter?

    Aaron Brady, Jun 7, 2009, in forum: Python
    Replies:
    7
    Views:
    324
    Carlos Valiente
    Jun 11, 2009
  5. Krekna Mektek
    Replies:
    3
    Views:
    90
Loading...

Share This Page