Most viewed Python recipes tagged "debugging"http://code.activestate.com/recipes/langs/python/tags/debugging/views/2011-01-24T18:40:51-08:00ActiveState Code RecipesDump all the attributes of an object (Python) 2002-07-05T23:05:49-07:00Philip Kromerhttp://code.activestate.com/recipes/users/552075/http://code.activestate.com/recipes/137951-dump-all-the-attributes-of-an-object/ <p style="color: grey"> Python recipe 137951 by <a href="/recipes/users/552075/">Philip Kromer</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>Print a nicely formatted overview of an object, including _everything_ in the object's `dir'. This is great when programming interactively.</p> <p>More comprehensive than help(), prettier than dir().</p> Add a method to a class instance at runtime (Python) 2001-02-22T17:04:07-08:00asdfsa asdfdsafhttp://code.activestate.com/recipes/users/515672/http://code.activestate.com/recipes/52192-add-a-method-to-a-class-instance-at-runtime/ <p style="color: grey"> Python recipe 52192 by <a href="/recipes/users/515672/">asdfsa asdfdsaf</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>This recipe demonstrates the runtime addition of a __str__ method to a class instance. This can sometimes be useful for debugging purposes. It also demonstrates the use of the two special attributes of class instances: '__dict__' and '__class__'.</p> Automatically start the debugger on an exception (Python) 2001-07-13T08:39:47-07:00Thomas Hellerhttp://code.activestate.com/recipes/users/98141/http://code.activestate.com/recipes/65287-automatically-start-the-debugger-on-an-exception/ <p style="color: grey"> Python recipe 65287 by <a href="/recipes/users/98141/">Thomas Heller</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 5. </p> <p>When Python runs a script and an uncatched exception is raised, a traceback is printed and the script is terminated. Python2.1 has introduced sys.excepthook, which can be used to override the handling of uncaught exceptions. This allows to automatically start the debugger on an unexpected exception, even if python is not running in interactive mode.</p> Debug with garbage collection (Python) 2001-06-25T04:46:01-07:00Dirk Holtwickhttp://code.activestate.com/recipes/users/98196/http://code.activestate.com/recipes/65333-debug-with-garbage-collection/ <p style="color: grey"> Python recipe 65333 by <a href="/recipes/users/98196/">Dirk Holtwick</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>You now that there is garbage in your program but you don't know what exactly. In this piece of code additionaly to the normal debugging output of gc the object itself are shown to get an idea where the leak may be.</p> Extending the 'logging' module (Python) 2006-02-27T00:57:54-08:00Ori Peleghttp://code.activestate.com/recipes/users/2056315/http://code.activestate.com/recipes/474089-extending-the-logging-module/ <p style="color: grey"> Python recipe 474089 by <a href="/recipes/users/2056315/">Ori Peleg</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 2. </p> <p>Adding new format specifiers to the logging module. In this example, it's for the user name and the name of the function that logged the message.</p> LoggingWebMonitor - a central logging server and monitor. (Python) 2010-02-02T01:56:42-08:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/577025-loggingwebmonitor-a-central-logging-server-and-mon/ <p style="color: grey"> Python recipe 577025 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/client_server/">client_server</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/distributed/">distributed</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/remote/">remote</a>, <a href="/recipes/tags/sysadmin/">sysadmin</a>, <a href="/recipes/tags/web/">web</a>). Revision 3. </p> <p>LoggingWebMonitor listens for log records sent from other processes running in the same box or network. Collects and saves them concurrently in a log file. Shows a summary web page with the latest N records received.</p> Reloading all modules (Python) 2001-10-15T02:08:03-07:00Sébastien Keimhttp://code.activestate.com/recipes/users/131730/http://code.activestate.com/recipes/81731-reloading-all-modules/ <p style="color: grey"> Python recipe 81731 by <a href="/recipes/users/131730/">Sébastien Keim</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>When you create a Python module, you can use a test script wich import your module. But you probably have noticed that when you run the test script, it always use the first version of your module even if you made changes in the code. This is because the import statement check if the module is already in memory and do the import stuff only when this is mandated.</p> <p>You can use the reload() function but this is quite difficult if you do changes in a module wich isn't directly imported by your test script.</p> <p>A good solution could be to remove all modules from memory before running the test script. You only have to put some few lines at the start of your test script.</p> Unicode String Hex Dump (Python) 2008-05-03T01:12:27-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/572181-unicode-string-hex-dump/ <p style="color: grey"> Python recipe 572181 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 3. </p> <p>Simple routine for dumping any kind of string, ascii, encoded, or unicode, to a standard hex dump. Plus read/write of unicode and encoded strings.</p> sane tab completion in pdb (Python) 2009-07-06T04:35:33-07:00Stephen Emsliehttp://code.activestate.com/recipes/users/4004606/http://code.activestate.com/recipes/498182-sane-tab-completion-in-pdb/ <p style="color: grey"> Python recipe 498182 by <a href="/recipes/users/4004606/">Stephen Emslie</a> (<a href="/recipes/tags/complete/">complete</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/pdb/">pdb</a>, <a href="/recipes/tags/tab/">tab</a>). Revision 6. </p> <p>I make frequent use of python's built-in debugger, but one obvious feature seems to be missing - the bash-like tab completion that you can add to the interpreter. Fortunately pdb's interactive prompt is an instance of Cmd, so we can write our own completion function.</p> <p>Note: this uses rlcompleter, which isn't available on windows</p> <p>Edit (6 Jul 2009): import rlcompleter early and force output to stdout to ensure monkeypatch sticks Edit: updated to handle changes in local scope Edit: Fixed start via 'python -m pdb ...'. Check the comments for details.</p> Generating Code Coverage HTML Output (Python) 2010-10-17T15:17:44-07:00Sebastien Martinihttp://code.activestate.com/recipes/users/2637141/http://code.activestate.com/recipes/491274-generating-code-coverage-html-output/ <p style="color: grey"> Python recipe 491274 by <a href="/recipes/users/2637141/">Sebastien Martini</a> (<a href="/recipes/tags/coverage/">coverage</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/html/">html</a>). Revision 11. </p> <p>Code coverage testing is very useful especially for dynamic languages, it can easily give a view of the unused parts of your code. Not that this method is able to prove that statements are dead code, but instead help to prevent from syntax errors and to force yourself to think about your code, to find the right test cases reaching the unused statements.</p> <p>In that sense, the module <a href="http://www.nedbatchelder.com/code/modules/coverage.py">coverage.py</a> made by <a href="http://www.nedbatchelder.com">Ned Batchelder</a> is very useful and efficient.</p> <p>The analysis returned by this module is very accurate, but as unreached lines numbers are not very readable by itself, this recipe simply generate an html output highlighting unreached lines. This recipe is directely based upon a <a href="http://chrisarndt.de/en/software/python/colorize.html">source code colorizer</a> derived from <a href="http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52298">this</a> recipe.</p> TimedCompressedRotatingFileHandler (Python) 2007-02-28T08:16:44-08:00Angel Freirehttp://code.activestate.com/recipes/users/4037917/http://code.activestate.com/recipes/502265-timedcompressedrotatingfilehandler/ <p style="color: grey"> Python recipe 502265 by <a href="/recipes/users/4037917/">Angel Freire</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>Python has really nice logging framework, it has too a zipfile library in the default installation that makes you able to write compressed files.</p> <p>Several of the logging handlers "rotate" files, by size, date, etc. Here is an example of handler class for the logging framework that, when the file is rotated, it will make a .zip of the old file.</p> Using the logging module (Python) 2005-05-04T05:41:45-07:00Christopher Dunnhttp://code.activestate.com/recipes/users/1683375/http://code.activestate.com/recipes/412552-using-the-logging-module/ <p style="color: grey"> Python recipe 412552 by <a href="/recipes/users/1683375/">Christopher Dunn</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 7. </p> <p>This amazingly powerful module is a mystery to novices. Once you figure it out, you'll use it everywhere.</p> <p>In addition to examples, here are a couple of useful filters.</p> Print Hook (Python) 2002-04-04T03:03:55-08:00anurag uniyalhttp://code.activestate.com/recipes/users/127639/http://code.activestate.com/recipes/119404-print-hook/ <p style="color: grey"> Python recipe 119404 by <a href="/recipes/users/127639/">anurag uniyal</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>Hook on stdout and stderr so that we can handle printing of text,error differently e.g in GUI base application divert text to a log window.</p> Trace decorator for debugging (Python) 2011-01-24T18:40:51-08:00Kevin L. Sitzehttp://code.activestate.com/recipes/users/4173535/http://code.activestate.com/recipes/577551-trace-decorator-for-debugging/ <p style="color: grey"> Python recipe 577551 by <a href="/recipes/users/4173535/">Kevin L. Sitze</a> (<a href="/recipes/tags/classes/">classes</a>, <a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/inspection/">inspection</a>, <a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/trace/">trace</a>). Revision 2. </p> <p>This package provides a decorator for tracing function and method calls in your applications. The tracing capabilities are managed through the logging package, and several mechanisms are provided for controlling the destination of the trace output.</p> <p>It also provides functionality for adding decorators to existing classes or modules.</p> Integrating Twisted reactor with IPython (Python) 2005-04-22T01:26:20-07:00Matthew Scotthttp://code.activestate.com/recipes/users/2422085/http://code.activestate.com/recipes/410670-integrating-twisted-reactor-with-ipython/ <p style="color: grey"> Python recipe 410670 by <a href="/recipes/users/2422085/">Matthew Scott</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>Runs the Twisted reactor event loop in a thread alongside an IPython shell, for introspecting a running Twisted process.</p> performance testing with a pystone measurement decorator (Python) 2005-10-12T16:18:30-07:00Tarek Ziadéhttp://code.activestate.com/recipes/users/2624842/http://code.activestate.com/recipes/440700-performance-testing-with-a-pystone-measurement-dec/ <p style="color: grey"> Python recipe 440700 by <a href="/recipes/users/2624842/">Tarek Ziadé</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 2. </p> <p>This recipe explains how to check that a given function does not run slower than a given pystone rate. It first calculates the pystone ratio on your box.</p> AST Pretty Printer (Python) 2007-10-09T20:22:37-07:00Martin Blaishttp://code.activestate.com/recipes/users/1643324/http://code.activestate.com/recipes/533146-ast-pretty-printer/ <p style="color: grey"> Python recipe 533146 by <a href="/recipes/users/1643324/">Martin Blais</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 2. </p> <p>A function that outputs a human-readable version of a Python AST.</p> Debug runtime objects using gc.get_objects() (Python) 2005-11-27T08:01:49-08:00Dirk Holtwickhttp://code.activestate.com/recipes/users/636691/http://code.activestate.com/recipes/457665-debug-runtime-objects-using-gcget_objects/ <p style="color: grey"> Python recipe 457665 by <a href="/recipes/users/636691/">Dirk Holtwick</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>Since Python 2.2 there is a handy function in the Garbage Collection Module called get_objects(). It gives back a list of all objects that are under control of the Garbeage Collector. This way you can extract informations of your application in runtime.</p> include function name and line number automatically in debug statements (Python) 2002-08-14T15:06:48-07:00Christian Birdhttp://code.activestate.com/recipes/users/627471/http://code.activestate.com/recipes/144838-include-function-name-and-line-number-automaticall/ <p style="color: grey"> Python recipe 144838 by <a href="/recipes/users/627471/">Christian Bird</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>This recipe allows a user to place debug messages, error messages and standard messages throughout a program. The function name and line number will be added to each debug and error message before it is printed out. In addition, each of these messages can be passed to multiple handler objects that can direct the output to log files, e-mails, stdout, etc.</p> Email pretty tracebacks to yourself (or someone you love) (Python) 2005-10-19T08:13:27-07:00Cliff Wellshttp://code.activestate.com/recipes/users/2631558/http://code.activestate.com/recipes/442459-email-pretty-tracebacks-to-yourself-or-someone-you/ <p style="color: grey"> Python recipe 442459 by <a href="/recipes/users/2631558/">Cliff Wells</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>Even production applications have bugs, and it would be nice to have Python tracebacks emailed to you rather than dumped to the hapless user's screen. This recipe shows you how.</p>