• Re: No solution for "--verbose" (on stdout) output in Pythonds standard

    From Chris Angelico@21:1/5 to [email protected] on Thu Jan 5 00:59:37 2023
    On Thu, 5 Jan 2023 at 00:54, <[email protected]> wrote:

    Hello,

    first I have to say that in my current and fresh humble opinion the
    often seen "--verbose" switch in command line applications should
    affect only the messages given to the users. This means messages on
    "stdout". That is what this question is about.

    The logging module is not an option because it works on stderr and is
    not intended to offer messages for the user but just for the system and
    its admins via logs (log-files, syslog, stderr redirection, ...).

    We've already explained in the other thread that these kinds of logs -
    which are EXACTLY what a --verbose switch usually covers - belong on
    stderr. I don't understand the problem here.

    But have you looked into argparse? I mean, you did say you researched
    the docs, so surely you must have found it.

    ChrisA

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Weatherby,Gerard@21:1/5 to All on Wed Jan 4 14:46:56 2023
    A couple options. The -vvv is more of Linux thing rather than Pythonic, to my way of thinking.



    import argparse
    parser = argparse.ArgumentParser() parser.add_argument('-v',action='store_true') parser.add_argument('-vv',action='store_true') parser.add_argument('-vvv',action='store_true')
    args = parser.parse_args()

    parser = argparse.ArgumentParser() parser.add_argument('-v','--verbose',type=int,default=0,nargs='?',help="verbose level")
    args = parser.parse_args()
    level = args.verbose if args.verbose is not None else 0
    print(level)


    Personally I just do:

    import argparse
    import logging


    logging.basicConfig()
    parser = argparse.ArgumentParser()
    parser.add_argument('-l', '--loglevel', default='WARN', help="Python logging level")
    args = parser.parse_args()
    logger = logging.getLogger(__name__) logger.setLevel(getattr(logging,args.loglevel))

    From: Python-list <python-list-bounces+gweatherby=[email protected]> on behalf of [email protected] <[email protected]>
    Date: Wednesday, January 4, 2023 at 8:55 AM
    To: [email protected] <[email protected]>
    Subject: No solution for "--verbose" (on stdout) output in Pythonds standard library?
    *** Attention: This is an external email. Use caution responding, opening attachments or clicking on links. ***

    Hello,

    first I have to say that in my current and fresh humble opinion the
    often seen "--verbose" switch in command line applications should
    affect only the messages given to the users. This means messages on
    "stdout". That is what this question is about.

    The logging module is not an option because it works on stderr and is
    not intended to offer messages for the user but just for the system and
    its admins via logs (log-files, syslog, stderr redirection, ...).

    Using logging handlers redirecting to stdout are considered as
    workarounds by me and also not an option.

    This is not only my opinion but also part of the Python documentation: https://urldefense.com/v3/__https://docs.python.org/3/howto/logging.html*when-to-use-logging__;Iw!!Cn_UX_p3!gctSoo2C-FjIeI4wfVetRVylYZ-X1te71-Q05ylEpJ_2XICGGoFbXFjrm02smi-UKx0H2EbiEXiJLfNcgTsq$<https://urldefense.com/v3/__https:/docs.python.org/3/howto/
    logging.html*when-to-use-logging__;Iw!!Cn_UX_p3!gctSoo2C-FjIeI4wfVetRVylYZ-X1te71-Q05ylEpJ_2XICGGoFbXFjrm02smi-UKx0H2EbiEXiJLfNcgTsq$>

    I'm so detailed here about that difference between stdout and stderr
    because in the wild (e.g. on StackOverflow) you often find "use logging
    log levels" as a solution for that problem, which IMHO isn't one.

    Now the question:
    From my research on the docs it seems there is no feature in the
    standard library which could help me to implement "--verbose" or
    multiple verbosity levels like "-vvv"?
    I found some workarounds/hacks. https://urldefense.com/v3/__https://stackoverflow.com/q/5980042/4865723__;!!Cn_UX_p3!gctSoo2C-FjIeI4wfVetRVylYZ-X1te71-Q05ylEpJ_2XICGGoFbXFjrm02smi-UKx0H2EbiEXiJLU6LbUZG$<https://urldefense.com/v3/__https:/stackoverflow.com/q/5980042/4865723__;!!Cn_UX_p3!
    gctSoo2C-FjIeI4wfVetRVylYZ-X1te71-Q05ylEpJ_2XICGGoFbXFjrm02smi-UKx0H2EbiEXiJLU6LbUZG$>
    But my experience with Python as a Swiss knife is that there is always
    a standard solution for such basic and often reinvented things. I won't
    believe that each Python developer writes its own verbose feature. ;)
    -- https://urldefense.com/v3/__https://mail.python.org/mailman/listinfo/python-list__;!!Cn_UX_p3!gctSoo2C-FjIeI4wfVetRVylYZ-X1te71-Q05ylEpJ_2XICGGoFbXFjrm02smi-UKx0H2EbiEXiJLYWBuS4g$<https://urldefense.com/v3/__https:/mail.python.org/mailman/listinfo/python-
    list__;!!Cn_UX_p3!gctSoo2C-FjIeI4wfVetRVylYZ-X1te71-Q05ylEpJ_2XICGGoFbXFjrm02smi-UKx0H2EbiEXiJLYWBuS4g$>

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Eryk Sun@21:1/5 to [email protected] on Wed Jan 4 09:02:48 2023
    On 1/4/23, [email protected] <[email protected]> wrote:

    often seen "--verbose" switch in command line applications should
    affect only the messages given to the users. This means messages on
    "stdout". That is what this question is about.

    Is this additional context information such as help and definitions?
    If it's anything to do with diagnostics and telemetry, then logging to
    stderr is normal. In my experience the verbosity level typically
    applies to the latter. I might step it up as I delve deeper into a
    problem and need to examine specific details.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Richard Damon@21:1/5 to All on Wed Jan 4 10:04:33 2023
    On Jan 4, 2023, at 8:56 AM, [email protected] wrote:

    Hello,

    first I have to say that in my current and fresh humble opinion the
    often seen "--verbose" switch in command line applications should
    affect only the messages given to the users. This means messages on
    "stdout". That is what this question is about.

    The logging module is not an option because it works on stderr and is
    not intended to offer messages for the user but just for the system and
    its admins via logs (log-files, syslog, stderr redirection, ...).

    Using logging handlers redirecting to stdout are considered as
    workarounds by me and also not an option.

    This is not only my opinion but also part of the Python documentation: https://docs.python.org/3/howto/logging.html#when-to-use-logging

    I'm so detailed here about that difference between stdout and stderr
    because in the wild (e.g. on StackOverflow) you often find "use logging
    log levels" as a solution for that problem, which IMHO isn't one.

    Now the question:
    From my research on the docs it seems there is no feature in the
    standard library which could help me to implement "--verbose" or
    multiple verbosity levels like "-vvv"?
    I found some workarounds/hacks.
    https://stackoverflow.com/q/5980042/4865723
    But my experience with Python as a Swiss knife is that there is always
    a standard solution for such basic and often reinvented things. I won't believe that each Python developer writes its own verbose feature. ;)
    --
    https://mail.python.org/mailman/listinfo/python-list

    First, I would say you are incorrect that Payton ALWAYS has a standard solution for “basic” problems, as some problems (like this) aren’t actual that easy to just provide a solution.

    “Verbosity” levels, need to be defined by an application, so can’t just be provided by Python, but WILL need some effort on by the programmer.

    Second, Stack Overflow is NOT a good source of “Truth” about things, it is just a source of what people on Stack Overflow think is truth, which is up to the reader to decide if it is actually usable.

    Now, it turns out that you CAN use argparse (or similar libraries) and logging (or similar libraries) to implement a form of verbosity.

    Start off by default to NOT have logging enabled (or only enable for “High” level messages).

    Different levels of Verbosity can enable lower levels of logging, and perhaps move the logging from the “default” of stderr to stdout, all of these are possible with these libraries.

    Whether this is an “abuse” of the logging library or not is up to you are the programmer to decide, no one is making you do it that way, or saying that is the way it should be done, just a way it COULD be done.

    Personally, I might consider the logging module for this, or I might just add print statements conditioned on a verbosity level set by the argument parsing module I was using.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)