Re: can it be shorter?

Discussion in 'Python' started by Aaron Brady, Jun 7, 2009.

  1. Aaron Brady

    Aaron Brady Guest

    On Jun 6, 8:07 am, "tsangpo" <> wrote:
    > 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?


    url+= { '/': '' }.get( url[ -1 ], '/' )

    Shorter is always better.
     
    Aaron Brady, Jun 7, 2009
    #1
    1. Advertising

  2. Aaron Brady

    Paul Rubin Guest

    Aaron Brady <> writes:
    > url+= { '/': '' }.get( url[ -1 ], '/' )
    >
    > Shorter is always better.


    url = url.rstrip('/') + '/'
     
    Paul Rubin, Jun 8, 2009
    #2
    1. Advertising

  3. Aaron Brady

    Aaron Brady Guest

    On Jun 7, 6:13 pm, Paul Rubin <http://> wrote:
    > Aaron Brady <> writes:
    > > url+= { '/': '' }.get( url[ -1 ], '/' )

    >
    > > Shorter is always better.

    >
    > url = url.rstrip('/') + '/'


    I was joking. Sheesh.
     
    Aaron Brady, Jun 8, 2009
    #3
  4. Aaron Brady wrote:
    > Shorter is always better.
    >
    > url+= { '/': '' }.get( url[ -1 ], '/' )


    Why bother with spaces or 3 letter-wide token, check this :eek:) :
    x+={'/':''}.get(x[-1],'/')

    Apart from joking, the following proposed solution is by **far** the one
    I prefer

    > if not url.endswith('/'):
    > url += '/'


    Maybe not the shorter, but the most concise and clear to me.

    Jean-Michel
     
    Jean-Michel Pichavant, Jun 8, 2009
    #4
  5. Aaron Brady

    MRAB Guest

    Jean-Michel Pichavant wrote:
    > Aaron Brady wrote:
    >> Shorter is always better.
    >> url+= { '/': '' }.get( url[ -1 ], '/' )

    >
    > Why bother with spaces or 3 letter-wide token, check this :eek:) :
    > x+={'/':''}.get(x[-1],'/')
    >

    Even shorter:

    x+='/'*(x[-1]!='/')

    > Apart from joking, the following proposed solution is by **far** the one
    > I prefer
    >
    >> if not url.endswith('/'):
    >> url += '/'

    >
    > Maybe not the shorter, but the most concise and clear to me.
    >

    Definitely.
     
    MRAB, Jun 8, 2009
    #5
  6. Aaron Brady

    Aaron Brady Guest

    On Jun 8, 9:50 am, Jean-Michel Pichavant <>
    wrote:
    > Aaron Brady wrote:
    > > Shorter is always better.

    >
    > > url+= { '/': '' }.get( url[ -1 ], '/' )

    >
    > Why bother with spaces or 3 letter-wide token, check this  :eek:) :
    > x+={'/':''}.get(x[-1],'/')
    >
    > Apart from joking, the following proposed solution is by **far** the one
    > I prefer
    >
    > > if not url.endswith('/'):
    > >    url += '/'

    >
    > Maybe not the shorter, but the most concise and clear to me.


    Why won't Python permit:

    url.endswith( '/' ) or url.append( '/' )

    ? Should it? Do we find it just as concise and clear? Does it
    outweigh the priority of the immutability of strings? It works on
    lists, for example. A sole mutating operation could create a highly
    and finely tempered compromise with immutability. Would it be
    'append'?

    I like Scott's and MRAB's idea for slicing, not indexing, the last
    character.

    The most literal translation of the original natural language is:

    >>> #ensure that the url ends with a '/'
    >>> ensure( url, string.endswith, '/' )


    (Is it not?) But the parameters aren't sufficient to define 'ensure'
    generally, and it won't be able to mutate 'url' regardless.
     
    Aaron Brady, Jun 8, 2009
    #6
  7. On Mon, 08 Jun 2009 12:57:58 -0700, Aaron Brady wrote:

    > Why won't Python permit:
    >
    > url.endswith( '/' ) or url.append( '/' )
    >
    > ?


    Because:

    (1) Strings are immutable, so that won't work.

    (2) Even if it did, you're programming by side-effect, which is bad style
    often leading to bugs, and so should be avoided.


    > Should it?


    Heavens no! It's bad enough that similar expressions are allowed for
    lists. Just because they're allowed, doesn't mean we should use them!


    > Do we find it just as concise and clear?


    No. It *looks* like a boolean expression which is produced then thrown
    away uselessly. If not for append() on lists having a side-effect, I'd
    call it an expensive no-op.


    > Does it
    > outweigh the priority of the immutability of strings?


    Certainly not. Special cases aren't special enough to break the rules.
    Strings have nice consistent behaviour. You're suggesting making their
    behaviour inconsistent.



    > It works on
    > lists, for example. A sole mutating operation could create a highly and
    > finely tempered compromise with immutability.


    You're not thinking it through. You can't say "strings are immutable,
    except for append, which mutates them". If you allow *one* mutable
    operation, then the type is mutable, full stop.


    > Would it be 'append'?
    >
    > I like Scott's and MRAB's idea for slicing, not indexing, the last
    > character.
    >
    > The most literal translation of the original natural language is:
    >
    >>>> #ensure that the url ends with a '/'
    >>>> ensure( url, string.endswith, '/' )

    >
    > (Is it not?) But the parameters aren't sufficient to define 'ensure'
    > generally, and it won't be able to mutate 'url' regardless.


    This suggestion appears to be a pie-in-the-sky impractical suggestion. It
    requires a function ensure() with close to human intelligence to "do what
    I mean". As such, I can't take it seriously.


    --
    Steven
     
    Steven D'Aprano, Jun 9, 2009
    #7
  8. Paul Rubin <http> writes:
    > url = url.rstrip('/') + '/'


    That's what I use: It has the (nice) side effect of ending the URL with a
    *single* slash.

    C
     
    Carlos Valiente, Jun 11, 2009
    #8
    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:
    283
    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:
    285
    Christos TZOTZIOY Georgiou
    Feb 25, 2005
  3. trvlDev
    Replies:
    3
    Views:
    267
    trvlDev
    Apr 6, 2007
  4. kj

    Re: can it be shorter?

    kj, Jun 6, 2009, in forum: Python
    Replies:
    5
    Views:
    324
  5. Krekna Mektek
    Replies:
    3
    Views:
    104
Loading...

Share This Page