<jupyter_start><jupyter_text>alchemy_sql_query Execute arbitrary SQL queries against a remote RDBMS supported by python's SQLAlchemy library<jupyter_code>!pip install sqlalchemy==1.4.29 pandas==1.3.5
import glob
import logging
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.sql import text
import pandas as pd
import os
import shutil
import sys
import re
# type of database server (sqlalchemy dialect), e.g. postgresql
db_type = os.environ.get('db_type', 'postgresql')

# hostname of database server
host = os.environ.get('host')

# database name
database = os.environ.get('database')

# db user
user = os.environ.get('user')

# db password
password = <PASSWORD>('password')

# db port
port = int(os.environ.get('port', 5432))

# SQL statement
sql = os.environ.get('sql')

# file containing SQL statements
sql_file = os.environ.get('sql_file')
parameters = list(
    map(lambda s: re.sub('$', '"', s),
        map(
            lambda s: s.replace('=', '="'),
            filter(
                lambda s: s.find('=') > -1 and bool(re.match(r'[A-Za-z0-9_]*=[.\/A-Za-z0-9]*', s)),
                sys.argv
            )
    )))


for parameter in parameters:
    logging.warning('Parameter: ' + parameter)
    exec(parameter)
logging.warning(sql)
engine = create_engine(f'{db_type}://{user}:{password}@{host}:{port}/{database}')
with engine.connect() as con:
    if sql_file is not None:
        with open(sql_file, 'r') as file:
            sql = file.read().replace('\n', '')
    con.execute(text(sql))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1476552598225602, 'fraction_numerical': 0.010139416983523447, 'mean_word_length': 3.671597633136095, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '274551', 'license': 'Apache-2.0', 'n_tokens_mistral': 540, 'n_tokens_neox': 528, 'n_words': 135, 'path': 'component-library/transform/alchemy-sql-query.ipynb', 'repo_name': 'mbalgabayev/claimed', 'size': '1582'}