473,624 Members | 2,288 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

debugging code

If I have some debugging code in a Python program (mostly print
statements) and I want to turn it "off", two obvious methods are to
(1) comment out the code
(2) set a logical 'debug' variable at the beginning and use it to
conditionally run the debugging code.

Is there a better way? Some other languages have conditional
compilation. (I don't know if that is better or worse). What is the
"best" way to maintain "production " and "debugging" versions of a
Python program at the same time, preferably in the same file?
Jul 18 '05 #1
8 1785
be*******@aol.c om wrote:
If I have some debugging code in a Python program (mostly print
statements) and I want to turn it "off", two obvious methods are to
(1) comment out the code
(2) set a logical 'debug' variable at the beginning and use it to
conditionally run the debugging code.

Is there a better way? Some other languages have conditional
compilation. (I don't know if that is better or worse). What is the
"best" way to maintain "production " and "debugging" versions of a
Python program at the same time, preferably in the same file?


Since 2.3, python features a log4j-like logging api - look in the modules
doc for 'logging'. It uses different levels of logging that can be set so
that the output gets suppressed if the level is below the configured one.

A preprocessor variant doesn't exist.

--
Regards,

Diez B. Roggisch
Jul 18 '05 #2
Since Python isn't "compiled", there is no such thing as
conditional compilation. I always have _debug and _trace
variables and use simple if _debug: and if _trace: statements
to test them (actually I allow debug to have levels that
show additional detail as I need it). I almost always read
these from the argument list on the processor call so I can
turn them on easily. I also read them from .INI file for
those programs that have one. Seems to work very well and
there is almost no overhead to them when turned off.

FYI,
Larry Bates
Syscon, Inc.

<be*******@aol. com> wrote in message
news:30******** *************** **@posting.goog le.com...
If I have some debugging code in a Python program (mostly print
statements) and I want to turn it "off", two obvious methods are to
(1) comment out the code
(2) set a logical 'debug' variable at the beginning and use it to
conditionally run the debugging code.

Is there a better way? Some other languages have conditional
compilation. (I don't know if that is better or worse). What is the
"best" way to maintain "production " and "debugging" versions of a
Python program at the same time, preferably in the same file?

Jul 18 '05 #3

Python defines __debug__ to True by default, and False when
optimizations are enabled...meani ng you can enable/disable code without
having to define/undefine vars ahead of time and without having to
change it prior to deployment. This is how the "assert" statement
works. You can only set __debug__ through the use of -O or -OO.

For example, you can define a func like this:

def printDebugStmt( stmt ) :
if __debug__ : print stmt
....and use it like this:

$ python
i=3
printDebugStmt( "Hello World...i = %s" % i ) Hello World...i = 3
$ python -O i=3
printDebugStmt( "Hello World...i = %s" % i )

....just remember to "ship" your app so it runs Python with -O or -OO.
BTW: you can use freeze.py with -O or -OO to get the same result for a
"frozen" app.

-Rick
be*******@aol.c om wrote: If I have some debugging code in a Python program (mostly print
statements) and I want to turn it "off", two obvious methods are to
(1) comment out the code
(2) set a logical 'debug' variable at the beginning and use it to
conditionally run the debugging code.

Is there a better way? Some other languages have conditional
compilation. (I don't know if that is better or worse). What is the
"best" way to maintain "production " and "debugging" versions of a
Python program at the same time, preferably in the same file?

Jul 18 '05 #4
"Rick Ratzel" <ri*********@ma gma-da.com> wrote in message
news:40******** *************** @news.twtelecom .net...

Python defines __debug__ to True by default, and False when
optimizations are enabled...meani ng you can enable/disable code without
having to define/undefine vars ahead of time and without having to
change it prior to deployment. This is how the "assert" statement
works.
I agree with Rick on using __debug__ not your own variables, as suggested
elsewhere
You can only set __debug__ through the use of -O or -OO.


There is a typo here (but all the examples that followed were correct), You
can only set __debug__ *to False* through the use of -O or -OO.

Jul 18 '05 #5
Rick Ratzel <ri*********@ma gma-da.com> wrote in message news:<40******* *************** *@news.twteleco m.net>...
Python defines __debug__ to True by default, and False when
optimizations are enabled...meani ng you can enable/disable code without
having to define/undefine vars ahead of time and without having to
change it prior to deployment. This is how the "assert" statement
works. You can only set __debug__ through the use of -O or -OO.


<SNIP>

Thanks -- I will take your suggestion. Where are the Python command
line options like -O and -OO documented? Typing 'python -h' just gives
me

-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE= x)
-OO : remove doc-strings in addition to the -O optimizations
Jul 18 '05 #6
be*******@aol.c om wrote:

<SNIP>

Thanks -- I will take your suggestion. Where are the Python command
line options like -O and -OO documented? Typing 'python -h' just gives
me

-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE= x)
-OO : remove doc-strings in addition to the -O optimizations


Thats strange. Here they are for Python 2.3.3 as built on my system
(should be the same for you):

[rlratzel@gt6 ~] python -h
usage: python [option] ... [-c cmd | file | -] [arg] ...
Options and arguments (and corresponding environment variables):
-c cmd : program passed in as string (terminates option list)
-d : debug output from parser (also PYTHONDEBUG=x)
-E : ignore environment variables (such as PYTHONPATH)
-h : print this help message and exit
-i : inspect interactively after running script, (also PYTHONINSPECT=x )
and force prompts, even if stdin does not appear to be a terminal
-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE= x)
-OO : remove doc-strings in addition to the -O optimizations
-Q arg : division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew
-S : don't imply 'import site' on initialization
-t : issue warnings about inconsistent tab usage (-tt: issue errors)
-u : unbuffered binary stdout and stderr (also PYTHONUNBUFFERE D=x)
see man page for details on internal buffering relating to '-u'
-v : verbose (trace import statements) (also PYTHONVERBOSE=x )
-V : print the Python version number and exit
-W arg : warning control (arg is action:message: category:module :lineno)
-x : skip first line of source, allowing use of non-Unix forms of #!cmd
file : program read from script file
- : program read from stdin (default; interactive mode if a tty)
arg ...: arguments passed to program in sys.argv[1:]
Other environment variables:
PYTHONSTARTUP: file executed on interactive startup (no default)
PYTHONPATH : ':'-separated list of directories prefixed to the
default module search path. The result is sys.path.
PYTHONHOME : alternate <prefix> directory (or <prefix>:<exec_ prefix>).
The default module search path uses <prefix>/pythonX.X.
PYTHONCASEOK : ignore case in 'import' statements (Windows).
Jul 18 '05 #7
Rick Ratzel <ri*********@ma gma-da.com> wrote in message news:<40******* *************** @news.twtelecom .net>...
be*******@aol.c om wrote:

<SNIP>

Thanks -- I will take your suggestion. Where are the Python command
line options like -O and -OO documented? Typing 'python -h' just gives
me

-O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE= x)
-OO : remove doc-strings in addition to the -O optimizations


