<commit_before>""" Steward extension that adds sqlalchemy """
import json
from sqlalchemy import engine_from_config
import sqlalchemy.ext.declarative
from sqlalchemy.orm import sessionmaker
#pylint: disable=F0401,E0611
from zope.sqlalchemy import ZopeTransactionExtension
#pylint: enable=F0401,E0611


__base__ = sqlalchemy.ext.declarative.declarative_base()

def declarative_base():
    """ Retrieve the global declarative base class """
    return __base__

def _db(request):
    """ Access a sqlalchemy session """
    maker = request.registry.dbmaker
    session = maker()

    def cleanup(request):
        """ Close the session after the request """
        session.close()
    request.add_finished_callback(cleanup)

    return session

def create_schema(registry):
    """
    Create the database schema if needed

    Parameters
    ----------
    registry : dict
        The configuration registry

    Notes
    -----
    The method should only be called after importing all modules containing
    models which extend the ``Base`` object.

    """
    __base__.metadata.create_all(bind=registry.dbmaker.kw['bind'])

def includeme(config):
    """ Configure the app """
    settings = config.get_settings()
    config.add_request_method(_db, name='db', reify=True)
    engine = engine_from_config(settings, prefix='sqlalchemy.')
    config.registry.dbmaker = sessionmaker(bind=engine,
                                           extension=ZopeTransactionExtension())<commit_msg>Pass the db object through to subrequests<commit_after>""" Steward extension that adds sqlalchemy """
import json
from sqlalchemy import engine_from_config
import sqlalchemy.ext.declarative
from sqlalchemy.orm import sessionmaker
#pylint: disable=F0401,E0611
from zope.sqlalchemy import ZopeTransactionExtension
#pylint: enable=F0401,E0611


__base__ = sqlalchemy.ext.declarative.declarative_base()

def declarative_base():
    """ Retrieve the global declarative base class """
    return __base__

def _db(request):
    """ Access a sqlalchemy session """
    maker = request.registry.dbmaker
    session = maker()

    def cleanup(request):
        """ Close the session after the request """
        session.close()
    request.add_finished_callback(cleanup)

    return session

def create_schema(registry):
    """
    Create the database schema if needed

    Parameters
    ----------
    registry : dict
        The configuration registry

    Notes
    -----
    The method should only be called after importing all modules containing
    models which extend the ``Base`` object.

    """
    __base__.metadata.create_all(bind=registry.dbmaker.kw['bind'])

def includeme(config):
    """ Configure the app """
    settings = config.get_settings()
    config.add_request_method(_db, name='db', reify=True)
    engine = engine_from_config(settings, prefix='sqlalchemy.')
    config.registry.dbmaker = sessionmaker(bind=engine,
                                           extension=ZopeTransactionExtension())
    config.registry.subrequest_methods.append('db')
{'fraction_non_alphanumeric': 0.0985592665356909, 'fraction_numerical': 0.010478061558611657, 'mean_word_length': 3.4211287988422576, '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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4416107', 'n_tokens_mistral': 861, 'n_tokens_neox': 818, 'n_words': 224}