I work with Python 3.6. I have connected to Hive using Pyhive and my cursor is cur.
Objective: Build a databases - tables map in a dictionary: dbname1 : list of tables within this db.
I have built it using for loop but I would like to transfer it to a dictionary comprehension please tell me what is wrong with my dict comprehension:
lst = extract_dbs_tbls(cur, s = 'show databases')
map_tbl_db = {}
for i in lst:
cur.execute("use %s" % i)
map_tbl_db['i'] = extract_dbs_tbls(cur, 'show tables')
Note: lst is a list of databases names like ['default', 'dwh', ...]
Basically what I want is the following dict comprehension:
{i:j for i in lst cur.execute('use %i' % i) j = extract_dbs_tbls(cur, s = 'show tables')}
Where extract_dbs_tbls:
def extract_dbs_tbls(cur, s = 'show tables'):
"Return list of dbs / tables. Note: use before 'use db_name'."
cur.execute(s)
lst = cur.fetchall()
return [j[0] for j in lst];