Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created February 29, 2020 10:40
Show Gist options
  • Save vlad-bezden/d3f16cf7edd187e1bd74f9951f4cf4ce to your computer and use it in GitHub Desktop.
Save vlad-bezden/d3f16cf7edd187e1bd74f9951f4cf4ce to your computer and use it in GitHub Desktop.
MODULE-SCOPED CODE TO CONFIGURE DEPLOYMENT ENVIRONMENTS.
# db_connection.py
from __main__ import TESTING
class TestingDatabase:
def __init__(self):
print("Connecting to DEV database")
class RealDatabase:
def __init__(self):
print("Connecting to PROD database")
Database = [RealDatabase, TestingDatabase][TESTING]
# dev_main.py
TESTING = True
import db_connection
db = db_connection.Database()
# prod_main.py
TESTING = False
import db_connection
db = db_connection.Database()
@vlad-bezden
Copy link
Author

For example, say that I want to run a program in a web server container and give it access to a database. Every time I want to modify my program’s code, I need to run a server container, the database schema must be set up properly, and my program needs the password for access. This is a very high cost if all I’m trying to do is verify that a one-line change to my program works correctly.

The best way to work around such issues is to override parts of a program at startup time to provide different functionality depending on the deployment environment. For example, I could have two different main files—one for production and one for development:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment