Skip to content

Instantly share code, notes, and snippets.

@gcarothers
Created April 29, 2015 22:54
Show Gist options
  • Save gcarothers/758e200f730e1e1efb30 to your computer and use it in GitHub Desktop.
Save gcarothers/758e200f730e1e1efb30 to your computer and use it in GitHub Desktop.
def engine_from_environ(prefix, settings, application_name=None):
"""Setup an SQLAlchemy engine with overrides from environment.
Maps KEYS_LIKE_THIS to keys.like.this"""
env_prefix = prefix.replace('.', '_').upper()
for env in os.environ:
if env.startswith(env_prefix):
key = env.replace('_', '.').lower()
settings[key] = os.environ[env]
if application_name is not None:
sqlalchemy_url = prefix + 'url'
settings[sqlalchemy_url] = add_application_name(
settings[sqlalchemy_url],
application_name,
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment