tsangpo | 6 Jun 2009 17:07
Picon

can it be shorter?

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? 

--

-- 
http://mail.python.org/mailman/listinfo/python-list

Aaron Brady | 8 Jun 2009 00:45
Picon

Re: can it be shorter?

On Jun 6, 8:07 am, "tsangpo" <tsangpo.newsgr... <at> 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.
--

-- 
http://mail.python.org/mailman/listinfo/python-list

Jean-Michel Pichavant | 8 Jun 2009 16:50

Re: can it be shorter?

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

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

--

-- 
http://mail.python.org/mailman/listinfo/python-list

MRAB | 8 Jun 2009 17:50

Re: can it be shorter?

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  :o) :
> 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.
--

-- 
http://mail.python.org/mailman/listinfo/python-list

Paul Rubin | 8 Jun 2009 01:13

Re: can it be shorter?

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

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

-- 
http://mail.python.org/mailman/listinfo/python-list

Carlos Valiente | 11 Jun 2009 10:34

Re: can it be shorter?

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

--

-- 
http://mail.python.org/mailman/listinfo/python-list

Aaron Brady | 8 Jun 2009 12:45
Picon

Re: can it be shorter?

On Jun 7, 6:13 pm, Paul Rubin <http://phr... <at> NOSPAM.invalid> wrote:
> Aaron Brady <castiro... <at> gmail.com> writes:
> > url+= { '/': '' }.get( url[ -1 ], '/' )
>
> > Shorter is always better.
>
> url = url.rstrip('/') + '/'

I was joking.  Sheesh.
--

-- 
http://mail.python.org/mailman/listinfo/python-list

Roland Mueller | 8 Jun 2009 14:25

Re: can it be shorter?



2009/6/8 Aaron Brady <castironpi <at> gmail.com>
On Jun 7, 6:13 pm, Paul Rubin <http://phr... <at> NOSPAM.invalid> wrote:
> Aaron Brady <castiro... <at> gmail.com> writes:
> > url+= { '/': '' }.get( url[ -1 ], '/' )
>
> > Shorter is always better.
>
> url = url.rstrip('/') + '/'

I was joking.  Sheesh.

my two cents: a solution based on regex: the pattern replaces all slashes at the end of the string with a single one. The * usage matches also the empty group of slashes (example s2)

>>> print s1
aaaaa/
>>> print s2
bbbb
>>> re.sub('[/]*$','/', s1)
'aaaaa/'
>>> re.sub('[/]*$','/', s2)
'bbbb/'

-Roland
--

-- 
http://mail.python.org/mailman/listinfo/python-list
Brian | 8 Jun 2009 00:59
Picon
Favicon

Re: can it be shorter?

Since extra slashes at the end of a URL are ignored, that means I win!

url+='/'

On Sun, Jun 7, 2009 at 4:45 PM, Aaron Brady <castironpi <at> gmail.com> wrote:
On Jun 6, 8:07 am, "tsangpo" <tsangpo.newsgr... <at> 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.

--

-- 
http://mail.python.org/mailman/listinfo/python-list
kj | 6 Jun 2009 17:59

Re: can it be shorter?

In <h0e0oi$1es2$1 <at> adenine.netfront.net> "tsangpo" <tsangpo.newsgroup <at> gmail.com> 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)
--

-- 
http://mail.python.org/mailman/listinfo/python-list

Steven D'Aprano | 6 Jun 2009 18:34
Picon

Re: can it be shorter?

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

> In <h0e0oi$1es2$1 <at> adenine.netfront.net> "tsangpo"
> <tsangpo.newsgroup <at> gmail.com> 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
--

-- 
http://mail.python.org/mailman/listinfo/python-list

kj | 6 Jun 2009 19:42

Re: can it be shorter?

In <023a8d04$0$20636$c3e8da3 <at> news.astraweb.com> Steven D'Aprano
<steve <at> REMOVE-THIS-cybersource.com.au> writes:

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

>> In <h0e0oi$1es2$1 <at> adenine.netfront.net> "tsangpo"
>> <tsangpo.newsgroup <at> gmail.com> 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

--

