<commit_before># coding: utf-8
import sys
if '.' not in sys.path:
	sys.path.insert(0, '.')
import sqlite3


class sqlite3utils (object):
	def __init__ (self,dbpath):
		self.conn = sqlite3.connect(dbpath)
		
	def run_query(self, sql):
		c = self.conn.cursor()
		c.execute(sql)
		res = c.fetchall()
		c.close()
		return res
	
	def get_table_data(self, table):
		c = self.conn.cursor()
		c.execute('SELECT * FROM ' + table)
		data = c.fetchall()
		keys = [name[0] for name in c.description]
		c.close()
		return keys, data
	
	def __get_tables (self):
		c = self.conn.cursor()
		c.execute('SELECT * FROM sqlite_master WHERE type = \'table\'')
		tables = c.fetchall()
		keys = [name[0] for name in c.description]
		c.close()
		return keys, tables

	def __get_views (self):
		c = self.conn.cursor()
		c.execute('SELECT * FROM sqlite_master WHERE type = \'view\'')
		views = c.fetchall()
		keys = [name[0] for name in c.description]
		c.close()
		return keys, views

	def get_all_tables_name(self):
		keys, tables = self.__get_tables()
		return [name[1] for name in tables]

	def get_all_tables(self):
		keys, tables = self.__get_tables()
		retlist = []
		for value in tables:
			retdict = {}
			for (i, key) in enumerate(keys):
				retdict[key] = value[i]
			retlist.append(retdict)
		return retlist

	def get_all_views_name(self):
		keys, views = self.__get_views()
		return [name[1] for name in views]

	def get_all_views(self):
		keys, views = self.__get_views()
		retlist = []
		for value in views:
			retdict = {}
			for (i, key) in enumerate(keys):
				retdict[key] = value[i]
			retlist.append(retdict)
		return retlist

	def get_all_system_tables(self):
		return ['sqlite_master']
	
	def table_info(self, tablename):
		return self.run_query('PRAGMA table_info('+tablename+')')
	
	def index_info(self, tablename):
		return self.run_query('PRAGMA index_info('+tablename+')')
		
	def close_db(self):
		self.conn.close()
		
if __name__ == '__main__':
	a = sqlite3utils(dbpath='../feeds1.db')
	print a.run_query('PRAGMA table_info(sqlite_master)')
<commit_msg>Reuse run_query(), use list & dict comprehensions<commit_after># coding: utf-8

import sys
if '.' not in sys.path:
    sys.path.insert(0, '.')
import sqlite3

class sqlite3utils (object):
    def __init__ (self,dbpath):
        self.conn = sqlite3.connect(dbpath)

    def run_query(self, sql):
        c = self.conn.execute(sql)
        data = c.fetchall()
        keys = [name[0] for name in c.description] if c.description else None
        return keys, data

    def get_table_data(self, table):
        return self.run_query('SELECT * FROM ' + table)

    def __get_tables (self):
        sql = 'SELECT * FROM sqlite_master WHERE type = "table"'
        return self.run_query(sql)

    def __get_views (self):
        sql = 'SELECT * FROM sqlite_master WHERE type = "view"'
        return self.run_query(sql)

    def get_all_tables_name(self):
        keys, tables = self.__get_tables()
        return [name[1] for name in tables]

    def get_all_tables(self):
        keys, tables = self.__get_tables()
        return [{key : value[i] for i, key in enumerate(keys)}
            for value in tables]

    def get_all_views_name(self):
        keys, views = self.__get_views()
        return [name[1] for name in views]

    def get_all_views(self):
        keys, views = self.__get_views()
        return [{key : value[i] for i, key in enumerate(keys)}
            for value in views]

    def get_all_system_tables(self):
        return ['sqlite_master']

    def table_info(self, tablename):
        return self.run_query('PRAGMA table_info('+tablename+')')[1]

    def index_info(self, tablename):
        return self.run_query('PRAGMA index_info('+tablename+')')[1]

    def close_db(self):
        self.conn.close()

if __name__ == '__main__':
    a = sqlite3utils(dbpath='../feeds1.db')
    print a.run_query('PRAGMA table_info(sqlite_master)')[1]

{'fraction_non_alphanumeric': 0.1324655436447167, 'fraction_numerical': 0.006380806533945891, 'mean_word_length': 3.287746170678337, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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}}
{'dir': 'git-commits-cleaned', 'id': '630081', 'n_tokens_mistral': 1471, 'n_tokens_neox': 1361, 'n_words': 347}