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


Groups > comp.lang.python > #41915 > unrolled thread

I need a neat way to print nothing or a number

Started bycl@isbd.net
First post2013-03-26 15:50 +0000
Last post2013-03-27 08:23 +0000
Articles 10 — 7 participants

Back to article view | Back to comp.lang.python


Contents

  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

#41915 — I need a neat way to print nothing or a number

Fromcl@isbd.net
Date2013-03-26 15:50 +0000
SubjectI need a neat way to print nothing or a number
Message-ID<jms82a-6sl.ln1@chris.zbmc.eu>
What's a neat way to print columns of numbers with blanks where a number
is zero or None?

E.g. I want to output something like:-

    Credit      Debit   Description
    100.00              Initial balance
    123.45              Payment for cabbages
                202.00  Telephone bill


For each line I have either the credit or the debit amount and the other
is 0 or None.  However you can't get number formatting (old or new) to
output a blank for 0 and it barfs on None.

-- 
Chris Green

[toc] | [next] | [standalone]


#41916

FromChris Angelico <rosuav@gmail.com>
Date2013-03-27 03:08 +1100
Message-ID<mailman.3753.1364314099.2939.python-list@python.org>
In reply to#41915
On Wed, Mar 27, 2013 at 2:50 AM,  <cl@isbd.net> wrote:
> What's a neat way to print columns of numbers with blanks where a number
> is zero or None?
>
> E.g. I want to output something like:-
>
>     Credit      Debit   Description
>     100.00              Initial balance
>     123.45              Payment for cabbages
>                 202.00  Telephone bill
>
>
> For each line I have either the credit or the debit amount and the other
> is 0 or None.  However you can't get number formatting (old or new) to
> output a blank for 0 and it barfs on None.

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

[toc] | [prev] | [next] | [standalone]


#41918

FromJohn Gordon <gordon@panix.com>
Date2013-03-26 16:29 +0000
Message-ID<kisicr$e1q$1@reader2.panix.com>
In reply to#41915
In <jms82a-6sl.ln1@chris.zbmc.eu> cl@isbd.net writes:

> What's a neat way to print columns of numbers with blanks where a number
> is zero or None?

print number or '      '

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

[toc] | [prev] | [next] | [standalone]


#41925

FromWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Date2013-03-26 17:06 +0000
Message-ID<mailman.3757.1364317589.2939.python-list@python.org>
In reply to#41915
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 '').

Wolfgang



[toc] | [prev] | [next] | [standalone]


#41926

FromChris Angelico <rosuav@gmail.com>
Date2013-03-27 04:14 +1100
Message-ID<mailman.3758.1364318075.2939.python-list@python.org>
In reply to#41915
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

[toc] | [prev] | [next] | [standalone]


#41927

FromPeter Otten <__peter__@web.de>
Date2013-03-26 18:22 +0100
Message-ID<mailman.3759.1364318537.2939.python-list@python.org>
In reply to#41915
Wolfgang Maier 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 '').

Here's a round-about way:

class Prepare:
    def __init__(self, value):
        self.value = value
    def __format__(self, spec):
        if self.value is None or self.value == 0:
            return format(0.0, spec).replace(".", " ").replace("0", " ")
        elif isinstance(self.value, str):
            return self.value.rjust(len(format(0.0, spec)))
        return format(self.value, spec)

def prepare(row):
    return map(Prepare, row)

data = [
    ("Credit", "Debit", "Description"),
    (100, 0, "Initial balance"),
    (123.45, None, "Payment for cabbages"),
    (0.0, 202.0, "Telephone bill"),
]

for row in data:
    print("{:10.2f} {:10.2f} {}".format(*prepare(row)))


[toc] | [prev] | [next] | [standalone]


#41931

FromEthan Furman <ethan@stoneleaf.us>
Date2013-03-26 10:21 -0700
Message-ID<mailman.3762.1364318846.2939.python-list@python.org>
In reply to#41915
On 03/26/2013 10:06 AM, Wolfgang Maier 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 '').

Use parens then:

("%.2f" % value) if value else ''

--
~Ethan~

[toc] | [prev] | [next] | [standalone]


#41940

FromChris Angelico <rosuav@gmail.com>
Date2013-03-27 05:48 +1100
Message-ID<mailman.3769.1364323722.2939.python-list@python.org>
In reply to#41915
On Wed, Mar 27, 2013 at 4:21 AM, Ethan Furman <ethan@stoneleaf.us> wrote:
> On 03/26/2013 10:06 AM, Wolfgang Maier 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 '').
>
>
> Use parens then:
>
> ("%.2f" % value) if value else ''

According to the operator precedence table, the parens are unnecessary there.

ChrisA

[toc] | [prev] | [next] | [standalone]


#41981

FromTony the Tiger <tony@tiger.invalid>
Date2013-03-27 01:11 -0500
Message-ID<M7idnTEOTvgKEM_MnZ2dnUVZ8rmdnZ2d@giganews.com>
In reply to#41915
On Tue, 26 Mar 2013 15:50:11 +0000, cl wrote:

> output a blank

How about \t ?

 /Grrr
-- 
          ___                  ___
 (\_--_/)  | _ ._    _|_|_  _   |o _  _ ._
 ( 9  9 )  |(_)| |\/  |_| |(/_  ||(_|(/_|
 stripes are forever - as overripe ferrets

[toc] | [prev] | [next] | [standalone]


#41986

FromWolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de>
Date2013-03-27 08:23 +0000
Message-ID<mailman.3795.1364372634.2939.python-list@python.org>
In reply to#41915
Chris Angelico <rosuav <at> gmail.com> writes:

> 
> On Wed, Mar 27, 2013 at 4:06 AM, Wolfgang Maier
> <wolfgang.maier <at> 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
> 

Hi Chris,
yes, I had put parens around your ternary operator expression after the %.
Should have read your code more carefully, but I assumed what you tried to do
was to obtain a *formatted* string in both cases. Your suggestion as it is
really just gives formatting for numbers, but returns an empty string for False
values, so it's just a partial solution to the original problem (basically
converting everything to strings ready for an additional round of formatting).
Anyway, there's a better answer by now, so never mind.
Cheers,
Wolfgang

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web