<commit_before>""" An abstract Table

>>> accounts = TableSymbol('accounts', '{name: string, amount: int}')
>>> deadbeats = accounts['name'][accounts['amount'] < 0]
"""
from __future__ import absolute_import, division, print_function

from datashape import dshape, var, DataShape, Record, isdimension
import datashape
import operator
from toolz import concat, partial, first, pipe, compose
from toolz.curried import filter
from . import scalar
from .core import Expr, Scalar
from .scalar import ScalarSymbol
from .scalar import *
from ..utils import unique
from ..compatibility import _strtypes, builtins


class TableExpr(Expr):
    """ Super class for all Table Expressions """
    @property
    def dshape(self):
        return datashape.var * self.schema

    @property
    def columns(self):
        return self.schema[0].names

    @property
    def dtype(self):
        ds = self.schema[-1]
        if isinstance(ds, Record):
            if len(ds.fields.values()) > 1:
                raise TypeError("`.dtype` not defined for multicolumn object. "
                                "Use `.schema` instead")
            else:
                return dshape(first(ds.fields.values()))
        else:
            return dshape(ds)

    def __getitem__(self, key):
        if isinstance(key, _strtypes):
            if key not in self.columns:
                raise ValueError("Mismatched Column: %s" % str(key))
            return Column(self, key)
        if isinstance(key, list) and all(isinstance(k, _strtypes) for k in key):
            key = tuple(key)
            if not all(col in self.columns for col in key):
                raise ValueError("Mismatched Columns: %s" % str(key))
            return Projection(self, tuple(key))
        if isinstance(key, TableExpr):
            return Selection(self, key)
        raise TypeError("Did not understand input: %s[%s]" % (self, key))

    def sort(self, key=None, ascending=True):
        """ Sort table

        Parameters
        ----------
        key: string, list of strings, TableExpr
            Defines by what you want to sort.  Either:
                A single column string, ``t.sort('amount')``
                A list of column strings, ``t.sort(['name', 'amount'])``
                A Table Expression, ``t.sort(-t['amount'])``
        ascending: bool
            Determines order of the sort
        """
        if key is None:
            key = self.columns[0]
        return Sort(self, key, ascending)

    def head(self, n=10):
        return Head(self, n)

    def relabel(self, labels):
        return ReLabel(self, labels)

    def map(self, func, schema=None):
        return Map(self, func, schema)

    def ancestors(self):
        return NotImplementedError()



class TableSymbol(TableExpr):
    """ A Symbol for Tabular data

    This is a leaf in the expression tree
<commit_msg>ancestors punts on non RowWises<commit_after>""" An abstract Table

>>> accounts = TableSymbol('accounts', '{name: string, amount: int}')
>>> deadbeats = accounts['name'][accounts['amount'] < 0]
"""
from __future__ import absolute_import, division, print_function

from datashape import dshape, var, DataShape, Record, isdimension
import datashape
import operator
from toolz import concat, partial, first, pipe, compose
from toolz.curried import filter
from . import scalar
from .core import Expr, Scalar
from .scalar import ScalarSymbol
from .scalar import *
from ..utils import unique
from ..compatibility import _strtypes, builtins


class TableExpr(Expr):
    """ Super class for all Table Expressions """
    @property
    def dshape(self):
        return datashape.var * self.schema

    @property
    def columns(self):
        return self.schema[0].names

    @property
    def dtype(self):
        ds = self.schema[-1]
        if isinstance(ds, Record):
            if len(ds.fields.values()) > 1:
                raise TypeError("`.dtype` not defined for multicolumn object. "
                                "Use `.schema` instead")
            else:
                return dshape(first(ds.fields.values()))
        else:
            return dshape(ds)

    def __getitem__(self, key):
        if isinstance(key, _strtypes):
            if key not in self.columns:
                raise ValueError("Mismatched Column: %s" % str(key))
            return Column(self, key)
        if isinstance(key, list) and all(isinstance(k, _strtypes) for k in key):
            key = tuple(key)
            if not all(col in self.columns for col in key):
                raise ValueError("Mismatched Columns: %s" % str(key))
            return Projection(self, tuple(key))
        if isinstance(key, TableExpr):
            return Selection(self, key)
        raise TypeError("Did not understand input: %s[%s]" % (self, key))

    def sort(self, key=None, ascending=True):
        """ Sort table

        Parameters
        ----------
        key: string, list of strings, TableExpr
            Defines by what you want to sort.  Either:
                A single column string, ``t.sort('amount')``
                A list of column strings, ``t.sort(['name', 'amount'])``
                A Table Expression, ``t.sort(-t['amount'])``
        ascending: bool
            Determines order of the sort
        """
        if key is None:
            key = self.columns[0]
        return Sort(self, key, ascending)

    def head(self, n=10):
        return Head(self, n)

    def relabel(self, labels):
        return ReLabel(self, labels)

    def map(self, func, schema=None):
        return Map(self, func, schema)

    def ancestors(self):
        return (self,)



class TableSymbol(TableExpr):
    """ A Symbol for Tabular data

    This is a leaf in the expression tree

{'fraction_non_alphanumeric': 0.11043265875785066, 'fraction_numerical': 0.0024424284717376133, 'mean_word_length': 2.304322766570605, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '3993936', 'n_tokens_mistral': 1609, 'n_tokens_neox': 1555, 'n_words': 540}