<commit_before>import getpass
import sqlalchemy
import numpy as np
import pandas as pd
import os
import warnings

from query.helpers import setup_demo_env
from query.html import df_to_html, GETPASS_USE_WARNING, QUERY_DB_ATTR_MSG


# Exceptions
class QueryDbError(Exception):
    pass


class NoPrimaryKeyException(Exception):
    pass


# Helper classes
class QueryDbAttributes(object):
    def _repr_html_(self):
        return QUERY_DB_ATTR_MSG


class QueryDbMeta(sqlalchemy.schema.MetaData):
    pass


# Main classes
class QueryDbOrm(object):
    """
    A helper class for QueryDb -- allows making Sqlalchemy schema
    (Tables and Columns) more accessible via the QueryDb interface
    and exposing additional helper methods, e.g., .last().
    """
    def __init__(self, orm_object, db):
        self._db = db
        if orm_object.__class__ == sqlalchemy.schema.Table:
            self._is_table = True
            self.table = orm_object
            self.column = None

            # Support custom styling of Pandas dataframe by calling .to_html() over _repr_html()
            # incl. not displaying dimensions, for example
            self._column_df = pd.DataFrame(
                [(c.name, c.type, c.primary_key) for c in self.table.columns.values()],
                columns=["Column", "Type", "Primary Key"]
            )
            self._html = df_to_html(self._column_df, ("Column Information for the %s Table"
                                                      % self.table.name))

        else:
            self._is_table = False
            self.table = orm_object.table
            self.column = orm_object
            self._html = ("<em>Inspecting column %s of the %s table. "
                          "Try the .head(), .tail(), and .where() "
                          "methods to further explore.<em>" %
                          (self.column.name, self.table.name))

    def _repr_html_(self):
        return self._html

    def __repr__(self):
        if self._is_table:
            return self.table.__repr__()
        else:
            return self.column.__repr__()

    def _query_helper(self, by=None):
        """
        Internal helper for preparing queries.
        """
        if by is None:
            primary_keys = self.table.primary_key.columns.keys()

            if len(primary_keys) > 1:
                warnings.warn("WARNING: MORE THAN 1 PRIMARY KEY FOR TABLE %s. "
                              "USING THE FIRST KEY %s." %
                              (self.table.name, primary_keys[0]))

            if not primary_keys:
                raise NoPrimaryKeyException("Table %s needs a primary key for"
                                            "the .last() method to work properly. "
                                            "Alternatively, specify an ORDER BY "
                                            "column with the by= argument. " %
                                            self.table.name)
            id_col = primary_keys[0]
        else:
            id_col = by

        if self.column is None:
            col = "*"
        else:
            col = self.column.name

        return col, id_col

    def head(self, n=10, by=None, **kwargs):
        """
        Get the first n entries for a given Table/Column. Additional keywords
        passed to QueryDb.query().

        Requires that the given table has a primary key specified.
        """
        col, id_col = self._query_helper(by=by)

        select = ("SELECT %s FROM %s ORDER BY %s ASC LIMIT %d" %
                  (col, self.table.name, id_col, n))

        return self._db.query(select, **kwargs)

    def tail(self, n=10, by=None, **kwargs):
        """
        Get the last n entries for a given Table/Column. Additional keywords
        passed to QueryDb.query().

        Requires that the given table has a primary key specified.
        """
        col, id_col = self._query_helper(by=by)

        select = ("SELECT %s FROM %s ORDER BY %s DESC LIMIT %d" %
                  (col, self.table.name, id_col, n))

        print "Select is: %s" % select

        return self._db.query(select, **kwargs)

    def first(self, n=10, by=None, **kwargs):
        """
        Alias for .head().
        """

    def last(self, n=10, by=None, **kwargs):
        """
        Alias for .tail().
        """
        return self.tail(n=n, by=by, **kwargs)<commit_msg>Remove extraneous left-over print line.<commit_after>import getpass
import sqlalchemy
import numpy as np
import pandas as pd
import os
import warnings

