473,320 Members | 1,817 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

syntax for -c cmd

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

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

James

May 10 '06 #1
7 2475
James wrote:
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>

May 10 '06 #2
On 5/10/06, Fredrik Lundh <fr*****@pythonware.com> wrote:
James wrote:
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.


Or there's always this; cool in a "The Dark Side is strong" kind of a way:

<http://www.unixuser.org/~euske/pyone/>

--
Cheers,
Simon B,
si***@brunningonline.net,
http://www.brunningonline.net/simon/blog/
May 10 '06 #3
"James" <hs******@yahoo.com> wrote in message
news:11**********************@i40g2000cwc.googlegr oups.com...
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
May 10 '06 #4
James wrote:
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?

May 11 '06 #5
Dennis Lee Bieber wrote:
On Thu, 11 May 2006 20:12:55 GMT, Edward Elliott <no****@127.0.0.1>
declaimed the following in comp.lang.python:
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.


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

--
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net
May 12 '06 #6
Edward Elliott wrote:
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>

May 12 '06 #7
Fredrik Lundh wrote:
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.

--
Edward Elliott
UC Berkeley School of Law (Boalt Hall)
complangpython at eddeye dot net
May 12 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

699
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro...
22
by: Tuang | last post by:
I'm checking out Python as a candidate for replacing Perl as my "Swiss Army knife" tool. The longer I can remember the syntax for performing a task, the more likely I am to use it on the spot if...
14
by: Sandy Norton | last post by:
If we are going to be stuck with @decorators for 2.4, then how about using blocks and indentation to elminate repetition and increase readability: Example 1 --------- class Klass: def...
16
by: George Sakkis | last post by:
I'm sure there must have been a past thread about this topic but I don't know how to find it: How about extending the "for <X> in" syntax so that X can include default arguments ? This would be very...
23
by: Carter Smith | last post by:
http://www.icarusindie.com/Literature/ebooks/ Rather than advocating wasting money on expensive books for beginners, here's my collection of ebooks that have been made freely available on-line...
19
by: Nicolas Fleury | last post by:
Hi everyone, I would to know what do you think of this PEP. Any comment welcomed (even about English mistakes). PEP: XXX Title: Specialization Syntax Version: $Revision: 1.10 $...
4
by: Jeremy Yallop | last post by:
Looking over some code I came across a line like this if isalnum((unsigned char)c) { which was accepted by the compiler without complaint. Should the compiler have issued a diagnostic in this...
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
4
by: Bob hotmail.com> | last post by:
Everyone I have been spending weeks looking on the web for a good tutorial on how to use regular expressions and other methods to satisfy my craving for learning how to do FAST c-style syntax...
3
by: Manuel | last post by:
I'm trying to compile glut 3.7.6 (dowbloaded from official site)using devc++. So I've imported the glut32.dsp into devc++, included manually some headers, and start to compile. It return a very...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.