Popular Python recipes tagged "debugging"http://code.activestate.com/recipes/langs/python/tags/debugging/2017-01-24T20:34:52-08:00ActiveState Code RecipesTesting 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>
Print Hook (Python)
2015-12-04T02:51:42-08:00kauliehttp://code.activestate.com/recipes/users/4193226/http://code.activestate.com/recipes/579132-print-hook/
<p style="color: grey">
Python
recipe 579132
by <a href="/recipes/users/4193226/">kaulie</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>
stupid trick: mimicking the python cgi library's FieldStorage() object for command line debuging (Python)
2015-06-18T20:46:32-07:00Jon Crumphttp://code.activestate.com/recipes/users/4174917/http://code.activestate.com/recipes/579069-stupid-trick-mimicking-the-python-cgi-librarys-fie/
<p style="color: grey">
Python
recipe 579069
by <a href="/recipes/users/4174917/">Jon Crump</a>
(<a href="/recipes/tags/cgi/">cgi</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/urls/">urls</a>).
Revision 6.
</p>
<p>create dictionary-like object that mimics the cgi.FieldStorage() object having both a <code>.value</code> property, and a <code>.getvalue()</code> method</p>
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 <module> 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>
Caller and Callee (Python)
2013-03-07T08:37:32-08:00Deepakhttp://code.activestate.com/recipes/users/4183429/http://code.activestate.com/recipes/578483-caller-and-callee/
<p style="color: grey">
Python
recipe 578483
by <a href="/recipes/users/4183429/">Deepak</a>
(<a href="/recipes/tags/callee/">callee</a>, <a href="/recipes/tags/caller/">caller</a>, <a href="/recipes/tags/debugging/">debugging</a>).
</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 — 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>
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>
Find what class an attribute - ie, myObj.myAttr - comes from, and how it's defined (Python)
2012-10-26T12:59:47-07:00Paul Molodowitchhttp://code.activestate.com/recipes/users/4184064/http://code.activestate.com/recipes/578305-find-what-class-an-attribute-ie-myobjmyattr-comes-/
<p style="color: grey">
Python
recipe 578305
by <a href="/recipes/users/4184064/">Paul Molodowitch</a>
(<a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/inspection/">inspection</a>, <a href="/recipes/tags/source/">source</a>, <a href="/recipes/tags/__dict__/">__dict__</a>, <a href="/recipes/tags/__getattribute__/">__getattribute__</a>, <a href="/recipes/tags/__getattr__/">__getattr__</a>, <a href="/recipes/tags/__slots__/">__slots__</a>).
Revision 3.
</p>
<p>When inspecting new code (or when debugging), it can be handy to know exactly where a given attribute on an object or class comes from.</p>
<p>As a simple example, if you have a class MyClass, you might want to know where MyClass().myMethod is defined.</p>
<p>However, things can get tricky when things like __getattr__, __getattribute__, or even compiled objects come into play. That's where this function comes in. It returns what class a given attribute comes from, and what method was used to define it - ie, '__dict__' ('normal' definitions), '__slots__', '__getattr__', '__getattribute__', '(BUILTIN)'.</p>
<p>(Note - this function should't be relied on to be 100% accurate - rather, it's a best guess, for where to look to find it. It takes some pretty infrequent edge cases for it to be wrong, though...)</p>
Delta Debug - DDMin (Python)
2012-05-25T07:07:04-07:00stevenYANGhttp://code.activestate.com/recipes/users/4182192/http://code.activestate.com/recipes/578144-delta-debug-ddmin/
<p style="color: grey">
Python
recipe 578144
by <a href="/recipes/users/4182192/">stevenYANG</a>
(<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/python/">python</a>).
</p>
<p>Simplifying Failure-Inducing Input.The minimizing delta debugging algorithm ddmin implementation. Using a correct program a_pass and a fail one a_fail to minimize the test set. "testcases/ft.6" is the original test set. The result will be store in a file. </p>
RPDB (RobotPythonDebugger) -- a smarter way to debug robotframework tests (Python)
2012-03-14T15:12:39-07:00Daniel Cohnhttp://code.activestate.com/recipes/users/4172918/http://code.activestate.com/recipes/578073-rpdb-robotpythondebugger-a-smarter-way-to-debug-ro/
<p style="color: grey">
Python
recipe 578073
by <a href="/recipes/users/4172918/">Daniel Cohn</a>
(<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/pdb/">pdb</a>, <a href="/recipes/tags/redirect/">redirect</a>, <a href="/recipes/tags/robot/">robot</a>, <a href="/recipes/tags/rpdb/">rpdb</a>, <a href="/recipes/tags/stdin/">stdin</a>, <a href="/recipes/tags/stdout/">stdout</a>).
</p>
<p>Robotframework (<a href="http://code.google.com/p/robotframework/" rel="nofollow">http://code.google.com/p/robotframework/</a>) is a tool used to run functional tests against a variety of targets. Tests are organized in the form of keyword tsv or html files, which map input parameters to keyword-argument methods in the test suite. Robot includes a fairly advanced logging mechanism, which is cool -- until you try to debug anything. Debugging is made difficult because robot steals stdin and stdout when it is run, which means bye-bye debugging in the terminal. rpdb solves this in a KISS simple way.</p>
objectsignature - debugging utility, will work with containers, classes, nested classes ... almost anything (Python)
2012-02-15T23:05:44-08:00Boris Katshttp://code.activestate.com/recipes/users/4180778/http://code.activestate.com/recipes/578039-objectsignature-debugging-utility-will-work-with-c/
<p style="color: grey">
Python
recipe 578039
by <a href="/recipes/users/4180778/">Boris Kats</a>
(<a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/objectsignature/">objectsignature</a>, <a href="/recipes/tags/objectview/">objectview</a>).
Revision 3.
</p>
<p>Small debug utility will help to detect unwonted changes of user's class during program execution.</p>
Coloured / highlighted version of string (Python)
2014-06-03T17:54:33-07:00Giampaolo Rodolàhttp://code.activestate.com/recipes/users/4178764/http://code.activestate.com/recipes/577818-coloured-highlighted-version-of-string/
<p style="color: grey">
Python
recipe 577818
by <a href="/recipes/users/4178764/">Giampaolo Rodolà</a>
(<a href="/recipes/tags/color/">color</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/highlighting/">highlighting</a>, <a href="/recipes/tags/shell/">shell</a>).
Revision 3.
</p>
<p>Ok, this is really simple but I think it worths a recipe as it's one of those things I always use in shell scripts and for debugging in general.</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>
Simple Regular Expression Tester (Python)
2010-12-25T00:12:44-08:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577517-simple-regular-expression-tester/
<p style="color: grey">
Python
recipe 577517
by <a href="/recipes/users/4174115/">Sunjay Varma</a>
(<a href="/recipes/tags/command/">command</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/line/">line</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/quick/">quick</a>, <a href="/recipes/tags/re/">re</a>, <a href="/recipes/tags/regex/">regex</a>, <a href="/recipes/tags/testing/">testing</a>).
Revision 2.
</p>
<p><strong><em>Is it possible to create a simple command line program that I can use to quickly test my regular expressions?</em></strong></p>
<p>Yes it is. This simple regular expression tester uses Python's re module as well as others to quickly allow you to test your regular expression in one go.</p>
<p>TODO:</p>
<ul>
<li>Add Support For Multiple Regular Expression Input</li>
</ul>
<p>Recent Changes:</p>
<ul>
<li>Made the output prettier with a little more whitespace. More bytes, but at least it's easier to read!</li>
</ul>
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>
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>
A simple function benchmarking module (Python)
2010-08-27T09:26:48-07:00Timothee Cezardhttp://code.activestate.com/recipes/users/4174785/http://code.activestate.com/recipes/577377-a-simple-function-benchmarking-module/
<p style="color: grey">
Python
recipe 577377
by <a href="/recipes/users/4174785/">Timothee Cezard</a>
(<a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/function/">function</a>).
</p>
<p>This module enable its user to monitor the amount of time spend in between two commands start and stop.
The module is fairly imprecise if the monitored task is quick as the start and stop commands are fairly slow (2e-07 - 5e-07 second)</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>
Easy Exception, Error Handling and Debugging with Auto Logging (Python)
2010-04-06T20:40:05-07:00AJ. Mayorgahttp://code.activestate.com/recipes/users/4173476/http://code.activestate.com/recipes/577144-easy-exception-error-handling-and-debugging-with-a/
<p style="color: grey">
Python
recipe 577144
by <a href="/recipes/users/4173476/">AJ. Mayorga</a>
(<a href="/recipes/tags/debug/">debug</a>, <a href="/recipes/tags/debugging/">debugging</a>, <a href="/recipes/tags/error_propagation/">error_propagation</a>, <a href="/recipes/tags/exception/">exception</a>, <a href="/recipes/tags/logging/">logging</a>).
Revision 5.
</p>
<p>Being new to Python and trying to chase where in my project exceptions were taking place always seems to take up a large amount of time. So I created this class that I now pretty use in all my projects to help me save time tracking exceptions and errors. its also has the ability to generate error logs or to output SQL query and data tuples for Database logging. Ive added sample code to give you an idea of how I normally use it. Hope this helps its saved me hours in tracking error/exception. Feed back would be great, mind you havent been Python all that long</p>