2

Say I have a file that's only used in pre-production code

I want to ensure it gets not run in production code- any calls out to it have to fail.

This snippet at the top of the file doesn't work - it breaks the Python grammar, which specifies that return must take place in a function.

if not __debug__:
   return None

What is the best solution here - the one that doesn't involve making a gigantic else, that is. :-)

4 Answers 4

7
if not __debug__:
    raise RuntimeError('This module must not be run in production code.')
Sign up to request clarification or add additional context in comments.

2 Comments

The kicker is, I want the module to be "empty" when in production mode. Raising an error is not the right answer here.
Ahh, apparently I misunderstood your question. Then I think the best choice is the gigantic if. I would probably use if __debug__:, put everything you want in there, and then you don't need an else. I realize you want to avoid this sort of thing, however :)
4

Maybe split the non-production code out into a module which is conditionally imported from the main code?

if __debug__:
    import non_production
    non_production.main()

Updated: Based on your comment, you might want to look a 3rd-party library pypreprocessor which lets you do C-style preprocessor directives in Python. They provide a debugging example which seems very close to what you're looking for (ignoring inline debug code without requiring indentation).

Copy/pasted from that url:

from pypreprocessor import pypreprocessor
pypreprocessor.parse()
#define debug

#ifdef debug
print('The source is in debug mode')
#else
print('The source is not in debug mode')
#endif

2 Comments

That's likely to be the solution, but I am hoping to avoid creating two different files for what amounts to an #ifdef.
@Paul Also check out code.google.com/p/pypreprocessor/source/browse/examples/…. It demonstrates how to use command line arguments to manipulate the #defines when you execute the script (Ie, the defines are stored in a python list and can be changed in code). That should eliminate the need to create multiple source files. Also, if you get a feel for it, there's a way to output the post-processed code do a file if, for some reason, you need a production-only version. Note: I'm the author of pypreprocessor so if you need any more info feel free to chime in.
1
import sys

if not __debug__:
    sys.exit()

Documentation for sys.exit.

Comments

1

One way you can do this is to hide all of the stuff in that module in another module that is imported conditionally.

.
├── main.py
├── _test.py
├── test.py

main.py:

import test
print dir(test)

test.py:

if __debug__:
    from _test import *

_test.py:

a = 1
b = 2

Edit:

Just realized your comment in another answer where you said "I am hoping to avoid creating two different files for what amounts to an #ifdef". As shown in another answer, there really isn't any way to do what you want without an if statement.

I've upvoted the answer by samplebias, as I think that answer (plus edit) describes the closest you're going to get without using an if statement.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.