<commit_before>"""

term utils.

>>> c = colored(enabled=True)
>>> print(str(c.red("the quick "), c.blue("brown ", c.bold("fox ")),
              c.magenta(c.underline("jumps over")),
              c.yellow(" the lazy "),
              c.green("dog ")))

"""
import platform

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
OP_SEQ = "\033[%dm"
RESET_SEQ = "\033[0m"
COLOR_SEQ = "\033[1;%dm"
fg = lambda s: COLOR_SEQ % s

SYSTEM = platform.system()
IS_WINDOWS = SYSTEM == "Windows"


class colored(object):

    def __init__(self, *s, **kwargs):
        self.s = s
        self.enabled = kwargs.get("enabled", True)
        if IS_WINDOWS:
            self.enabled = False
        self.op = kwargs.get("op", "")
        self.names = {"black": self.black,
                      "red": self.red,
                      "green": self.green,
                      "yellow": self.yellow,
                      "blue": self.blue,
                      "magenta": self.magenta,
                      "cyan": self.cyan,
                      "white": self.white}

    def _add(self, a, b):
        return str(a) + str(b)

    def _fold_no_color(self, a, b):
        try:
            A = a.no_color()
        except AttributeError:
            A = str(a)
        try:
            B = b.no_color()
        except AttributeError:
            B = str(b)
        return A + B

    def no_color(self):
        return reduce(self._fold_no_color, self.s)

    def __str__(self):
        prefix, suffix = "", ""
        if self.enabled:
            prefix, suffix = self.op, RESET_SEQ
        return prefix + reduce(self._add, self.s) + suffix

    def node(self, s, op):
        return self.__class__(enabled=self.enabled, op=op, *s)

    def black(self, *s):
        return self.node(s, fg(30 + BLACK))

    def red(self, *s):
        return self.node(s, fg(30 + RED))

    def green(self, *s):
        return self.node(s, fg(30 + GREEN))

    def yellow(self, *s):
        return self.node(s, fg(30 + YELLOW))

    def blue(self, *s):
        return self.node(s, fg(30 + BLUE))

    def magenta(self, *s):
        return self.node(s, fg(30 + MAGENTA))

    def cyan(self, *s):
        return self.node(s, fg(30 + CYAN))

    def white(self, *s):
        return self.node(s, fg(30 + WHITE))

    def __repr__(self):
        return repr(self.no_color())
<commit_msg>term.colored breaks with non-str objects.  (django-celery #35)<commit_after>"""

term utils.

>>> c = colored(enabled=True)
>>> print(str(c.red("the quick "), c.blue("brown ", c.bold("fox ")),
              c.magenta(c.underline("jumps over")),
              c.yellow(" the lazy "),
              c.green("dog ")))

"""
import platform

BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE = range(8)
OP_SEQ = "\033[%dm"
RESET_SEQ = "\033[0m"
COLOR_SEQ = "\033[1;%dm"
fg = lambda s: COLOR_SEQ % s

SYSTEM = platform.system()
IS_WINDOWS = SYSTEM == "Windows"


class colored(object):

    def __init__(self, *s, **kwargs):
        self.s = s
        self.enabled = kwargs.get("enabled", True)
        if IS_WINDOWS:
            self.enabled = False
        self.op = kwargs.get("op", "")
        self.names = {"black": self.black,
                      "red": self.red,
                      "green": self.green,
                      "yellow": self.yellow,
                      "blue": self.blue,
                      "magenta": self.magenta,
                      "cyan": self.cyan,
                      "white": self.white}

    def _add(self, a, b):
        return str(a) + str(b)

    def _fold_no_color(self, a, b):
        try:
            A = a.no_color()
        except AttributeError:
            A = str(a)
        try:
            B = b.no_color()
        except AttributeError:
            B = str(b)
        return A + B

    def no_color(self):
        return reduce(self._fold_no_color, self.s)

    def __str__(self):
        prefix, suffix = "", ""
        if self.enabled:
            prefix, suffix = self.op, RESET_SEQ
        return prefix + str(reduce(self._add, self.s)) + suffix

    def node(self, s, op):
        return self.__class__(enabled=self.enabled, op=op, *s)

    def black(self, *s):
        return self.node(s, fg(30 + BLACK))

    def red(self, *s):
        return self.node(s, fg(30 + RED))

    def green(self, *s):
        return self.node(s, fg(30 + GREEN))

    def yellow(self, *s):
        return self.node(s, fg(30 + YELLOW))

    def blue(self, *s):
        return self.node(s, fg(30 + BLUE))

    def magenta(self, *s):
        return self.node(s, fg(30 + MAGENTA))

    def cyan(self, *s):
        return self.node(s, fg(30 + CYAN))

    def white(self, *s):
        return self.node(s, fg(30 + WHITE))

    def __repr__(self):
        return repr(self.no_color())

{'fraction_non_alphanumeric': 0.1532831451275617, 'fraction_numerical': 0.012128816394813885, 'mean_word_length': 1.85381861575179, 'pattern_counts': {'":': 16, '<': 3, '<?xml version=': 0, '>': 15, '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': '6571177', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1611, 'n_words': 390}