Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41926

Re: I need a neat way to print nothing or a number

Path csiph.com!usenet.pasdenom.info!news.franciliens.net!fdn.fr!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.003
X-Spam-Evidence '*H*': 0.99; '*S*': 0.00; "'',": 0.07; 'float': 0.07; 'string': 0.09; 'expression:': 0.09; 'false.': 0.09; 'formatting': 0.09; 'literal': 0.09; 'plug': 0.09; 'subject:number': 0.09; '(either': 0.16; 'evaluates': 0.16; 'expression,': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'really?': 0.16; 'sign,': 0.16; 'ternary': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'bit': 0.19; 'subject:need': 0.19; 'fit': 0.20; 'written': 0.21; '>>>': 0.22; 'logical': 0.24; "i've": 0.25; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'idea': 0.28; 'rest': 0.29; 'chris': 0.29; 'am,': 0.29; 'message- id:@mail.gmail.com': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; 'gives': 0.31; 'code': 0.31; 'too.': 0.31; 'apparently': 0.31; 'operators': 0.31; 'writes:': 0.31; 'up.': 0.33; 'worked': 0.33; 'there,': 0.34; "can't": 0.35; 'but': 0.35; 'received:google.com': 0.35; '2.6': 0.36; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'how': 0.40; 'skip:u 10': 0.60; 'easy': 0.60; 'expression': 0.60; 'tell': 0.60; 'full': 0.61; 'first': 0.61; 'mar': 0.68; 'percent': 0.68; 'guaranteed': 0.75; 'subject:nothing': 0.84; 'working,': 0.84; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=B7aRueG73ErNIbpHj6oFyiXPVQxKOSR1k44lfV3QU+Y=; b=dsCWGeEEAwDhGUNk6ctXqY7pFdYhC5shjnhYqLQE/6zVLnv66/wJWNNvTHuO1oaPD/ oUsduuakdNbjGDWf1aDLK1CxvqIzd6jZRNhSFnPoXaO3H20WRDEXnkq0oudpdjtct9jT Zm6zCvhVe6r7/rveCihD8xDrZCnGuTW0gFBPlbLUC+MXcpsJ7Rniw4h/tbGTaEJDhq8f ok8+YC+sTlXz3k7glm2/hBwlo9MBUxCm5/EzSdrkDBzDXPh7QJoJWmKpntWAIB3MewWm Ony3+QBeIiCGyiq7enaoUFEB2ayo/NTm0Hca1tr74PFzvpHtCTgnbkX7uNjpTLqHgg2k HG/A==
MIME-Version 1.0
X-Received by 10.220.109.145 with SMTP id j17mr19948380vcp.34.1364318072903; Tue, 26 Mar 2013 10:14:32 -0700 (PDT)
In-Reply-To <loom.20130326T175824-258@post.gmane.org>
References <jms82a-6sl.ln1@chris.zbmc.eu> <CAPTjJmp2-x0Nsw9JtLscF3QfHpYGb9fgCRAvFMf7EwVdWqt24g@mail.gmail.com> <loom.20130326T175824-258@post.gmane.org>
Date Wed, 27 Mar 2013 04:14:32 +1100
Subject Re: I need a neat way to print nothing or a number
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3758.1364318075.2939.python-list@python.org> (permalink)
Lines 42
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1364318075 news.xs4all.nl 6913 [2001:888:2000:d::a6]:60611
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41926

Show key headers only | View raw


On Wed, Mar 27, 2013 at 4:06 AM, Wolfgang Maier
<wolfgang.maier@biologie.uni-freiburg.de> wrote:
> Chris Angelico <rosuav <at> gmail.com> writes:
>
>>
>> Try printing out this expression:
>>
>> "%.2f"%value if value else ''
>>
>> Without the rest of your code I can't tell you how to plug that in,
>> but a ternary expression is a good fit here.
>>
>> ChrisA
>>
>
> Unfortunately, that's not working, but gives a TypeError: a float is required
> when the first value evaluates to False.
> Apparently it's not that easy to combine number formatting with logical
> operators - the same happens with my idea ('{:.2f}').format(value or '').

Really? Works for me in 3.3:

>>> value=1.2
>>> "%.2f"%value if value else ''
'1.20'
>>> value=0
>>> "%.2f"%value if value else ''
''
>>> value=None
>>> "%.2f"%value if value else ''
''

What's the full context? The way I've written the expression, it's
guaranteed to return a string (either "%.2f"5value or the literal '',
and yes, I'm aware that I was inconsistent with the quotes).

I tried it in 2.6 and it worked there, too. Now, if you parenthesize
the bit after the percent sign, the TypeError comes up. But that
wasn't the intention of the code (and "value if value else
something-else" is just "value or something-else", anyway).

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

I need a neat way to print nothing or a number cl@isbd.net - 2013-03-26 15:50 +0000
  Re: I need a neat way to print nothing or a number Chris Angelico <rosuav@gmail.com> - 2013-03-27 03:08 +1100
  Re: I need a neat way to print nothing or a number John Gordon <gordon@panix.com> - 2013-03-26 16:29 +0000
  Re: I need a neat way to print nothing or a number Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-26 17:06 +0000
  Re: I need a neat way to print nothing or a number Chris Angelico <rosuav@gmail.com> - 2013-03-27 04:14 +1100
  Re: I need a neat way to print nothing or a number Peter Otten <__peter__@web.de> - 2013-03-26 18:22 +0100
  Re: I need a neat way to print nothing or a number Ethan Furman <ethan@stoneleaf.us> - 2013-03-26 10:21 -0700
  Re: I need a neat way to print nothing or a number Chris Angelico <rosuav@gmail.com> - 2013-03-27 05:48 +1100
  Re: I need a neat way to print nothing or a number Tony the Tiger <tony@tiger.invalid> - 2013-03-27 01:11 -0500
  Re: I need a neat way to print nothing or a number Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> - 2013-03-27 08:23 +0000

csiph-web