473,394 Members | 1,781 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

debug print shortcut?

When debugging python, I add print statements such as these.

print 'i=%s' % `i`
print 'foo()=%s' % `foo()`
print 'a,b,c=%s' % `a,b,c`

I just want to dump the value of a variable or expression. But I
don't like having to type the expression twice, everytime. I can
solve this in 'C' with a preprocessor macro. Can it be solved in
python?

John
Jul 18 '05 #1
3 2040
John Mudd wrote:
When debugging python, I add print statements such as these.

print 'i=%s' % `i`
print 'foo()=%s' % `foo()`
print 'a,b,c=%s' % `a,b,c`

I just want to dump the value of a variable or expression. But I
don't like having to type the expression twice, everytime. I can
solve this in 'C' with a preprocessor macro. Can it be solved in
python?

John

I found this:
def dump(s, glob, loc): .... print "%s = %s\n" % (s, eval(s,glob,loc) )
.... a=55
dump("a",globals(),locals()) a = 55

Cheers, Pad.

Jul 18 '05 #2
John Mudd wrote:
When debugging python, I add print statements such as these.

print 'i=%s' % `i`
print 'foo()=%s' % `foo()`
print 'a,b,c=%s' % `a,b,c`

I just want to dump the value of a variable or expression. But I
don't like having to type the expression twice, everytime. I can
solve this in 'C' with a preprocessor macro. Can it be solved in
python?


def printdebug(s):
print '%s = %s' % (s, `eval(s)`)

--
"Codito ergo sum"
Roel Schroeven
Jul 18 '05 #3
Here's one way. You specify the expression as a string, and the value
or the exception raised is printed.

import sys

def debug(expression, depth=1):
frame = sys._getframe(depth)
locals = frame.f_locals
globals = frame.f_globals
try:
value = eval(expression, globals, locals)
except:
value = sys.exc_info()[1]
print >>sys.stderr, "%r -> %r" % (expression, value)
debug("3+3") '3+3' -> 6 debug("1/0") '1/0' -> <exceptions.ZeroDivisionError instance at 0x00B6E248> def f(): .... y = 3
.... debug("y+1")
.... f() 'y+1' -> 4

Another way would be to pass the result of the expression to debug(),
and use inspect.getsource() to locate the line of code in the caller. I
haven't written this, but usage might look like debug(3+3)

debug(3+3) -> 6

With this approach, exceptions would be propagated, not handled in
debug().

Jeff

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFAw5LvJd01MZaTXX0RAuVCAJ9efkj4FJ//mb9oErKGhipcje2s6QCfbuw7
2HdglpNUphcWVm+ELtdT2Hk=
=FCVc
-----END PGP SIGNATURE-----

Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

30
by: Martin Bless | last post by:
Why can't we have an additional 'print' statement, that behaves exactly like 'print' but doesn't insert that damn blank between two arguments? Could be called 'printn' or 'prin1' or 'prinn'...
6
by: Dennis Allen | last post by:
Hi. On one particular web page I want to offer the user a print button. Question. How do I create one? Something compatible with most browsers would be nice. Appreciate any advice...Dennis
2
by: G | last post by:
I am trying to duplicate the printing of a shortcut-print of an access report in my NT Scheduled Tasks. I can now print the report manually very easily by right-clicking on a shortcut to the access...
13
by: cefrancke | last post by:
I have a custom toolbar for users during any preview of a report launched by a button click on a form. I would like to add a toolbar button to launch the Print Dialog (like 'Ctrl-P' or...
5
by: Bob Day | last post by:
VS 2003, vb.net... Is there a way to determine the line number currently executing in your code, so you can log this line number information (e.g. via trace)? Thanks! Bob Day
5
by: MLH | last post by:
I use a fair number of debug.print statements. My apps are fairly well 'peppered' with them. I've often wondered if leaving them in the mdb, creating and rolling out an mde intended for use in the...
6
by: swartzbill2000 | last post by:
Hello, I have a VB 2005 Express project with a TraceListener-derived class to route Debug.Print output to a log file. It works fine for Debug builds. What is the correct combination of changes to...
2
by: Greg P. | last post by:
I've lost my F8 shortcut on my Debug tool bar? I have no idea how or how to reset it. I evern did a repair install off of my dvd and still it didn't come back? I can go into the keyboard short...
2
by: Bill_DBA | last post by:
I have the following stored procedure that is called from the source of a transformation in a DTS package. The first parameter turns on PRINT debug messages. The second, when equals 1, turns on the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.