From: Bruno Desthuilliers on
mk a �crit :
(snip)
> So in this context this is fine. But I wanted to make the class more
> robust. Perhaps I should do smth like this before setting self.cmd?
>
> assert isinstance(cmd, basestring) or cmd is None, "cmd should be string
> or None"
>
> and then:
>
> if cmd:
> self.cmd = cmd.replace..

And what if cmd happens to be the empty string ?-)

ok, me --->[]
From: Gerald Britton on
[snip]

> Last August [1], I offered this alternative:
>
>  self.cmd = (cmd.replace(r'${ADDR}',ip)
>              if isinstance(cmd, str) else
>              cmd)
>
> But it didn't get much love in this forum!

I'd probably go for that one as well though I might consider removing
the outer parentheses.

--
Gerald Britton
From: Bruno Desthuilliers on
mk a �crit :
>
> if isinstance(cmd, str):
> self.cmd = cmd.replace(r'${ADDR}',ip)
> else:
> self.cmd = cmd

What could "cmd" be except a string ? From other posts here, I guess
it's either a string or None ? If yes, then I'd go for this:

if cmd:
cmd = cmd.replace(r'${ADDR}',ip)

self.cmd = cmd



From: John Posner on
On 2/5/2010 11:06 AM, Gerald Britton wrote:
> [snip]
>
>> Last August [1], I offered this alternative:
>>
>> self.cmd = (cmd.replace(r'${ADDR}',ip)
>> if isinstance(cmd, str) else
>> cmd)
>>
>> But it didn't get much love in this forum!
>
> I'd probably go for that one as well though I might consider removing
> the outer parentheses.

Agreed ... except that you *need* the outer parentheses if the statement
occupies multiple source lines.

-John


From: mk on
Bruno Desthuilliers wrote:
>> if cmd:
>> self.cmd = cmd.replace..
>
> And what if cmd happens to be the empty string ?-)
>
> ok, me --->[]

Right, I didn't think much when I wrote that. Anyway, that's back to
square one.

I will probably go for this anyway:

assert isinstance(cmd, basestring) or cmd is None, 'cmd has to
be string or None'
if cmd:
cmd = cmd.replace(r'${ADDR}',ip)
self.cmd = cmd