import sys
'3.4.2 (default, Oct  8 2014, 10:45:20) \n[GCC 4.9.1]'
import os
from sqlalchemy import create_engine
from sqlalchemy.sql import text
host = os.environ['MYSQL_HOST']
user = os.environ['MYSQL_USERNAME']
password = os.environ['MYSQL_PASSWORD']
connection_string = 'mysql+pymysql://{user}:{password}@{host}/?charset=utf8mb4'.format(
engine = create_engine(connection_string, pool_recycle=300)
cur_db = 'enwiki_p'
def use_db(db):
    global cur_db
    if not db.endswith('_p'):
        db = db + '_p'
    cur_db = db
def pre_process_param(param):
    if type(param) == list:
        return tuple(param)
    return param
def sql(query_str, params={}, db=None):
    Run the given SQL query with given 
    def encode_if_necessary(b):
        if type(b) is bytes:
            return b.decode('utf8')
        return b
    if db is None:
        db = cur_db
    if type(db) != list:
        db = [db]
    query = text(query_str)
    con = engine.connect()
    for d in db:
        con.execute('use %s' % d)
        for k in params:
            params[k] = pre_process_param(params[k])
        for r in con.execute(query, **params):
            d = dict(r)
            # This is unfortunately necessary since I can not seem to get sqlalchemy
            # nor pymysql to co-erce all strings to unicode values. This might be
            # because our dbs don't have proper encoding set.
            unicode_d = {k: encode_if_necessary(v) for k, v in d.items()}
            yield unicode_d
!pip install --upgrade pip
!pip install ipython-sql
%load_ext sql
%sql $connection_string
%sql USE enwiki_p;
%sql select * from revision where rev_page = 9906294 ORDER BY rev_timestamp DESC  LIMIT 10;
# install conda
! wget
--2017-03-03 23:10:48--
Resolving (,, 2400:cb00:2048:1::6810:130a, ...
Connecting to (||:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 34663461 (33M) [application/x-sh]
Saving to: ‘’

Miniconda3-latest-L 100%[=====================>]  33.06M   101MB/s   in 0.3s   

2017-03-03 23:10:54 (101 MB/s) - ‘’ saved [34663461/34663461]

Welcome to Miniconda3 4.3.11 (by Continuum Analytics, Inc.)

In order to continue the installation process, please review the license
Please, press ENTER to continue
>>> ^C