Created
February 29, 2020 10:40
-
-
Save vlad-bezden/d3f16cf7edd187e1bd74f9951f4cf4ce to your computer and use it in GitHub Desktop.
MODULE-SCOPED CODE TO CONFIGURE DEPLOYMENT ENVIRONMENTS.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# dev_main.py | |
TESTING = True | |
import db_connection | |
db = db_connection.Database() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# prod_main.py | |
TESTING = False | |
import db_connection | |
db = db_connection.Database() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: