<commit_before>"""db

Database functions for channelguide.
"""

import os
import re

from sqlhelper.pool import ConnectionPool
from sqlhelper.dbinfo import MySQLDBInfo
from channelguide.cache import client
from channelguide import util
import version
import update

from django.conf import settings

kwargs = { 
        'host': settings.DATABASE_HOST,
        'db': settings.DATABASE_NAME,
        'user': settings.DATABASE_USER, 
        'passwd': settings.DATABASE_PASSWORD
}
if settings.DATABASE_PORT:
    kwargs['port'] = settings.DATABASE_PORT
dbinfo = MySQLDBInfo(**kwargs)
pool = ConnectionPool(dbinfo, settings.MAX_DB_CONNECTIONS)

class CachingConnectionWrapper(object):
    def __init__(self, connection):
        self.connection = connection

    def __getattr__(self, attr):
        if attr in self.__dict__:
            return self.__dict__[attr]
        else:
            return getattr(self.connection, attr)

    @staticmethod
    def get_key(sql, args):
        return 'SQL%s' % hash((sql, args))

    @staticmethod
    def can_cache(sql, args):
        return sql.upper().startswith('SELECT') and 'user' not in sql

    def execute(self, sql, args):
        if self.can_cache(sql, args):
            cached = client.get(self.get_key(sql, args))
            if cached:
                return cached
        ret = self.connection.execute(sql, args)
        if self.can_cache(sql, args):
            client.set(self.get_key(sql, args), ret, time=60)
        return ret


def connect():
    c = pool.connect()
    return CachingConnectionWrapper(c)

def syncdb():
    connection = connect()
    version.initialize_version_table(connection)
    update_dir = os.path.join(os.path.dirname(__file__), 'updates')
    update.run_updates(connection, update_dir)
    connection.close()

def execute_file(path):
    empty_re = re.compile(r'\s*$')
    comment_re = re.compile(r'\s*--')
    for statement in util.read_file(path).split(';'):<commit_msg>make it easy to turn off caching<commit_after>"""db

Database functions for channelguide.
"""

import os
import re

from sqlhelper.pool import ConnectionPool
from sqlhelper.dbinfo import MySQLDBInfo
from channelguide.cache import client
from channelguide import util
import version
import update

from django.conf import settings

kwargs = { 
        'host': settings.DATABASE_HOST,
        'db': settings.DATABASE_NAME,
        'user': settings.DATABASE_USER, 
        'passwd': settings.DATABASE_PASSWORD
}
if settings.DATABASE_PORT:
    kwargs['port'] = settings.DATABASE_PORT
dbinfo = MySQLDBInfo(**kwargs)
pool = ConnectionPool(dbinfo, settings.MAX_DB_CONNECTIONS)

class CachingConnectionWrapper(object):
    def __init__(self, connection):
        self.connection = connection

    def __getattr__(self, attr):
        if attr in self.__dict__:
            return self.__dict__[attr]
        else:
            return getattr(self.connection, attr)

    @staticmethod
    def get_key(sql, args):
        return 'SQL%s' % hash((sql, args))

    @staticmethod
    def can_cache(sql, args):
        return sql.upper().startswith('SELECT') and 'user' not in sql

    def execute(self, sql, args=None):
        if self.can_cache(sql, args):
            cached = client.get(self.get_key(sql, args))
            if cached:
                return cached
        ret = self.connection.execute(sql, args)
        if self.can_cache(sql, args):
            client.set(self.get_key(sql, args), ret, time=60)
        return ret


def connect():
    c = pool.connect()
    return CachingConnectionWrapper(c)
    return c

def syncdb():
    connection = connect()
    version.initialize_version_table(connection)
    update_dir = os.path.join(os.path.dirname(__file__), 'updates')
    update.run_updates(connection, update_dir)
    connection.close()

def execute_file(path):
    empty_re = re.compile(r'\s*$')
    comment_re = re.compile(r'\s*--')
    for statement in util.read_file(path).split(';'):
{'fraction_non_alphanumeric': 0.10919685837344818, 'fraction_numerical': 0.0010134279199391944, 'mean_word_length': 3.1601685985247627, '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': '3682575', 'n_tokens_mistral': 1214, 'n_tokens_neox': 1182, 'n_words': 293}