<commit_before>from contextlib import contextmanager
from concurrent.futures import ThreadPoolExecutor

from sqlalchemy import create_engine, event
from sqlalchemy.orm import sessionmaker


async_pool = ThreadPoolExecutor(max_workers=5)


class MissingFactoryError(Exception):
    pass


def make_session_factory(database_url, pool_size, engine_events):
    engine = create_engine(database_url, pool_size=pool_size)

    for (name, listener) in engine_events:
        event.listen(engine, name, listener)

    factory = sessionmaker()
    factory.configure(bind=engine)

    return factory


def wrap_in_future(query):
    """Wrap a `sqlalchemy.orm.query.Query` object into a
    `concurrent.futures.Future` so that it can be yielded.

    :param query: `sqlalchemy.orm.query.Query` object
    :returns: `concurrent.futures.Future` object wrapping the given query so
    that tornado can yield on it.
    """
    return async_pool.submit(query)


class SessionMixin(object):
    @contextmanager
    def make_session(self):
        if not hasattr(self.application, 'session_factory'):
            raise MissingFactoryError()

        try:
            session = self.application.session_factory()

            yield session
        except:
            session.rollback()
        else:
            session.commit()
        finally:
            session.close()<commit_msg>add declarative base<commit_after>from contextlib import contextmanager
from concurrent.futures import ThreadPoolExecutor

from sqlalchemy import create_engine, event
from sqlalchemy.ext.declarative import declarative_base as _declarative_base
from sqlalchemy.orm import sessionmaker


async_pool = ThreadPoolExecutor(max_workers=5)


class MissingFactoryError(Exception):
    pass


def make_session_factory(database_url, pool_size, engine_events):
    engine = create_engine(database_url, pool_size=pool_size)

    for (name, listener) in engine_events:
        event.listen(engine, name, listener)

    factory = sessionmaker()
    factory.configure(bind=engine)

    return factory


def wrap_in_future(query):
    """Wrap a `sqlalchemy.orm.query.Query` object into a
    `concurrent.futures.Future` so that it can be yielded.

    :param query: `sqlalchemy.orm.query.Query` object
    :returns: `concurrent.futures.Future` object wrapping the given query so
    that tornado can yield on it.
    """
    return async_pool.submit(query)


class SessionMixin(object):
    @contextmanager
    def make_session(self):
        if not hasattr(self.application, 'session_factory'):
            raise MissingFactoryError()

        try:
            session = self.application.session_factory()

            yield session
        except:
            session.rollback()
        else:
            session.commit()
        finally:
            session.close()


def declarative_base():
    if not declarative_base._instance:
        declarative_base._instance = _declarative_base()
    return declarative_base._instance
{'fraction_non_alphanumeric': 0.07718120805369127, 'fraction_numerical': 0.0006711409395973154, 'mean_word_length': 3.332848837209302, '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': '1759948', 'n_tokens_mistral': 851, 'n_tokens_neox': 809, 'n_words': 214}