syntax for -c cmd

J

James

Wrong syntax is shown below. What should be the delimiter before else?

python -c 'if 1==1: print "yes"; else print "no"'

James
 
F

Fredrik Lundh

James said:
Wrong syntax is shown below. What should be the delimiter before else?

python -c 'if 1==1: print "yes"; else print "no"'

there is no such delimiter. Python's syntax doesn't allow you to put multiple
clauses on a single line. if your shell supports it, use a "here document", or
embedded newlines. if not, use a script.

</F>
 
P

Paul McGuire

James said:
Wrong syntax is shown below. What should be the delimiter before else?

python -c 'if 1==1: print "yes"; else print "no"'

James

So you can approximate this same logic with a boolean expression:

print (1==1 and "yes" or "no")

This is because Python short-circuits boolean and's and or's, and returns
the last evaluated value. So the expanded logic here is:
evaluate 1==1 -> True
evaluate "yes" -> True - we are done, return "yes"

if the expression were (math.pi==3 and "yes" or "no"), we would get:
evaluate math.pi==3 -> False
(skip evaluation of "yes", first part failed so go straight to or term)
evaluate "no" -> well, doesn't matter if this is True or False, it's the
last thing we have to evaluate, return it

Generally this gets idiomatically expressed as:

(condition and trueConditionValue or falseConditionValue)


This is currently (Python 2.4) the closest Python gets to C's ternary
operator. Note that there are some risks, for example, this rendition
*wont* work.

numberOfApples = 1
print "I have %d apple%s" % ( numberOfApples, (numberOfApples ==1 and "" or
"s"))

This is because "" evaluates boolean-ly to False, so the conditional
conjunction of (numberOfApples==1) and "" is False, so we continue to
evaluate the expression, and wind up printing "I have 1 apples".

The resolution is to invert the test:
numberOfApples = 1
print "I have %d apple%s" % ( numberOfApples, (numberOfApples !=1 and "s" or
""))

(If I have zero of something, I prefer to say "I have 0 somethings" - if you
like to say "I have 0 something" then make the test read (numberOfApples > 1
and "s" or "") )

In the coming 2.5 version of Python, there is a ternary-like expression:

numberOfApples = 1
print "I have %d apple%s" % ( numberOfApples, ("" if numberOfApples ==1 else
"s"))

Python 2.5 is still in preliminary releases though, and will not be
generally available until this coming fall. This will allow you to spell
out if-then-else conditions on the command line. In general scripting,
though, compaction of code into one-liners is not always considered a
virtue. This code:

print "I have %s apple%s" % (numberOfApples==0 and "no" or numberOfApples,
numberOfApples !=1 and "s" or "")

will not win you points with your software maintenance team.

-- Paul
 
E

Edward Elliott

James said:
Wrong syntax is shown below. What should be the delimiter before else?

python -c 'if 1==1: print "yes"; else print "no"'

Now this is interesting. I broke the line up into separate arguments and it
seemed to work fine:

$ python -c 'if 1==1: print "yes"' 'else: print "no"'
yes

But then I tested the else branch and it produces no output:
$ python -c 'if 1==0: print "yes"' 'else: print "no"'
$

If putting the else in a separate arg unbinds it from the if, I would expect
a syntax error. If OTOH naked elses are allowed on the command line for
some odd reason, then this shouldn't happen:

$ python -c 'else: print "no"'
File "<string>", line 1
else: print "no"
^
SyntaxError: invalid syntax

What's with the silent failure in the two-arg version? If the else arg is
syntactically acceptable, why doesn't it behave as expected?
 
D

Dennis Lee Bieber

If putting the else in a separate arg unbinds it from the if, I would expect
a syntax error. If OTOH naked elses are allowed on the command line for
some odd reason, then this shouldn't happen:
More likely, the -C never processed anything after the first quoted
statement.

--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
E

Edward Elliott

Dennis said:
More likely, the -C never processed anything after the first quoted
statement.

Yep you are correct. Odd, I could've sworn I've done that before. Here's
what the man page has to say:

"when called with -c command, it executes the Python statement(s) given as
command. Here command may contain multiple statements separated by
newlines. ... following options are passed as arguments to the command."

So the following works:
$ python -c 'if 0 == 1: print "foo" # arg is unterminated
$$ else: print "bar"' # now we close it
bar
 
F

Fredrik Lundh

Edward said:
Now this is interesting. I broke the line up into separate arguments and it
seemed to work fine:

$ python -c 'if 1==1: print "yes"' 'else: print "no"'
yes

But then I tested the else branch and it produces no output:
$ python -c 'if 1==0: print "yes"' 'else: print "no"'
$

If putting the else in a separate arg unbinds it from the if, I would expect
a syntax error. If OTOH naked elses are allowed on the command line for
some odd reason, then this shouldn't happen:

$ python -c 'else: print "no"'
File "<string>", line 1
else: print "no"
^
SyntaxError: invalid syntax

What's with the silent failure in the two-arg version? If the else arg is
syntactically acceptable, why doesn't it behave as expected?

hint:

$ python -c 'import sys; print sys.argv' 'else: print "no"'

</F>
 
E

Edward Elliott

Fredrik said:
hint:

$ python -c 'import sys; print sys.argv' 'else: print "no"'

</F>

Yeah the man page knows all.

About the only time I use python on the command line is with the timeit
module, which evals all arguments given. Hence the confusion.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,297
Messages
2,571,529
Members
48,240
Latest member
เพิ่มไลค์|LikePro

Latest Threads

Top