-- 
http://mail.python.org/mailman/listinfo/python-list

kj | 6 Jun 2009 23:56

Re: can it be shorter?

In <h0e9q8$ni7$1 <at> reader1.panix.com> kj <no.email <at> please.post> writes:

>In <023a8d04$0$20636$c3e8da3 <at> news.astraweb.com> Steven D'Aprano
<steve <at> REMOVE-THIS-cybersource.com.au> writes:

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

>>> In <h0e0oi$1es2$1 <at> adenine.netfront.net> "tsangpo"
>>> <tsangpo.newsgroup <at> gmail.com> 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

--

-- 
http://mail.python.org/mailman/listinfo/python-list

Scott David Daniels | 7 Jun 2009 00:28
Picon
Favicon

Re: can it be shorter?

kj wrote:
> ... 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

_but_, try this with s = '', and you are in trouble.
So, try:

     if s[-1:] != '/':
         s += '/'

To be a trifle more reliable. But, for more normal semantics,
you might prefer either:
     if s[-1:] != '/':
         s = (s or '.') + '/'
or:
     if s and s[-1] != '/':
         s += '/'

--Scott David Daniels
Scott.Daniels <at> Acm.Org
--

-- 
http://mail.python.org/mailman/listinfo/python-list

MRAB | 7 Jun 2009 00:24

Re: can it be shorter?

kj wrote:
> In <h0e9q8$ni7$1 <at> reader1.panix.com> kj <no.email <at> please.post> writes:
> 
>> In <023a8d04$0$20636$c3e8da3 <at> news.astraweb.com> Steven D'Aprano
<steve <at> REMOVE-THIS-cybersource.com.au> writes:
> 
>>> On Sat, 06 Jun 2009 15:59:37 +0000, kj wrote:
> 
>>>> In <h0e0oi$1es2$1 <at> adenine.netfront.net> "tsangpo"
>>>> <tsangpo.newsgroup <at> gmail.com> 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 += '/'
--

-- 
http://mail.python.org/mailman/listinfo/python-list

tsangpo | 6 Jun 2009 18:21
Picon

Re: can it be shorter?


"kj" <no.email <at> please.post> 写入消息 news:h0e3p9$85t$1 <at> reader1.panix.com...
> In <h0e0oi$1es2$1 <at> adenine.netfront.net> "tsangpo" 
> <tsangpo.newsgroup <at> gmail.com> 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. 

--

-- 
http://mail.python.org/mailman/listinfo/python-list
Nick Craig-Wood | 6 Jun 2009 20:29
Gravatar

Re: can it be shorter?

tsangpo <tsangpo.newsgroup <at> gmail.com> wrote:
> 
>  "kj" <no.email <at> please.post> 写入消息 news:h0e3p9$85t$1 <at> reader1.panix.com...
> > In <h0e0oi$1es2$1 <at> adenine.netfront.net> "tsangpo" 
> > <tsangpo.newsgroup <at> gmail.com> 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 is because you typoed what kynn wrote.

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

That solution is very perl-ish I'd say, IMHO

    if not url.endswith("/"):
        url += "/"

is much more pythonic and immediately readable.  In fact even someone
who doesn't know python could understand what it does, unlike the
regexp solution which requires a little bit of thought.

-- 
Nick Craig-Wood <nick <at> craig-wood.com> -- http://www.craig-wood.com/nick
--

-- 
http://mail.python.org/mailman/listinfo/python-list
Steven D'Aprano | 6 Jun 2009 18:38
Picon

Re: can it be shorter?

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

> "kj" <no.email <at> please.post> 写入消息 news:h0e3p9$85t
$1 <at> reader1.panix.com...
>> In <h0e0oi$1es2$1 <at> adenine.netfront.net> "tsangpo"
>> <tsangpo.newsgroup <at> gmail.com> 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
--

-- 
http://mail.python.org/mailman/listinfo/python-list
Steven D'Aprano | 6 Jun 2009 17:16
Picon

Re: can it be shorter?

On Sat, 06 Jun 2009 23:07:58 +0800, 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?

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

-- 
Steven
--

-- 
http://mail.python.org/mailman/listinfo/python-list


Gmane