Top-rated recipes tagged "debugging"http://code.activestate.com/recipes/tags/debugging/top/2017-01-24T20:34:52-08:00ActiveState Code RecipesCaller and Callee (Python) 2010-03-26T20:04:11-07:00Michael Grünewaldhttp://code.activestate.com/recipes/users/4172244/http://code.activestate.com/recipes/576925-caller-and-callee/ <p style="color: grey"> Python recipe 576925 by <a href="/recipes/users/4172244/">Michael Grünewald</a> (<a href="/recipes/tags/callee/">callee</a>, <a href="/recipes/tags/caller/">caller</a>, <a href="/recipes/tags/debugging/">debugging</a>). Revision 4. </p> <p>A recipe to find out which function is the caller of the current function. </p> <p>The <code>caller</code> function can be helpful for debugging &mdash; if there is no real debugger available. In terms of software engineering (loose coupling etc.) this should not be used in production code though.</p> Lightweight Unittester (Python) 2009-04-07T19:47:08-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/572194-lightweight-unittester/ <p style="color: grey"> Python recipe 572194 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 4. </p> <p>Inspired by py.test, the unittester is a single, simple function that is easily customized. The main virtue is that tests are much clearer than with the unittest.py.</p> Metaclass for Interface Checking (Python) 2003-06-12T02:00:39-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/204349-metaclass-for-interface-checking/ <p style="color: grey"> Python recipe 204349 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 4. </p> <p>Checks a class definition for required attributes &lt;br&gt; To use it, add two lines to your class, __metaclass__=InterfaceChecker and __implements__=[InterfaceName]. The example below generates the following error message: &lt;br&gt; InterfaceOmission: ['__delitem__'] &lt;br&gt; Verifying interfaces for an object becomes trivial. For instance, if you need to validate that variable 'x' implements a minimal sequence interface, verify that: &lt;br&gt; MinimalSequence in x.__implements__</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> doctest, unittest, and python 2.4's cool doctest.DocFileSuite (Python) 2004-09-16T08:59:32-07:00John Nielsenhttp://code.activestate.com/recipes/users/135654/http://code.activestate.com/recipes/305292-doctest-unittest-and-python-24s-cool-doctestdocfil/ <p style="color: grey"> Python recipe 305292 by <a href="/recipes/users/135654/">John Nielsen</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>Doctest and unittest are like 2 extremes of testing your python code. One is simple and intuitive and the other is powerful and requires more formality. In Python 2.4, doctest, builds in some of the power of unittest, perhaps bridging the gap between the two. This example shows you how to use doctest and the doctest.DocFileSuite feature.</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> Repeat procedure every X seconds (Tcl) 2001-09-10T15:20:39-07:00Jeff Hobbshttp://code.activestate.com/recipes/users/98167/http://code.activestate.com/recipes/68393-repeat-procedure-every-x-seconds/ <p style="color: grey"> Tcl recipe 68393 by <a href="/recipes/users/98167/">Jeff Hobbs</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>This is a cheap rescheduler that uses the built-in after command to repeat a command every X milliseconds. It has cancel and info commands similar to the after command.</p> Tiny trace/debug-logging tool with smart event filtering (Python) 2011-04-29T02:23:37-07:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/577429-tiny-tracedebug-logging-tool-with-smart-event-filt/ <p style="color: grey"> Python recipe 577429 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/trace/">trace</a>). Revision 3. </p> <ul> <li><p>Just import, call and log your selected call/return/exception/etc. events.</p></li> <li><p>You can use a standard Python logger from the <code>logging</code> module.</p></li> <li><p>Quite flexible tool and about 50 efective SLOC only (excluding the example script).</p></li> <li><p>For debugging rather than production environments (programs noticeably slow down).</p></li> </ul> Stack-based indentation of formatted logging (Python) 2005-04-28T23:27:16-07:00Christopher Dunnhttp://code.activestate.com/recipes/users/1683375/http://code.activestate.com/recipes/412603-stack-based-indentation-of-formatted-logging/ <p style="color: grey"> Python recipe 412603 by <a href="/recipes/users/1683375/">Christopher Dunn</a> (<a href="/recipes/tags/debugging/">debugging</a>). Revision 3. </p> <p>Some people like to sprinkle stack trace information in their code, and it is always helpful to get a visual clue to the call stack depth. inspect.stack() contains the entire call stack.</p> <p>To make this information available conditionally, on a per-subsystem basis, the logging module is helpful. Here is one way to combine the two ideas.</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> Use DebugView utility with standard python logging (Python) 2010-02-10T10:35:59-08:00Christopher Prinoshttp://code.activestate.com/recipes/users/481494/http://code.activestate.com/recipes/577040-use-debugview-utility-with-standard-python-logging/ <p style="color: grey"> Python recipe 577040 by <a href="/recipes/users/481494/">Christopher Prinos</a> (<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/debugview/">debugview</a>, <a href="/recipes/tags/logging/">logging</a>, <a href="/recipes/tags/sysinternals/">sysinternals</a>, <a href="/recipes/tags/windows/">windows</a>). </p> <p>This is a custom logging.Handler class that lets you use standard logging calls to output messages to SysInternals' DebugView utility.</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> Get the value of a cell from a closure (Python) 2005-08-11T14:46:42-07:00paul cannonhttp://code.activestate.com/recipes/users/2551140/http://code.activestate.com/recipes/439096-get-the-value-of-a-cell-from-a-closure/ <p style="color: grey"> Python recipe 439096 by <a href="/recipes/users/2551140/">paul cannon</a> (<a href="/recipes/tags/debugging/">debugging</a>). </p> <p>Python closures hold closed-over values in a special datatype called a "cell"; a sort of indirect pointer. It's not simple, though, to see what values are stored there. Here's the key.</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> Testing Tkinter or Selenium for Tkinter (Python) 2017-01-24T20:34:52-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580751-testing-tkinter-or-selenium-for-tkinter/ <p style="color: grey"> Python recipe 580751 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/rpyc/">rpyc</a>, <a href="/recipes/tags/selenium/">selenium</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 3. </p> <p>This code is a little variation of my other trick:</p> <p><a href="https://code.activestate.com/recipes/580721-tkinter-remote-debugging" rel="nofollow">https://code.activestate.com/recipes/580721-tkinter-remote-debugging</a></p> <p>It makes more easy to create tests for Tkinter.</p> <p>Install rpyc:</p> <blockquote> <p>pip install rpyc</p> </blockquote> <p>Save the code below to a file named for example tkinter_selenium.py.</p> <p>This is the usage:</p> <blockquote> <p>python tkinter_selenium.py [-h] [-p PORT] filename</p> </blockquote> <p>where filename is the path to main file of Tkinter application, and port is an optional port number for the remote interpreter. Otherwise it uses default port.</p> <p>Then in another python interpreter you can interact with the application. For example, write:</p> <pre class="prettyprint"><code>import rpyc c = rpyc.classic.connect("localhost") c.execute(""" from Tkinter import Button, Toplevel import tkMessageBox responsive_button = Button(Toplevel(), text="It's responsive", command = lambda:tkMessageBox.showinfo("alert window", "It's responsive!")) responsive_button.pack() """) responsive_button = c.eval("responsive_button") responsive_button.invoke() </code></pre> <p>(This example only works for Python 2. For python 3 use "tkinter" instead of "Tkinter" and so on)</p> <p>Use port keyword argument to <em>"repyc.classic.connect"</em> if you want a different port number than default port. For example:</p> <pre class="prettyprint"><code>import rpyc c = rpyc.classic.connect("localhost", port=8000) </code></pre> <p>For the selection of tkinter widgets, I have this other trick:</p> <p><a href="https://code.activestate.com/recipes/580738-tkinter-selectors" rel="nofollow">https://code.activestate.com/recipes/580738-tkinter-selectors</a></p> <p>Using this remote debugging utility and selectors makes easy to test tkinter applications similar to selenium.</p> <p>This utility could be used not only for Tkinter applications. It could be used also for wxpython, pygtk and pyqt applications.</p> <p>NOTE: Interact with remote application using python of same version. If the application is running using a Python 2 interpreter, use a python 2 interpreter for remote interaction. Similarly use a python 3 interpreter for remote interaction with a python 3 application.</p> Two quick functions for object introspection (Python) 2017-01-14T22:35:17-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580747-two-quick-functions-for-object-introspection/ <p style="color: grey"> Python recipe 580747 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/functions/">functions</a>, <a href="/recipes/tags/introspection/">introspection</a>, <a href="/recipes/tags/methods/">methods</a>, <a href="/recipes/tags/objects/">objects</a>, <a href="/recipes/tags/reflection/">reflection</a>). </p> <p>This recipe shows two quick-and-clean :) utility functions for introspection of Python objects. They are meant to be used while working interactively in the reular Python shell or in the IPython shell. Both of them display attributes of any given object passed as the argument. The first function displays all attributes. The second function only displays atttributes that do not begin and end with a double underscore, so as to filter out "dunder" methods a.k.a. "special" methods - like __len__, __str__, __repr__, etc. The first function - oa(o) , where o is some object - does the same as dir(o), but is useful - in IPython - because, dir(o) output will scroll off the screen if the output is long, since it prints the attributes vertically, one per line, while oa(o) prints them horizontally, so has less chance of the output scrolling off, and the output also occupies fewer lines on the screen, so is easier to scan quickly. The second function - oar(o) - is like oa(o), but filters out attribute names that begin and end with a dunder. So it is useful in both IPython and Python.</p> <p>More information and outputs here:</p> <p><a href="https://jugad2.blogspot.in/2017/01/two-simple-python-object-introspection.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/two-simple-python-object-introspection.html</a></p> Tkinter remote debugging (Python) 2017-01-24T20:23:45-08:00Miguel Martínez Lópezhttp://code.activestate.com/recipes/users/4189907/http://code.activestate.com/recipes/580721-tkinter-remote-debugging/ <p style="color: grey"> Python recipe 580721 by <a href="/recipes/users/4189907/">Miguel Martínez López</a> (<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/remote/">remote</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 6. </p> <p>This trick requires rpyc.</p> <p>You can install rpyc typing:</p> <blockquote> <p>pip install rpyc</p> </blockquote> <p>Run the code below and in another interpreter write:</p> <pre class="prettyprint"><code>import rpyc c = rpyc.classic.connect("localhost") c.execute("from Tkinter import Label; label=Label(app, text='a label')") c.execute("label.pack()") app = c.eval("app") app.responsive_button.invoke() </code></pre> RecursionError exception: concise and informative output (Python) 2015-07-05T23:46:59-07:00elazarhttp://code.activestate.com/recipes/users/4187847/http://code.activestate.com/recipes/578660-recursionerror-exception-concise-and-informative-o/ <p style="color: grey"> Python recipe 578660 by <a href="/recipes/users/4187847/">elazar</a> (<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/handler/">handler</a>, <a href="/recipes/tags/recursion/">recursion</a>). Revision 3. </p> <p>Replaces the default exception hook with one that, upon "infinite recursion", removes the last cycle. This results in a significantly cleaner and shorter error message.</p> <p>Usage: simply import &lt;module&gt; as _</p> <p>For more details see the descussion here: <a href="https://mail.python.org/pipermail/python-ideas/2013-September/023190.html" rel="nofollow">https://mail.python.org/pipermail/python-ideas/2013-September/023190.html</a></p> type Checking in Python using decorators (version 2.0) (Python) 2012-11-19T13:15:35-08:00LL Snarkhttp://code.activestate.com/recipes/users/4180463/http://code.activestate.com/recipes/578330-type-checking-in-python-using-decorators-version-2/ <p style="color: grey"> Python recipe 578330 by <a href="/recipes/users/4180463/">LL Snark</a> (<a href="/recipes/tags/checking/">checking</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/type/">type</a>). Revision 2. </p> <p>The two decorators checkparams and checkreturn allow you to check (at execution time) that function parameters or a function return value are the right type.</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>