I'm writing a Python script and I'd like it to behave differently from my CLI than in Production. Here's how I have it currently:
testing = False # True
def run_test():
if testing == True:
print message
else:
alert_department(message)
but I don't like my core functions littered with if/else
statements.
What better patterns should I use?
This particular use looks like you want logging
with different logger configurations based on the environment. Then you can just happily
log = logging.getLogger('some_scope')
# ...
log.warning('Something warning-worthy occurred')
and your configuration determines whether it's printed to the console or whether a custom "alert department" handler is called.
In general, though, I'd recommend against having your script behave (very) differently in dev, testing and production as that makes debugging difficult.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.