Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Re: can it be shorter?

Reply
Thread Tools

Re: can it be shorter?

 
 
Aaron Brady
Guest
Posts: n/a
 
      06-07-2009
On Jun 6, 8:07*am, "tsangpo" <tsangpo.newsgr...@gmail.com> 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.
 
Reply With Quote
 
 
 
 
Paul Rubin
Guest
Posts: n/a
 
      06-07-2009
Aaron Brady <> writes:
> url+= { '/': '' }.get( url[ -1 ], '/' )
>
> Shorter is always better.


url = url.rstrip('/') + '/'
 
Reply With Quote
 
 
 
 
Aaron Brady
Guest
Posts: n/a
 
      06-08-2009
On Jun 7, 6:13*pm, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> Aaron Brady <castiro...@gmail.com> writes:
> > url+= { '/': '' }.get( url[ -1 ], '/' )

>
> > Shorter is always better.

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


I was joking. Sheesh.
 
Reply With Quote
 
Jean-Michel Pichavant
Guest
Posts: n/a
 
      06-08-2009
Aaron Brady wrote:
> Shorter is always better.
>
> url+= { '/': '' }.get( url[ -1 ], '/' )


Why bother with spaces or 3 letter-wide token, check this ) :
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



 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      06-08-2009
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 ) :
> 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.
 
Reply With Quote
 
Aaron Brady
Guest
Posts: n/a
 
      06-08-2009
On Jun 8, 9:50*am, Jean-Michel Pichavant <jeanmic...@sequans.com>
wrote:
> Aaron Brady wrote:
> > Shorter is always better.

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

>
> Why bother with spaces or 3 letter-wide token, check this *) :
> 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.
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      06-08-2009
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
 
Reply With Quote
 
Carlos Valiente
Guest
Posts: n/a
 
      06-11-2009
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




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can Groovy be used in an applet and/or can it generate the Java bytecodes that then can be used in an applet? Casey Hawthorne Java 1 03-18-2009 12:56 AM
Word Docs Won't Open, Can't Be E-Mailed, Can't Be Deleted, Can't Be Copied, Etc. Martin Computer Support 16 02-24-2009 07:35 PM
Wireless can get internet but can't see network -- can when wired 02befree Computer Support 0 12-24-2007 09:10 PM
SOLVED - can't open file in windows media player / WMP. But can in VLC - video LAN .. Now can in WMP jameshanley39@yahoo.co.uk Computer Information 2 09-19-2007 02:53 AM
Windows can see mapped drives, but applications can't? =?Utf-8?B?RGFuaWVsIEVpY2hvcm4=?= Wireless Networking 3 11-18-2004 11:03 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57