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. :-)

Paul Nathan
  • 39,638
  • 28
  • 112
  • 212

4 Answers4

7
if not __debug__:
    raise RuntimeError('This module must not be run in production code.')
dappawit
  • 12,182
  • 2
  • 32
  • 26
  • The kicker is, I want the module to be "empty" when in production mode. Raising an error is not the right answer here. – Paul Nathan Mar 08 '11 at 16:54
  • 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 :) – dappawit Mar 08 '11 at 20:29
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
samplebias
  • 37,113
  • 6
  • 107
  • 103
  • That's likely to be the solution, but I am hoping to avoid creating two different files for what amounts to an `#ifdef`. – Paul Nathan Mar 08 '11 at 16:55
  • @Paul Also check out http://code.google.com/p/pypreprocessor/source/browse/examples/debug2production.py. 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. – Evan Plaice Mar 14 '11 at 05:20
1
import sys

if not __debug__:
    sys.exit()

Documentation for sys.exit.

Gary Kerr
  • 13,650
  • 4
  • 48
  • 51
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.

Community
  • 1
  • 1
Mark Hildreth
  • 42,023
  • 11
  • 120
  • 109