Package: traceback-with-variables
Python Traceback (Error Message) Printing VariablesVery simple to use, but versatile when needed. Try for debug and keep for production.“It is useless work that darkens the heart.”– Ursula K. Le GuinTired 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:warning:This module is actively updated and has a substantial list of features to add this week: so any proposal or advice or warning is very welcome and will be taken into account of course. When I started it I wanted to make a tool meeting all standard use cases. I think in this particular domain this is rather achievable, so I'll try. Notenext_versionbranch also. Have fun!Installationpip install traceback-with-variablesconda install -c conda-forge traceback-with-variables🚀 Quick StartUsing without code editing,running your script/command/module:traceback-with-variables tested_script.py ...srcipt's args...Simplest usage in regular Python, for the whole program:fromtraceback_with_variablesimportactivate_by_importSimplest usage in Jupyter or IPython, for the whole program:fromtraceback_with_variablesimportactivate_in_ipython_by_importDecorator, for a single function:@prints_exc# def main(): or def some_func(...):Context, for a single code block:withprinting_exc():Work with traceback linesin a custom manner:lines=list(iter_exc_lines(e))No exception but you want to print the stack anyway?:print_cur_tb()Using a logger [with a decorator,with a context]:withprinting_exc(file_=LoggerAsFile(logger)):# or@prints_exc(file_=LoggerAsFile(logger)):Print tracebackin interactive mode after an exception:>>> print_exc()Customizeany of the previous examples:default_format.max_value_str_len=10000default_format.skip_files_except='my_project'ColorsHow 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_importdef 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 / widthAnd 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 zeroAutomate your logging too:logger=logging.getLogger('main')defmain():...withprinting_exc(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 zeroFree your exceptions of unnecessary information load:defmake_a_cake(sugar,eggs,milk,flour,salt,water):is_sweet=sugar>saltis_vegan=not(eggsormilk)is_huge=(sugar+eggs+milk+flour+salt+water>10000)ifnot(is_sweetoris_veganoris_huge):raiseValueError('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 (watch out for sensitive data
like passwords and tokens in you logs). Note: you can deploy more serious frameworks,
e.g.Sentry:)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 recipesrun python code without changes: a script, a module, a commnadsimple usagesimple usage in Jupyter or IPythonprint current stack, when there's no exceptionprint last exception in Python consolemanually change global printermanually change global printer in Jupyter or IPythonworking with a functionworking with a function, loggingworking with a code blockworking with a code block, loggingget traceback lines for custom thingsusing withflaskcustomize the outputReferenceAll functions havefmt=argument, aFormatobject with fields:max_value_str_lenmax length of each variable string, -1 to disable, default=1000max_exc_str_lenmax length of exception, should variable print fail, -1 to disable, default=10000beforenumber of code lines before the raising line, default=0afternumber of code lines after the raising line, default=0ellipsis_string to denote long strings truncation, default=...skip_files_exceptuse to print only certain files; list of regexes, ignored if empty, default=Nonebrief_files_exceptuse to print variables only in certain files; list of regexes, ignored if empty, default=Nonecustom_var_printerslist of pairs of (filter, printer); filter is a name fragment, a type or a function or a list thereof; printer returnsNoneto skip a varcolor_schemeisNoneor one ofColorSchemes:.none,.common,.nice,.synthwave.Noneis for auto-detectactivate_by_importJust import it. No arguments, for real quick use in regular Python.fromtraceback_with_variablesimportactivate_by_importactivate_in_ipython_by_importJust import it. No arguments, for real quick use in Jupyter or IPython.fromtraceback_with_variablesimportactivate_in_ipython_by_importglobal_print_excCall once in the beginning of your program, to change how traceback after an uncaught exception looks.defmain():override_print_exc(...)global_print_exc_in_ipythonCall once in the beginning of your program, to change how traceback after an uncaught exception looks.defmain():override_print_exc(...)print_excPrints traceback for a given/current/last (first being notNonein the priority list) exception to a file, default=sys.stderr. Convenient for manual console or Jupyter sessions or custom try/except blocks. Note that it can be called with a given exception value or it can auto discover current exception in anexcept:block or it can auto descover last exception value (long) aftertry/catchblock.print_exc()print_cur_tbPrints current traceback when no exception is raised.print_cur_tb()prints_excFunction 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_excdeff(...):@prints_exc(...)deff(...):printing_excContext 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 thewithscope. Program exiting due to unhandled exception still prints a usual traceback.withprinting_exc(...):LoggerAsFileA logger-to-file wrapper, to pass a logger to print tools as a file.iter_exc_linesIterates the lines, which are usually printed one-by-one in terminal.format_excLikeiter_exc_linesbut returns a single string.iter_cur_tb_linesLikeiter_exc_linesbut doesn't need an exception and prints upper frames..format_cur_tbLikeiter_cur_tb_linesbut returns a single string.FAQIn Windows console crash messages have no colors.The default Windows console/terminal cannot print [so calledansi] colors, but this isfixable, 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 calledansi] colors, but this isfixable, 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 noneconsole 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 noneconsole argument.
The choice for keeping colors in piped output was made to allow convenient usage ofhead,tail, file redirection etc.
In cases like| grepit might have issues, in which case you can disable colors.Output redirected to a file in> output.txtmanner has no colors when Icatit.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_importorglobal_print_excdon't work in Jupyter or IPython as if not called at all.In Jupyter or IPython you should useactivate_in_ipython_by_importorglobal_print_exc_in_ipython. IPython handles exceptions differently than regular Python.The server framework (flask,streamlitetc.) still shows usual tracebacks.In such frameworks tracebacks are printed not while exiting the program (the program continues running), hence you should override exception handling in a manner
proper for the given framework. Please address theflaskexample.How do I reduce output? I don't need all files or all variables.Useskip_files_except,brief_files_except,custom_var_printersto cut excess output.I have ideas about good colors.Please fork, add a newColorSchemetoColorSchemesand create a Pull Request tonext_versionbranch.Choose the color codesand visually
test it likepython3 -m traceback_with_variables.main --color-scheme {its name} examples/for_readme_image.py.My code doesn't work.Pleasepost your case. You are very welcome!Other questions or requests to elaborate answers.Pleasepost your question or request. You are very welcome!