• Can one output something other than 'nan' for not a number values?

    From Chris Green@cl@isbd.net to comp.lang.python on Fri Feb 16 22:12:33 2024
    From Newsgroup: comp.lang.python

    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'. This would
    then make it much easier to handle outputting values from sensors when
    not all sensors are present.

    So, for example, my battery monitoring program outputs:-

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - 12.34 volts -0.01 Amps

    If the starter battery sensor has failed, or is disconnected, I see:-

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - nan volts nan Amps


    What I would like is for those 'nan' strings to be just a '-' or
    something similar.

    Obviously I can write conditional code to check for float('nan')
    values but is there a neater way with any sort of formatting string or
    other sort of cleverness?
    --
    Chris Green
    ·
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From ram@ram@zedat.fu-berlin.de (Stefan Ram) to comp.lang.python on Sat Feb 17 01:13:48 2024
    From Newsgroup: comp.lang.python

    Chris Green <cl@isbd.net> writes:
    Obviously I can write conditional code to check for float('nan')
    values but is there a neater way with any sort of formatting string or

    This solution does not take the various formatting specifiers into
    account correctly, but shows a broad outline of a possible approach.

    main.py

    import string
    import math

    class MyFormatter( string.Formatter ):
    def format_field( self, value, format_spec ):
    if isinstance( value, float )and value != value: value = '-'
    return super().format_field( value, format_spec )

    fmt = MyFormatter()
    a = math.nan
    b = 2.3

    print( fmt.format( '{a}, {b}', a=a, b=b))

    sys.stdout

    -, 2.3
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Cameron Simpson@cs@cskk.id.au to comp.lang.python on Sat Feb 17 16:05:52 2024
    From Newsgroup: comp.lang.python

    On 16Feb2024 22:12, Chris Green <cl@isbd.net> wrote:
    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'. This would
    then make it much easier to handle outputting values from sensors when
    not all sensors are present.

    So, for example, my battery monitoring program outputs:-

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - 12.34 volts -0.01 Amps

    If the starter battery sensor has failed, or is disconnected, I see:-

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - nan volts nan Amps


    What I would like is for those 'nan' strings to be just a '-' or
    something similar.

    Obviously I can write conditional code to check for float('nan')
    values but is there a neater way with any sort of formatting string or
    other sort of cleverness?

    The simplest thing is probably just a function writing it how you want
    it:

    def float_s(f):
    if isnan(f):
    return "-"
    return str(f)

    and then use eg:

    print(f'value is {float_s(value)}')

    or whatever fits your code.

    Cheers,
    Cameron Simpson <cs@cskk.id.au>

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Grant Edwards@invalid@invalid.invalid to comp.lang.python on Sat Feb 17 23:52:09 2024
    From Newsgroup: comp.lang.python

    On 2024-02-16, Chris Green <cl@isbd.net> wrote:

    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'.

    I tried monkey-patching the __format__ method of float, but it's
    immutable, so that didnt' work. Is float.__format__ what's used by
    f-strings, the % operator, etc.?

    --
    Grant
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From piergiorgio.sartor.this.should.not.be.used@piergiorgio.sartor.this.should.not.be.used@nexgo.REMOVETHIS.de to comp.lang.python on Sun Feb 18 15:03:51 2024
    From Newsgroup: comp.lang.python

    On 16/02/2024 23.12, Chris Green wrote:
    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'. This would
    then make it much easier to handle outputting values from sensors when
    not all sensors are present.

    So, for example, my battery monitoring program outputs:-

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - 12.34 volts -0.01 Amps

    If the starter battery sensor has failed, or is disconnected, I see:-

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - nan volts nan Amps


    What I would like is for those 'nan' strings to be just a '-' or
    something similar.

    Obviously I can write conditional code to check for float('nan')
    values but is there a neater way with any sort of formatting string or
    other sort of cleverness?

    Uhm, I cannot see how to avoid conditional code.

    Somewhere, function, class, method, there should be
    an "if isnan(x)".

    You can hide that, but you cannot avoid, I suspect.

    bye,
    --

    piergiorgio

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Grant Edwards@grant.b.edwards@gmail.com to comp.lang.python on Sat Feb 17 15:47:09 2024
    From Newsgroup: comp.lang.python

    On 2024-02-16, Chris Green via Python-list <python-list@python.org> wrote:

    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'.

    It would probably help if you told us how you're "outputting" them now
    (the Python feaatures/functions used, not the actual output format).

    Are you using f-strings, the % operator, str.format(), or ??

    I would be tempted to try monkey-patching the float class to override
    the __format__ method. I have no idea what side effects that might
    have, or if it's even used by the various formatting mechanisms, so
    you might end up scraping bits off the walls...

    --
    Grant
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Grant Edwards@grant.b.edwards@gmail.com to comp.lang.python on Sat Feb 17 15:53:26 2024
    From Newsgroup: comp.lang.python

    On 2024-02-17, Cameron Simpson via Python-list <python-list@python.org> wrote:
    On 16Feb2024 22:12, Chris Green <cl@isbd.net> wrote:
    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'.
    [...]

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - nan volts nan Amps

    What I would like is for those 'nan' strings to be just a '-' or
    something similar.

    The simplest thing is probably just a function writing it how you want
    it:

    def float_s(f):
    if isnan(f):
    return "-"
    return str(f)

    and then use eg:

    print(f'value is {float_s(value)}')

    or whatever fits your code.

    Except he's obviously using some sort of formatting to control the
    number of columns and decimal places, so 'str(f)' is not going to cut
    it. Is the basic floating point number formatting functionality seen
    when using f-strings or '%' operator part of the float type or is it
    part of the f-string and % operator?

    --
    Grant


    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Grant Edwards@grant.b.edwards@gmail.com to comp.lang.python on Sat Feb 17 17:55:49 2024
    From Newsgroup: comp.lang.python

    On 2024-02-17, Cameron Simpson via Python-list <python-list@python.org> wrote:
    On 16Feb2024 22:12, Chris Green <cl@isbd.net> wrote:
    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'. [...]

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - nan volts nan Amps

    What I would like is for those 'nan' strings to be just a '-' or
    something similar.

    The simplest thing is probably just a function writing it how you want
    it:

    def float_s(f):
    if isnan(f):
    return "-"
    return str(f)

    He's obviouisly using a formatting feature to control columns and
    decimal places, so I doubt that 'str(f)' is going to meet the need.

    I tried monkey-patching the __format__ method of the 'float' type, but
    it's immutable -- so that didn't work.

    Is float.__format__() what's used by f-strings, the % operator, etc.?

    --
    Grant
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Grant Edwards@grant.b.edwards@gmail.com to comp.lang.python on Sat Feb 17 18:10:07 2024
    From Newsgroup: comp.lang.python

    On 2024-02-17, Cameron Simpson via Python-list <python-list@python.org> wrote:
    On 16Feb2024 22:12, Chris Green <cl@isbd.net> wrote:
    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'. [...]

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - 12.34 volts -0.01 Amps

    The simplest thing is probably just a function writing it how you
    want it:

    def float_s(f):
    if isnan(f):
    return "-"
    return str(f)

    Since he's obviously using one of the float formatting mechanisms to
    control the number of columsn and decimal places, I doubt str(f) will
    meet the need.

    I tried monkey-patching the float type's __format__ method, but it's
    immutable.

    Is float.__format__() what's used by f-strings, the '%' operator, etc.?

    --
    Grant
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Grant Edwards@grant.b.edwards@gmail.com to comp.lang.python on Sat Feb 17 17:32:00 2024
    From Newsgroup: comp.lang.python

    [I've been trying all afternoon to post via slrn, but nothing is
    showing up on the list. Forgive me if multiple posts eventually show
    up.]

    On 2024-02-17, Cameron Simpson via Python-list <python-list@python.org> wrote:
    On 16Feb2024 22:12, Chris Green <cl@isbd.net> wrote:
    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'. [...]

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - 12.34 volts -0.01 Amps

    The simplest thing is probably just a function writing it how you
    want it:

    def float_s(f):
    if isnan(f):
    return "-"
    return str(f)

    Since he's obviously using one of the float formatting mechanisms to
    control the number of columsn and decimal places, I doubt str(f) will
    meet the need.

    I tried monkey-patching the float type's __format__ method, but it's
    immutable.

    Is float.__format__() what's used by f-strings, the '%' operator, etc.?

    --
    Grant
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Grant Edwards@grante@panix.com to comp.lang.python on Sat Feb 17 17:39:33 2024
    From Newsgroup: comp.lang.python

    [Posts via slrn and my GMail account aren't showing up, so I guess I'll
    try
    subscribing from a different e-mail address.]

    On 2024-02-17, Cameron Simpson via Python-list <python-list@python.org>
    wrote:
    On 16Feb2024 22:12, Chris Green <cl@isbd.net> wrote:
    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'. [...]

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - 12.34 volts -0.01 Amps

    The simplest thing is probably just a function writing it how you
    want it:

    def float_s(f):
    if isnan(f):
    return "-"
    return str(f)

    Since he's obviously using one of the float formatting mechanisms to
    control the number of columsn and decimal places, I doubt str(f) will
    meet the need.

    I tried monkey-patching the float type's __format__ method, but it's
    immutable.

    Is float.__format__() what's used by f-strings, the '%' operator, etc.?
    --
    Grant
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Chris Angelico@rosuav@gmail.com to comp.lang.python on Mon Feb 19 06:50:21 2024
    From Newsgroup: comp.lang.python

    On Mon, 19 Feb 2024 at 06:47, Grant Edwards via Python-list <python-list@python.org> wrote:
    I would be tempted to try monkey-patching the float class to override
    the __format__ method. I have no idea what side effects that might
    have, or if it's even used by the various formatting mechanisms, so
    you might end up scraping bits off the walls...


    You can try, but you'd have to do it in C - the float type is
    immutable in Python.

    ChrisA
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From dn@PythonList@DancesWithMice.info to comp.lang.python on Mon Feb 19 10:44:41 2024
    From Newsgroup: comp.lang.python

    On 18/02/24 09:53, Grant Edwards via Python-list wrote:
    On 2024-02-17, Cameron Simpson via Python-list <python-list@python.org> wrote:
    On 16Feb2024 22:12, Chris Green <cl@isbd.net> wrote:
    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'.
    [...]

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - nan volts nan Amps

    What I would like is for those 'nan' strings to be just a '-' or
    something similar.

    The simplest thing is probably just a function writing it how you want
    it:

    def float_s(f):
    if isnan(f):
    return "-"
    return str(f)

    and then use eg:

    print(f'value is {float_s(value)}')

    or whatever fits your code.

    Except he's obviously using some sort of formatting to control the
    number of columns and decimal places, so 'str(f)' is not going to cut
    it. Is the basic floating point number formatting functionality seen
    when using f-strings or '%' operator part of the float type or is it
    part of the f-string and % operator?

    It's part of the PSL's string library: "Format Specification
    Mini-Language" https://docs.python.org/3/library/string.html#format-specification-mini-language

    Has the OP stated if we're talking 'Python' or numpy, pandas, ...?
    --
    Regards,
    =dn
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Chris Green@cl@isbd.net to comp.lang.python on Mon Feb 19 12:04:44 2024
    From Newsgroup: comp.lang.python

    dn <PythonList@danceswithmice.info> wrote:
    On 18/02/24 09:53, Grant Edwards via Python-list wrote:
    On 2024-02-17, Cameron Simpson via Python-list <python-list@python.org> wrote:
    On 16Feb2024 22:12, Chris Green <cl@isbd.net> wrote:
    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'.
    [...]

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - nan volts nan Amps

    What I would like is for those 'nan' strings to be just a '-' or
    something similar.

    The simplest thing is probably just a function writing it how you want
    it:

    def float_s(f):
    if isnan(f):
    return "-"
    return str(f)

    and then use eg:

    print(f'value is {float_s(value)}')

    or whatever fits your code.

    Except he's obviously using some sort of formatting to control the
    number of columns and decimal places, so 'str(f)' is not going to cut
    it. Is the basic floating point number formatting functionality seen
    when using f-strings or '%' operator part of the float type or is it
    part of the f-string and % operator?

    It's part of the PSL's string library: "Format Specification
    Mini-Language" https://docs.python.org/3/library/string.html#format-specification-mini-language

    Has the OP stated if we're talking 'Python' or numpy, pandas, ...?

    Just python, on a Raspberry Pi, so currently Python 3.9.2.
    --
    Chris Green
    ·
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Chris Green@cl@isbd.net to comp.lang.python on Mon Feb 19 12:08:00 2024
    From Newsgroup: comp.lang.python

    Grant Edwards <grant.b.edwards@gmail.com> wrote:
    On 2024-02-16, Chris Green via Python-list <python-list@python.org> wrote:

    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'.

    It would probably help if you told us how you're "outputting" them now
    (the Python feaatures/functions used, not the actual output format).

    Are you using f-strings, the % operator, str.format(), or ??

    I would be tempted to try monkey-patching the float class to override
    the __format__ method. I have no idea what side effects that might
    have, or if it's even used by the various formatting mechanisms, so
    you might end up scraping bits off the walls...

    It's using f'{...}' at the moment.
    --
    Chris Green
    ·
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From Grant Edwards@grant.b.edwards@gmail.com to comp.lang.python on Mon Feb 19 11:58:31 2024
    From Newsgroup: comp.lang.python

    On 2024-02-19, Chris Green via Python-list <python-list@python.org> wrote:

    It's using f'{...}' at the moment.

    Here's a demonstration of how to hook custom code into the f-string
    formatting engine. It's brilliantly depraved.

    https://stackoverflow.com/questions/55876683/hook-into-the-builtin-python-f-string-format-machinery

    From the above:

    You can, but only if you write evil code that probably should
    never end up in production software. So let's get started!

    I'm not going to integrate it into your library, but I will show
    you how to hook into the behavior of f-strings. This is roughly
    how it'll work:

    1. Write a function that manipulates the bytecode instructions of
    code objects to replace FORMAT_VALUE instructions with calls
    to a hook function;

    2. Customize the import mechanism to make sure that the bytecode
    of every module and package (except standard library modules
    and site-packages) is modified with that function.

    Final code is here:

    https://github.com/mivdnber/formathack

    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From dn@PythonList@DancesWithMice.info to comp.lang.python on Tue Feb 20 09:11:31 2024
    From Newsgroup: comp.lang.python

    On 20/02/24 05:58, Grant Edwards via Python-list wrote:
    Here's a demonstration of how to hook custom code into the f-string formatting engine. It's brilliantly depraved.

    https://stackoverflow.com/questions/55876683/hook-into-the-builtin-python-f-string-format-machinery

    From the above:

    You can, but only if you write evil code that probably should
    never end up in production software. So let's get started!

    I'm not going to integrate it into your library, but I will show
    you how to hook into the behavior of f-strings. This is roughly
    how it'll work:

    1. Write a function that manipulates the bytecode instructions of
    code objects to replace FORMAT_VALUE instructions with calls
    to a hook function;

    2. Customize the import mechanism to make sure that the bytecode
    of every module and package (except standard library modules
    and site-packages) is modified with that function.

    Final code is here:

    https://github.com/mivdnber/formathack

    Some of this (Expression components inside f-strings) newly available in
    v3.12 (PEP-701) - which can be used in production...
    --
    Regards,
    =dn
    --- Synchronet 3.20a-Linux NewsLink 1.114
  • From dn@PythonList@DancesWithMice.info to comp.lang.python on Tue Feb 20 09:17:10 2024
    From Newsgroup: comp.lang.python

    On 20/02/24 01:04, Chris Green via Python-list wrote:
    dn <PythonList@danceswithmice.info> wrote:
    On 18/02/24 09:53, Grant Edwards via Python-list wrote:
    On 2024-02-17, Cameron Simpson via Python-list <python-list@python.org> wrote:
    On 16Feb2024 22:12, Chris Green <cl@isbd.net> wrote:
    I'm looking for a simple way to make NaN values output as something
    like '-' or even just a space instead of the string 'nan'.
    [...]

    Battery Voltages and Currents
    Leisure Battery - 12.42 volts -0.52 Amps
    Starter Battery - nan volts nan Amps

    What I would like is for those 'nan' strings to be just a '-' or
    something similar.

    The simplest thing is probably just a function writing it how you want >>>> it:

    def float_s(f):
    if isnan(f):
    return "-"
    return str(f)

    and then use eg:

    print(f'value is {float_s(value)}')

    or whatever fits your code.

    Except he's obviously using some sort of formatting to control the
    number of columns and decimal places, so 'str(f)' is not going to cut
    it. Is the basic floating point number formatting functionality seen
    when using f-strings or '%' operator part of the float type or is it
    part of the f-string and % operator?

    It's part of the PSL's string library: "Format Specification
    Mini-Language"
    https://docs.python.org/3/library/string.html#format-specification-mini-language

    Has the OP stated if we're talking 'Python' or numpy, pandas, ...?

    Just python, on a Raspberry Pi, so currently Python 3.9.2.

    Concur with earlier advice (and assuming is only a consideration during output) - use if.

    Alternately, encode appropriately during the data-capture phase.
    --
    Regards,
    =dn
    --- Synchronet 3.20a-Linux NewsLink 1.114