<commit_before>from contextlib import contextmanager
from datetime import datetime, time
from decimal import Decimal
from falcon import HTTPConflict, HTTPBadRequest, HTTPNotFound
from sqlalchemy import inspect, schema
from sqlalchemy.exc import IntegrityError, ProgrammingError
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from sqlalchemy.orm.properties import ColumnProperty
from sqlalchemy.sql import sqltypes

from api.resources.base import BaseCollectionResource, BaseSingleResource


@contextmanager
def session_scope(db_engine):
    """
    Provide a scoped db session for a series of operarions.
    The session is created immediately before the scope begins, and is closed
    on scope exit.
    """
    db_session = sessionmaker(bind=db_engine)()
    try:
        yield db_session
        db_session.commit()
    except:
        db_session.rollback()
        raise
    finally:
        db_session.close()


class AlchemyMixin(object):
    """
    Provides serialize and deserialize methods to convert between JSON and SQLAlchemy datatypes.
    """
    def serialize(self, obj, skip_keys=False):
        data = {}
        columns = inspect(obj).mapper.columns
        for key, column in columns.items():
            if skip_keys and (column.primary_key or isinstance(column.type, schema.ForeignKey)):<commit_msg>allow to skip keys in sql alchemy serializer (no refs)<commit_after>from contextlib import contextmanager
from datetime import datetime, time
from decimal import Decimal
from falcon import HTTPConflict, HTTPBadRequest, HTTPNotFound
from sqlalchemy import inspect, schema
from sqlalchemy.exc import IntegrityError, ProgrammingError
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
from sqlalchemy.orm.properties import ColumnProperty
from sqlalchemy.sql import sqltypes

from api.resources.base import BaseCollectionResource, BaseSingleResource


@contextmanager
def session_scope(db_engine):
    """
    Provide a scoped db session for a series of operarions.
    The session is created immediately before the scope begins, and is closed
    on scope exit.
    """
    db_session = sessionmaker(bind=db_engine)()
    try:
        yield db_session
        db_session.commit()
    except:
        db_session.rollback()
        raise
    finally:
        db_session.close()


class AlchemyMixin(object):
    """
    Provides serialize and deserialize methods to convert between JSON and SQLAlchemy datatypes.
    """
    def serialize(self, obj, skip_keys=False):
        data = {}
        columns = inspect(obj).mapper.columns
        for key, column in columns.items():
            if skip_keys and (column.primary_key or len(column.foreign_keys)):
{'fraction_non_alphanumeric': 0.06315034086831718, 'fraction_numerical': 0.0, 'mean_word_length': 3.8402777777777777, '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': '1850476', 'n_tokens_mistral': 754, 'n_tokens_neox': 728, 'n_words': 253}