from query.helpers import setup_demo_env
from query.html import df_to_html, GETPASS_USE_WARNING, QUERY_DB_ATTR_MSG


# Exceptions
class QueryDbError(Exception):
    pass


class NoPrimaryKeyException(Exception):
    pass


# Helper classes
class QueryDbAttributes(object):
    def _repr_html_(self):
        return QUERY_DB_ATTR_MSG


class QueryDbMeta(sqlalchemy.schema.MetaData):
    pass


# Main classes
class QueryDbOrm(object):
    """
    A helper class for QueryDb -- allows making Sqlalchemy schema
    (Tables and Columns) more accessible via the QueryDb interface
    and exposing additional helper methods, e.g., .last().
    """
    def __init__(self, orm_object, db):
        self._db = db
        if orm_object.__class__ == sqlalchemy.schema.Table:
            self._is_table = True
            self.table = orm_object
            self.column = None

            # Support custom styling of Pandas dataframe by calling .to_html() over _repr_html()
            # incl. not displaying dimensions, for example
            self._column_df = pd.DataFrame(
                [(c.name, c.type, c.primary_key) for c in self.table.columns.values()],
                columns=["Column", "Type", "Primary Key"]
            )
            self._html = df_to_html(self._column_df, ("Column Information for the %s Table"
                                                      % self.table.name))

        else:
            self._is_table = False
            self.table = orm_object.table
            self.column = orm_object
            self._html = ("<em>Inspecting column %s of the %s table. "
                          "Try the .head(), .tail(), and .where() "
                          "methods to further explore.<em>" %
                          (self.column.name, self.table.name))

    def _repr_html_(self):
        return self._html

    def __repr__(self):
        if self._is_table:
            return self.table.__repr__()
        else:
            return self.column.__repr__()

    def _query_helper(self, by=None):
        """
        Internal helper for preparing queries.
        """
        if by is None:
            primary_keys = self.table.primary_key.columns.keys()

            if len(primary_keys) > 1:
                warnings.warn("WARNING: MORE THAN 1 PRIMARY KEY FOR TABLE %s. "
                              "USING THE FIRST KEY %s." %
                              (self.table.name, primary_keys[0]))

            if not primary_keys:
                raise NoPrimaryKeyException("Table %s needs a primary key for"
                                            "the .last() method to work properly. "
                                            "Alternatively, specify an ORDER BY "
                                            "column with the by= argument. " %
                                            self.table.name)
            id_col = primary_keys[0]
        else:
            id_col = by

        if self.column is None:
            col = "*"
        else:
            col = self.column.name

        return col, id_col

    def head(self, n=10, by=None, **kwargs):
        """
        Get the first n entries for a given Table/Column. Additional keywords
        passed to QueryDb.query().

        Requires that the given table has a primary key specified.
        """
        col, id_col = self._query_helper(by=by)

        select = ("SELECT %s FROM %s ORDER BY %s ASC LIMIT %d" %
                  (col, self.table.name, id_col, n))

        return self._db.query(select, **kwargs)

    def tail(self, n=10, by=None, **kwargs):
        """
        Get the last n entries for a given Table/Column. Additional keywords
        passed to QueryDb.query().

        Requires that the given table has a primary key specified.
        """
        col, id_col = self._query_helper(by=by)

        select = ("SELECT %s FROM %s ORDER BY %s DESC LIMIT %d" %
                  (col, self.table.name, id_col, n))

        return self._db.query(select, **kwargs)

    def first(self, n=10, by=None, **kwargs):
        """
        Alias for .head().
        """

    def last(self, n=10, by=None, **kwargs):
        """
        Alias for .tail().
        """
        return self.tail(n=n, by=by, **kwargs)
{'fraction_non_alphanumeric': 0.09671907040328093, 'fraction_numerical': 0.002734107997265892, 'mean_word_length': 1.6716372489348752, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1621858', 'n_tokens_mistral': 2469, 'n_tokens_neox': 2358, 'n_words': 736}