easy verbose in functions

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pascal

    #1

    easy verbose in functions

    Hello,

    I've a function like this:
    def myfunction(data , verbose = False):
    dothis...
    if verbose: print 'I do this'
    dothisother
    if verbose: print 'I do this other'
    ...

    How can I do to exclude eachtime the `if verbose` statement?

    I tried `print verbose and 'I do this'` but it's printing `False`.
    So I tried with `verbose = None` in the definition, but it's printing None!

    Thanks.
  • Timo Virkkala

    #2
    Re: easy verbose in functions

    Pascal wrote:[color=blue]
    > Hello,
    >
    > I've a function like this:
    > def myfunction(data , verbose = False):
    > dothis...
    > if verbose: print 'I do this'
    > dothisother
    > if verbose: print 'I do this other'
    > ...
    >
    > How can I do to exclude eachtime the `if verbose` statement?[/color]

    How about defining a function like:

    def myprint(arg):
    if verbose:
    print arg

    and using that?

    --
    Timo Virkkala

    Comment

    • Duncan Booth

      #3
      Re: easy verbose in functions

      pascal.parent@f ree.fr (Pascal) wrote in
      news:e567c03a.0 403290150.727c9 f63@posting.goo gle.com:
      [color=blue]
      > def myfunction(data , verbose = False):
      > dothis...
      > if verbose: print 'I do this'
      > dothisother
      > if verbose: print 'I do this other'
      > ...
      >
      > How can I do to exclude eachtime the `if verbose` statement?
      >[/color]

      If it offends you that much:

      def log_verbose(s):
      print s
      def log_dummy(s):
      pass


      def myfunction(data , verbose = False):
      if verbose:
      log = log_verbose
      else:
      log = log_dummy
      dothis...
      log('I do this')
      dothisother
      log('I do this other')
      ...

      or even pass the log function around as the parameter.

      A more general solution might be to use Python's logging facility. That
      gives you the opportunity to classify your log messages by different levels
      (debug, info, warning, error, critical) and also by the section of program
      in a hierarchical arrangement. This gives you the ability to, say, turn on
      debug logging for a class, or a module by editing a config file.

      Comment

      • Miki Tebeka

        #4
        Re: easy verbose in functions

        Hello Pascal,
        [color=blue]
        > I've a function like this:
        > def myfunction(data , verbose = False):
        > dothis...
        > if verbose: print 'I do this'
        > dothisother
        > if verbose: print 'I do this other'
        > ...
        >
        > How can I do to exclude eachtime the `if verbose` statement?
        >
        > I tried `print verbose and 'I do this'` but it's printing `False`.
        > So I tried with `verbose = None` in the definition, but it's printing None![/color]
        Try the logging package?

        HTH.
        Miki

        Comment

        • Steve

          #5
          Re: easy verbose in functions

          Hello Pascal,

          Pascal wrote:[color=blue]
          > Hello,
          >
          > I've a function like this:
          > def myfunction(data , verbose = False):
          > dothis...
          > if verbose: print 'I do this'
          > dothisother
          > if verbose: print 'I do this other'
          > ...
          >
          > How can I do to exclude eachtime the `if verbose` statement?
          >
          > I tried `print verbose and 'I do this'` but it's printing `False`.
          > So I tried with `verbose = None` in the definition, but it's printing None![/color]

          I'm not sure exactly what you are doing because I can't
          duplicate your results.

          But I am guessing that what you are doing is passing a
          string "False" instead of the boolean constant False.
          The constant False is only defined in Python 2.2 and
          higher.

          You can put something like this at the beginning of
          your program to make sure that False and True are
          defined no matter what version of Python you are using:

          try:
          if False:
          pass
          except:
          False = 0
          True = not False


          Notice the difference between a string and a boolean value:
          [color=blue][color=green][color=darkred]
          >>> print False; print "False"[/color][/color][/color]
          0
          False[color=blue][color=green][color=darkred]
          >>> type(False); type("False")[/color][/color][/color]
          <type 'int'>
          <type 'str'>

          Likewise None is not the same as "None".


          The way I would handle verbose printing in functions is
          to factor out the "verbosity" from the function logic:

          def verboseprint(s, verbose):
          if verbose: print s

          def myfunction(arg, verbose=False):
          dothis
          verboseprint("D o this", verbose)
          dothat
          verboseprint("D o that", verbose)


          That way, if there is a bug in your handling of verbose
          printing, you only need to fix it in one place. If you
          decide to change verbose printing to (say) using a log
          file, you only need to change one place.

          The statement:

          print verbose and "Do this"


          will not work. If verbose is True, then the term
          (verbose and "Do this") will evaluate to "Do this", and
          Python will print what you expect, namely "Do this".

          But if verbose is False, then the term (verbose and "Do
          this") will evaluate to False, and Python will then
          print False, which is not what you want.



          --
          Steven D'Aprano


          Comment

          Working...