<commit_before>from .database import BaseDbManager
from collections import Iterable
from pandas import read_sql
from . import models
import sqlalchemy


class QueryManager(BaseDbManager):
    """Query interface to database."""

    def _limit_and_df(self, query, limit, as_df=False):
        """adds a limit (limit==None := no limit) to any query and allow a return as pandas.DataFrame

        :param bool as_df: if is set to True results return as pandas.DataFrame
        :param `sqlalchemy.orm.query.Query` query: SQL Alchemy query
        :param int,tuple limit: maximum number of results
        :return: query result of pyhgnc.manager.models.XY objects
        """
        if limit:

            if isinstance(limit, int):
                query = query.limit(limit)

            if isinstance(limit, Iterable) and len(limit) == 2 and [int, int] == [type(x) for x in limit]:
                page, page_size = limit
                query = query.limit(page_size)
                query = query.offset(page * page_size)

        if as_df:
            results = read_sql(query.statement, self.engine)

        else:
            try:
                results = query.all()
            except sqlalchemy.exec.Statement.Error:
                query.session.rollback()
                results = query.all()

        return results
<commit_msg>generalized exception in query<commit_after>from .database import BaseDbManager
from collections import Iterable
from pandas import read_sql
from . import models
import sqlalchemy


class QueryManager(BaseDbManager):
    """Query interface to database."""

    def _limit_and_df(self, query, limit, as_df=False):
        """adds a limit (limit==None := no limit) to any query and allow a return as pandas.DataFrame

        :param bool as_df: if is set to True results return as pandas.DataFrame
        :param `sqlalchemy.orm.query.Query` query: SQL Alchemy query
        :param int,tuple limit: maximum number of results
        :return: query result of pyhgnc.manager.models.XY objects
        """
        if limit:

            if isinstance(limit, int):
                query = query.limit(limit)

            if isinstance(limit, Iterable) and len(limit) == 2 and [int, int] == [type(x) for x in limit]:
                page, page_size = limit
                query = query.limit(page_size)
                query = query.offset(page * page_size)

        if as_df:
            results = read_sql(query.statement, self.engine)

        else:
            try:
                results = query.all()
            except:
                query.session.rollback()
                results = query.all()

        return results

{'fraction_non_alphanumeric': 0.08517823639774859, 'fraction_numerical': 0.00075046904315197, 'mean_word_length': 2.143867924528302, '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': '6409530', 'n_tokens_mistral': 739, 'n_tokens_neox': 693, 'n_words': 240}