Sunday, November 29, 2020

Add variables to Python traceback

Example

Python Traceback (Error Message) Printing Variables

Very simple to use, but versatile when needed. Try for debug and keep for production.

Actions Status Codecov Language grade: Python License: MIT PyPI PyPI Platform Annotations coverage No-OOP Dependencies Gitter


“It is useless work that darkens the heart.” – Ursula K. Le Guin

Tired of useless job of putting all your variables into debug exception messages? Just stop it. Automate it and clean your code. Once and for all.


Contents: Installation | 🚀 Quick Start | Colors | How does it save my time? | Examples and recipes | Reference | FAQ


Installation

pip install traceback-with-variables
conda install -c conda-forge traceback-with-variables

🚀 Quick Start

Using without code editing, running your script/command/module:

traceback-with-variables ...script/tool/module with arguments...

Simplest usage in regular Python, for the whole program:

    from traceback_with_variables import activate_by_import

Simplest usage in Jupiter or IPython, for the whole program:

    from traceback_with_variables import activate_in_ipython_by_import

Decorator, for a single function:

    @prints_tb
    # def main(): or def some_func(...):

Context, for a single code block:

Work with traceback lines in a custom manner:

    return '\n'.join(iter_tb_lines(e))

Using a logger [with a decorator, with a context]:

    with printing_tb(file_=LoggerAsFile(logger)):
    # or
    @prints_tb(file_=LoggerAsFile(logger)): 

Colors

Example

How does it save my time?

  • Turn a code totally covered by debug formatting from this:

      def main():
          sizes_str = sys.argv[1]
          h1, w1, h2, w2 = map(int, sizes_str.split())
    -     try:
              return get_avg_ratio([h1, w1], [h2, w2])
    -     except:
    -         logger.error(f'something happened :(, variables = {locals()[:1000]}')
    -         raise
    -         # or
    -         raise MyToolException(f'something happened :(, variables = {locals()[:1000]}')
              
      def get_avg_ratio(size1, size2):
    -     try:
              return mean(get_ratio(h, w) for h, w in [size1, size2])
    -     except:
    -         logger.error(f'something happened :(, size1 = {size1}, size2 = {size2}')
    -         raise
    -         # or
    -         raise MyToolException(f'something happened :(, size1 = {size1}, size2 = {size2}')
    
      def get_ratio(height, width):
    -     try:
              return height / width
    -     except:
    -         logger.error(f'something happened :(, width = {width}, height = {height}')
    -         raise
    -         # or
    -         raise MyToolException(f'something happened :(, width = {width}, height = {height}')

    into this (zero debug code):

    + from traceback_with_variables import activate_by_import
    
      def main():
          sizes_str = sys.argv[1]
          h1, w1, h2, w2 = map(int, sizes_str.split())
          return get_avg_ratio([h1, w1], [h2, w2])
              
      def get_avg_ratio(size1, size2):
          return mean(get_ratio(h, w) for h, w in [size1, size2])
    
      def get_ratio(height, width):
          return height / width

    And obtain total debug info spending 0 lines of code:

    Traceback with variables (most recent call last):
      File "./temp.py", line 7, in main
        return get_avg_ratio([h1, w1], [h2, w2])
          sizes_str = '300 200 300 0'
          h1 = 300
          w1 = 200
          h2 = 300
          w2 = 0
      File "./temp.py", line 10, in get_avg_ratio
        return mean([get_ratio(h, w) for h, w in [size1, size2]])
          size1 = [300, 200]
          size2 = [300, 0]
      File "./temp.py", line 10, in <listcomp>
        return mean([get_ratio(h, w) for h, w in [size1, size2]])
          .0 = <tuple_iterator object at 0x7ff61e35b820>
          h = 300
          w = 0
      File "./temp.py", line 13, in get_ratio
        return height / width
          height = 300
          width = 0
    builtins.ZeroDivisionError: division by zero
    
  • Automate your logging too:

    logger = logging.getLogger('main')
    
    def main():
        ...
        with printing_tb(file_=LoggerAsFile(logger))
            ...
    2020-03-30 18:24:31 main ERROR Traceback with variables (most recent call last):
    2020-03-30 18:24:31 main ERROR   File "./temp.py", line 7, in main
    2020-03-30 18:24:31 main ERROR     return get_avg_ratio([h1, w1], [h2, w2])
    2020-03-30 18:24:31 main ERROR       sizes_str = '300 200 300 0'
    2020-03-30 18:24:31 main ERROR       h1 = 300
    2020-03-30 18:24:31 main ERROR       w1 = 200
    2020-03-30 18:24:31 main ERROR       h2 = 300
    2020-03-30 18:24:31 main ERROR       w2 = 0
    2020-03-30 18:24:31 main ERROR   File "./temp.py", line 10, in get_avg_ratio
    2020-03-30 18:24:31 main ERROR     return mean([get_ratio(h, w) for h, w in [size1, size2]])
    2020-03-30 18:24:31 main ERROR       size1 = [300, 200]
    2020-03-30 18:24:31 main ERROR       size2 = [300, 0]
    2020-03-30 18:24:31 main ERROR   File "./temp.py", line 10, in <listcomp>
    2020-03-30 18:24:31 main ERROR     return mean([get_ratio(h, w) for h, w in [size1, size2]])
    2020-03-30 18:24:31 main ERROR       .0 = <tuple_iterator object at 0x7ff412acb820>
    2020-03-30 18:24:31 main ERROR       h = 300
    2020-03-30 18:24:31 main ERROR       w = 0
    2020-03-30 18:24:31 main ERROR   File "./temp.py", line 13, in get_ratio
    2020-03-30 18:24:31 main ERROR     return height / width
    2020-03-30 18:24:31 main ERROR       height = 300
    2020-03-30 18:24:31 main ERROR       width = 0
    2020-03-30 18:24:31 main ERROR builtins.ZeroDivisionError: division by zero
    
  • Free your exceptions of unnecessary information load:

    def make_a_cake(sugar, eggs, milk, flour, salt, water):
        is_sweet = sugar > salt
        is_vegan = not (eggs or milk)
        is_huge = (sugar + eggs + milk + flour + salt + water > 10000)
        if not (is_sweet or is_vegan or is_huge):
            raise ValueError('This is unacceptable, guess why!')
        ...
  • — Should I use it after debugging is over, i.e. in production?

    Yes, of course! That way it might save you even more time.


  • Stop this tedious practice in production:

    step 1: Notice some exception in a production service.
    step 2: Add more printouts, logging, and exception messages.
    step 3: Rerun the service.
    step 4: Wait till (hopefully) the bug repeats.
    step 5: Examine the printouts and possibly add some more info (then go back to step 2).
    step 6: Erase all recently added printouts, logging and exception messages.
    step 7: Go back to step 1 once bugs appear.

