<commit_before>__all__ = ["uprint", "print_table"]

import itertools


def uprint(string):
    print unicode(string).encode("utf-8")


def to_str(obj):
    if obj is None:
        return ""
    return str(obj)


def align(string, length, orientation="left"):
    extra_space = length - len(string)
    if extra_space < 0:
        extra_space = 0
    if orientation == "middle":
        side_space = " " * ((extra_space) / 2)
        return side_space + string + side_space + " " * (extra_space % 2)
    if orientation == "left":
        return string + extra_space * " "
    if orientation == "right":
        return " " * extra_space + string


lines = {"v": unichr(0x2502),
         "+": unichr(0x253c),
         "h": unichr(0x2500)
         }


def print_column_names(column_names):
    if len(column_names) < 1:
        raise Exception("too few column_names")
    lengths = []
    header_strings = []
    dividers = []
    for header, length in column_names:
        if length < len(header):
            length = len(header)
        lengths.append(length)
        header_strings.append(align(header, length, "middle"))
        dividers.append(lines["h"] * length)
    uprint(lines["v"].join(header_strings))
    uprint(lines["+"].join(dividers))
    return lengths


def print_line(values, lengths):
    if len(values) != len(lengths):
        raise Exception("too few values")
    strings = [align(to_str(value)[:length], length, "left") for value, length in itertools.izip(values, lengths)]
    return lines["v"].join(strings)


def print_table(column_names, data):
    lengths = print_column_names(column_names)
    for values in data:
        uprint(print_line(values, lengths))<commit_msg>changed the print table function<commit_after># -*- coding: utf-8 -*-
__all__ = ["uprint", "print_table", ]


def uprint(string):
    print unicode(string).encode("utf-8")


def to_str(obj):
    if obj is None:
        return ""
    return str(obj)


lines = {"v": unichr(0x2502),
         "+": unichr(0x253c),
         "h": unichr(0x2500)
         }


def print_table(columns, data):
    table = Table(columns)
    table.print_multiple_rows(data)


class Table(object):
    def __init__(self, columns):
        self._column_count = len(columns)
        header = []
        divider = []
        row_format = []
        for name, width in columns:
            if width < len(name):
                width = len(name)
            header.append(("{:%s%d}" % ("^", width)).format(name))
            divider.append(lines["h"] * width)
            row_format.append("{:%s%d}" % ("<", width))
        uprint(lines["v"].join(header))
        uprint(lines["+"].join(divider))
        self._row_format = lines["v"].join(row_format)

    def print_row(self, data):
        assert(len(data) == self._column_count)
        uprint(self._row_format.format(*data))

    def print_multiple_rows(self, data_list):
        for data in data_list:
            self.print_row(data)
{'fraction_non_alphanumeric': 0.14546684709066307, 'fraction_numerical': 0.012178619756427604, 'mean_word_length': 2.7430379746835443, 'pattern_counts': {'":': 9, '<': 8, '<?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': '4170008', 'n_tokens_mistral': 983, 'n_tokens_neox': 955, 'n_words': 203}