samuelcolvin / python-devtools

Dev tools for python
https://python-devtools.helpmanual.io/
MIT License
985 stars 47 forks source link

Option to disable when not developing? #51

Closed smlbiobot closed 4 years ago

smlbiobot commented 4 years ago

I read the docs but I can’t see any options to turn off the module once development has been completed. I find all the features very useful, but now my code has a lot of these debug codes that I would prefer to keep but not output when I’m ready to put them in production. Is there a way? Thanks!

samuelcolvin commented 4 years ago

It's something I've thought about.

The short answer, out of the box is "no". But it would be very easy to implement yourself:

Create a new file where you always import debug from:

import os
__all__ = ('debug',)

if os.getenv('DEBUG', '').upper() == 'TRUE':
    from devtools import debug
else:
    def debug(*args, **kwargs):
        pass

Then set the DEBUG env variable when you want debug() to work, or you could reverse the check to look for some environment variable in production, or you could not install devtools in production and do

try: 
    from devtools import debug
except ImportError: 
    def debug(*args, **kwargs):
        pass

Does that make sense?

samuelcolvin commented 4 years ago

just for completeness, personally I like deleting all debug commands before releasing - one of the advantages of the auto import system (or not including devtools to your requirements file) is that if you do miss one when pushing, CI fails.