Last active
January 28, 2019 18:42
-
-
Save DandikUnited/aed83dc59bc0534c7103726a19a10f7b to your computer and use it in GitHub Desktop.
sqlalchemy_utils postgresql
This file contains 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
import os | |
from sqlalchemy import create_engine | |
DEFAULT_CONN_STR = 'postgresql://{0}:{1}@{2}:5432/{3}?client_encoding=utf8' | |
DB_USER = os.environ.get('DB_USER') | |
DB_PASS = os.environ.get('DB_PASS') | |
DB_HOST = os.environ.get('DB_HOST') | |
DB_DB_NAME = os.environ.get('DB_DB_NAME') | |
def get_connection_string(conn_name): | |
available_connections = {'db': DEFAULT_CONN_STR.format(DB_USER, DB_PASS, | |
DB_HOST, DB_DB_NAME) | |
} | |
try: | |
return available_connections[conn_name] | |
except KeyError: | |
print("Cannot find connection for that string.") | |
def create_db_connection(db_name): | |
conn = get_connection_string(db_name) | |
return create_engine(conn) | |
### convert from list to in clause string | |
def _make_in_clause(array): | |
in_clause = '' | |
for value in array: | |
in_clause += '\'' + value + '\',' | |
in_clause = '(' + in_clause[:-1] + ')' | |
return in_clause | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment