<commit_before>import query
import query_language_pb2 as p

###################
# PRETTY PRINTING #
###################

# A note about pretty printing: The result of a pretty-print shouldn't contain
# newlines or tab characters. It may contain spaces.

PRETTY_PRINT_EXPR_WRAPPED = "wrapped"
PRETTY_PRINT_EXPR_UNWRAPPED = "unwrapped"

class PrettyPrinter(object):
    def expr_wrapped(self, expr, backtrace_steps):
        raise NotImplementedError()
    def expr_unwrapped(self, expr, backtrace_steps):
        raise NotImplementedError()
    def write_query(self, query, backtrace_steps):
        raise NotImplementedError()
    def simple_string(self, string, backtrace_steps):
        raise NotImplementedError()

class ReprPrettyPrinter(PrettyPrinter):
    # This implementation has a lot of assertions so that it validates the
    # implementations of `pretty_print()` on the various objects.

    def expr_wrapped(self, expr, backtrace_steps):
        assert isinstance(expr, query.ReadQuery)
        assert isinstance(backtrace_steps, list)
        string, wrapped = expr._inner.pretty_print(self)
        assert "\n" not in string
        if wrapped == PRETTY_PRINT_EXPR_UNWRAPPED:
            string = "expr(%s)" % string
        return string

    def expr_unwrapped(self, expr, backtrace_steps):
        assert isinstance(expr, query.ReadQuery)
        assert isinstance(backtrace_steps, list)
        string = expr._inner.pretty_print(self)[0]
        assert "\n" not in string
        return string

    def write_query(self, wq, backtrace_steps):
        assert isinstance(wq, query.WriteQuery)
        assert isinstance(backtrace_steps, list)
        string = wq._inner.pretty_print(self)
        assert "\n" not in string
        return string

    def meta_query(self, mq, backtrace_steps):
        assert isinstance(mq, query.MetaQuery)
        assert isinstance(backtrace_steps, list)
        string = mq._inner.pretty_print(self)
        assert "\n" not in string
        return string

    def simple_string(self, string, backtrace_steps):
        assert "\n" not in string
        return string

#####################################
# DATABASE AND TABLE ADMINISTRATION #
#####################################

class MetaQueryInner(object):
    def _write_meta_query(self, parent):
        raise NotImplementedError()
    def pretty_print(self, printer):
        raise NotImplementedError()

class DBCreate(MetaQueryInner):
    def __init__(self, db_name):
        assert isinstance(db_name, str)
        self.db_name = db_name
    def _write_meta_query(self, parent):
        parent.type = p.MetaQuery.CREATE_DB
        parent.db_name = self.db_name
    def pretty_print(self, printer):
        return "db_create(%r)" % self.db_name

class DBDrop(MetaQueryInner):
    def __init__(self, db_name):
        assert isinstance(db_name, str)
        self.db_name = db_name
    def _write_meta_query(self, parent):
        parent.type = p.MetaQuery.DROP_DB
        parent.db_name = self.db_name
    def pretty_print(self, printer):
        return "db_drop(%r)" % self.db_name

class DBList(MetaQueryInner):
    def _write_meta_query(self, parent):
        parent.type = p.MetaQuery.LIST_DBS
    def pretty_print(self, printer):
        return "db_list()"

class TableCreate(MetaQueryInner):
    def __init__(self, table_name, db_expr, primary_key, primary_datacenter, cache_size):
        assert isinstance(table_name, str)
        assert isinstance(db_expr, query.Database)
        assert (not primary_key) or isinstance(primary_key, str)
        assert (not primary_datacenter) or isinstance(primary_datacenter, str)
        assert (not cache_size) or isinstance(cache_size, int)
        self.table_name = table_name
        self.db_expr = db_expr
        self.primary_key = primary_key
        self.primary_datacenter = primary_datacenter
        self.cache_size = cache_size
    def _write_meta_query(self, parent):
        parent.type = p.MetaQuery.CREATE_TABLE
        parent.create_table.table_ref.db_name = self.db_expr.db_name
        parent.create_table.table_ref.table_name = self.table_name
        if self.primary_key:
            parent.create_table.primary_key = self.primary_key
        if self.primary_datacenter:
            parent.create_table.datacenter = self.primary_datacenter
        if self.cache_size:
            parent.create_table.cache_size = self.cache_size
    def pretty_print(self, printer):
        return "db(%s).table_create(%r, primary_datacenter=%s, primary_key=%r)" % (
            printer.simple_string(repr(self.db_expr.db_name), ["table_ref", "db_name"]),
            self.table_name,
            printer.simple_string(repr(self.primary_datacenter), ["datacenter"]),
            self.primary_key)

class TableDrop(MetaQueryInner):
    def __init__(self, table_name, db_expr):
        assert isinstance(table_name, str)
        assert isinstance(db_expr, query.Database)
        self.table_name = table_name
        self.db_expr = db_expr
    def _write_meta_query(self, parent):
        parent.type = p.MetaQuery.DROP_TABLE<commit_msg>A little pretty printing python fix<commit_after>import query
import query_language_pb2 as p

###################
# PRETTY PRINTING #
###################

# A note about pretty printing: The result of a pretty-print shouldn't contain
# newlines or tab characters. It may contain spaces.

PRETTY_PRINT_EXPR_WRAPPED = "wrapped"
PRETTY_PRINT_EXPR_UNWRAPPED = "unwrapped"

class PrettyPrinter(object):
    def expr_wrapped(self, expr, backtrace_steps):
        raise NotImplementedError()
    def expr_unwrapped(self, expr, backtrace_steps):
        raise NotImplementedError()
    def write_query(self, query, backtrace_steps):
        raise NotImplementedError()
    def simple_string(self, string, backtrace_steps):
        raise NotImplementedError()

class ReprPrettyPrinter(PrettyPrinter):
    # This implementation has a lot of assertions so that it validates the
    # implementations of `pretty_print()` on the various objects.

    def expr_wrapped(self, expr, backtrace_steps):
        assert isinstance(expr, query.ReadQuery)
        assert isinstance(backtrace_steps, list)
        string, wrapped = expr._inner.pretty_print(self)
        assert "\n" not in string
        if wrapped == PRETTY_PRINT_EXPR_UNWRAPPED:
            string = "expr(%s)" % string
        return string

    def expr_unwrapped(self, expr, backtrace_steps):
        assert isinstance(expr, query.ReadQuery)
        assert isinstance(backtrace_steps, list)
        string = expr._inner.pretty_print(self)[0]
        assert "\n" not in string
        return string

    def write_query(self, wq, backtrace_steps):
        assert isinstance(wq, query.WriteQuery)
        assert isinstance(backtrace_steps, list)
        string = wq._inner.pretty_print(self)
        assert "\n" not in string
        return string

    def meta_query(self, mq, backtrace_steps):
        assert isinstance(mq, query.MetaQuery)
        assert isinstance(backtrace_steps, list)
        string = mq._inner.pretty_print(self)
        assert "\n" not in string
        return string

    def simple_string(self, string, backtrace_steps):
        assert "\n" not in string
        return string

#####################################
# DATABASE AND TABLE ADMINISTRATION #
#####################################

class MetaQueryInner(object):
    def _write_meta_query(self, parent):
        raise NotImplementedError()
    def pretty_print(self, printer):
        raise NotImplementedError()

class DBCreate(MetaQueryInner):
    def __init__(self, db_name):
        assert isinstance(db_name, str)
        self.db_name = db_name
    def _write_meta_query(self, parent):
        parent.type = p.MetaQuery.CREATE_DB
        parent.db_name = self.db_name
    def pretty_print(self, printer):
        return "db_create(%r)" % self.db_name

class DBDrop(MetaQueryInner):
    def __init__(self, db_name):
        assert isinstance(db_name, str)
        self.db_name = db_name
    def _write_meta_query(self, parent):
        parent.type = p.MetaQuery.DROP_DB
        parent.db_name = self.db_name
    def pretty_print(self, printer):
        return "db_drop(%r)" % self.db_name

class DBList(MetaQueryInner):
    def _write_meta_query(self, parent):
        parent.type = p.MetaQuery.LIST_DBS
    def pretty_print(self, printer):
        return "db_list()"

class TableCreate(MetaQueryInner):
    def __init__(self, table_name, db_expr, primary_key, primary_datacenter, cache_size):
        assert isinstance(table_name, str)
        assert isinstance(db_expr, query.Database)
        assert (not primary_key) or isinstance(primary_key, str)
        assert (not primary_datacenter) or isinstance(primary_datacenter, str)
        assert (not cache_size) or isinstance(cache_size, int)
        self.table_name = table_name
        self.db_expr = db_expr
        self.primary_key = primary_key
        self.primary_datacenter = primary_datacenter
        self.cache_size = cache_size
    def _write_meta_query(self, parent):
        parent.type = p.MetaQuery.CREATE_TABLE
        parent.create_table.table_ref.db_name = self.db_expr.db_name
        parent.create_table.table_ref.table_name = self.table_name
        if self.primary_key:
            parent.create_table.primary_key = self.primary_key
        if self.primary_datacenter:
            parent.create_table.datacenter = self.primary_datacenter
        if self.cache_size:
            parent.create_table.cache_size = self.cache_size
    def pretty_print(self, printer):
        return "db(%s).table_create(%r, primary_key=%r, primary_datacenter=%s)" % (
            printer.simple_string(repr(self.db_expr.db_name), ["table_ref", "db_name"]),
            self.table_name,
            self.primary_key,
            printer.simple_string(repr(self.primary_datacenter), ["datacenter"]))

class TableDrop(MetaQueryInner):
    def __init__(self, table_name, db_expr):
        assert isinstance(table_name, str)
        assert isinstance(db_expr, query.Database)
        self.table_name = table_name
        self.db_expr = db_expr
    def _write_meta_query(self, parent):
        parent.type = p.MetaQuery.DROP_TABLE
{'fraction_non_alphanumeric': 0.1049539847268455, 'fraction_numerical': 0.00039161934599569217, 'mean_word_length': 3.2669172932330826, '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': '5919711', 'n_tokens_mistral': 3064, 'n_tokens_neox': 3019, 'n_words': 696}