Thats strange. Here they are for Python 2.3.3 as built on my system
(should be the same for you):


<SNIP>

I get the same output from 'python -h'. What I meant to say is that I
am looking for more detailed documentation of the Python interpreter
options. I looked briefly on python.org and did not find it.
Jul 18 '05 #8
be*******@aol.c om writes:
Rick Ratzel <ri*********@ma gma-da.com> wrote in message news:<40******* *************** @news.twtelecom .net>...
be*******@aol.c om wrote:
>
> <SNIP>
>
> Thanks -- I will take your suggestion. Where are the Python command
> line options like -O and -OO documented? Typing 'python -h' just gives
> me
>
> -O : optimize generated bytecode (a tad; also PYTHONOPTIMIZE= x)
> -OO : remove doc-strings in addition to the -O optimizations


Thats strange. Here they are for Python 2.3.3 as built on my system
(should be the same for you):


<SNIP>

I get the same output from 'python -h'. What I meant to say is that I
am looking for more detailed documentation of the Python interpreter
options. I looked briefly on python.org and did not find it.


http://www.python.org/doc/current/re...t.html#l2h-460

Thomas
Jul 18 '05 #9

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

Similar topics

6
25196
by: Dmitri Shvetsov | last post by:
Hi All, Did somebody see the situation when the VS refuses to debug the Web Service at all? I can't catch why, the initially created Web Service can be debugged very easy but after some changes in a source code, maybe the source code becomes bigger that some hidden threshold, the debugger can't enter into this code anymore. I can use this web service, all methods but can't see in debugger what's going on. I have already catched this...
0
3221
by: ZMan | last post by:
Scenario: This is about debugging server side scripts that make calls to middle-tier business DLLs. The server side scripts are legacy ASP 3.0 pages, and the DLLs are managed DLLs converted/developed with VB.NET. What I want from debugging is to be able to step into the methods in the DLLs called from ASP scripts using Visual Studio .NET. Background: For typical script debugging issues, you can read and follow the two documents on...
5
2942
by: Velvet | last post by:
Can someone tell me to what process I need to attach to be able to step through my classic ASP code in VS.net 2003. I'm working on an XP box with IIS installed. I also have VS.net 2005 (The final, never installed beta) installed on this box if it makes a difference (I did not install VS Development Web Server as I'm already using the XP web server). I've seen that I need to attach to the native IIS engine, but I don't know what it's...
5
3623
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As New System.Diagnostics.Process 'p.Start(MDEPDirStr & "macrun.exe", sPPTOut) p.Start("C:\WINDOWS\SYSTEM32\CALC.EXE") 'p.Start("C:\WINDOWS\SYSTEM32\macrun.exe", sPPTOut)
8
2209
by: razael1 | last post by:
I am putting debugging messages into my program by putting blocks that look like this: #ifdef DEBUG errlog << "Here is some information"; #endif All these #ifdef blocks make the code bulky and harder to read, so I'd like to do something where I put: errMsg("Here is some information");
5
7791
by: phnimx | last post by:
Hi , We have developed a number of plug-in .NET Library Components that we typically deploy with our various applications by installing them into the GAC. Each of the applications contains an app.config file referencing arbitrary versions of the plug-in components they wish to consume. Here's the problem: Assuming I have installed any one of our application software,
5
2798
by: rn5a | last post by:
Can someone please suggest me a text editor especially for DEBUGGING ASP scripts apart from Microsoft Visual Interdev? I tried using Visual Interdev & created a project but Interdev generates some error related to FrontPage extensions. I couldn't exactly understand the error. I tried to create the project in C: \Inetpub\wwwroot. If I just open a ASP file (by navigating to the File-->Open File... menu), then Interdev doesn't give the...
0
7323
jwwicks
by: jwwicks | last post by:
Introduction This tutorial describes how to use Visual Studio to create a new C++ program, compile/run a program, resume work on an existing program and debug a program. It is aimed at the beginning CIS student who is struggling to get their programs working. I work in the computer lab at the college I'm attending and I see many students who don't know how to use the IDE for best results. Visual Studio automatically creates a number of...
2
20835
jwwicks
by: jwwicks | last post by:
C/C++ Programs and Debugging in Linux This tutorial will give you a basic idea how to debug a program in Linux using GDB. As you are aware Visual Studio doesn’t run on Linux so you have to use some of the tools provided on the command-line. If you hate the command line tools, get over it since you’re bound to be using them at some point in your career. All commands in Linux ARE case sensitive so capital letters are different from lowercase...
33
2872
by: fmassei | last post by:
Hello! I made a short piece of code that I find very useful for debugging, and I wanted to ask you if it is correct, somehow acceptable or if I simply reinvented the wheel. To deal with some bad bugs caused by memory leaks I ended up with this simple solution: I made one header file that, when included, replaces the malloc/calloc/realloc/free functions with some other functions that do the actual job and insert (or remove) the pointers to...
0
8240
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8175
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8625
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7168
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1791
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.