Examples and recipes

Reference

All functions have output customization

  • max_value_str_len max length of each variable string, -1 to disable, default=1000
  • max_exc_str_len max length of exception, should variable print fail, -1 to disable, default=10000
  • num_context_lines number of lines around the target code line to print, default=1
  • ellipsis_ string to denote long strings truncation, default=...
  • file_ where to print exception, a file or a wrapped logger, default=sys.stderr i.e. usual printing to console
  • color_scheme one of ColorSchemes: .auto, .none , .common, .nice, .synthwave

Just import it. No arguments, for real quick use in regular Python.

from traceback_with_variables import activate_by_import

Just import it. No arguments, for real quick use in Jupyter or IPython.

from traceback_with_variables import activate_in_ipython_by_import

Call once in the beginning of your program, to change how traceback after an uncaught exception looks.

def main():
    override_print_tb(...)

Function decorator, used for logging or simple printing of scoped tracebacks with variables. I.e. traceback is shorter as it includes only frames inside the function call. Program exiting due to unhandled exception still prints a usual traceback.

@prints_tb
def f(...):

@prints_tb(...)
def f(...):

Context manager (i.e. with ...), used for logging or simple printing of scoped tracebacks with variables. I.e. traceback is shorter as it includes only frames inside the with scope. Program exiting due to unhandled exception still prints a usual traceback.


A logger-to-file wrapper, to pass a logger to print tools as a file.


Iterates the lines, which are usually printed one-by-one in terminal.


FAQ

  • In Windows console crash messages have no colors.

    The default Windows console/terminal cannot print [so called ansi] colors, but this is fixable , especially with modern Windows versions. Therefore colors are disabled by default, but you can enable them and check if it works in your case. You can force enable colors by passing --color-scheme common (for complete list of colors pass --help) console argument.

  • Windows console prints junk symbols when colors are enabled.

    The default Windows console/terminal cannot print [so called ansi] colors, but this is fixable , especially with modern Windows versions. If for some reason the colors are wrongly enabled by default, you can force disable colors by passing --color-scheme none console argument.

  • Bash tools like grep sometimes fail to digest the output when used with pipes (|) because of colors.

    Please disable colors by passing --color-scheme none console argument. The choice for keeping colors in piped output was made to allow convenient usage of head, tail, file redirection etc. In cases like | grep it might have issues, in which case you can disable colors.

  • Output redirected to a file in > output.txt manner has no colors when I cat it.

    This is considered a rare use case, so colors are disabled by default when outputting to a file. But you can force enable colors by passing --color-scheme common (for complete list of colors pass --help) console argument.

  • activate_by_import or override_print_tb don't work in Jupiter or IPython as if not called at all.

    Don't forget to use ipython=True or import activate_in_ipython_by_import since Jupyer/Ipython handles exceptions differently than regular Python.

  • My code doesn't work.

    Please post your case. You are very welcome!

  • Other questions or requests to elaborate answers.

    Please post your question or request. You are very welcome!



from Hacker News https://ift.tt/2KCRrfu

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.