<commit_before>#!/usr/bin/env python

# This module provides some tools for manipulating the position of the cursor.  
# This is useful for programs that need to update some simple pieces of 
# information (like a clock) but don't require a full-blown ncurses interface.

# This module defines a class which can be used to temporarily suppress stdout 
# and stderr, using the natural 'with-statement' syntax.  Anything written to 
# either file descriptor while it is being muffled can be accessed as a string.

from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import sys
import contextlib
from pprint import pprint

colors = {
    'normal'        : 0,
    'black'         : 30,
    'red'           : 31,
    'green'         : 32,
    'yellow'        : 33,
    'blue'          : 34,
    'magenta'       : 35,
    'cyan'          : 36,
    'white'         : 37 }

styles = {
    'normal'        : 0,
    'bold'          : 1,
    'reverse'       : 2 }


def printf(string, *args, **kwargs):
    print(string.format(*args, **kwargs))

def echo(*args, **kwargs):
    print(args, kwargs or '')

@contextlib.contextmanager
def swap_streams(stdout=None, stderr=None):
    original_stdout = sys.stdout
    original_stderr = sys.stderr

    try:
        if stdout: sys.stdout = stdout
        if stderr: sys.stderr = stderr
        yield

    finally:
        sys.stdout = original_stdout
        sys.stderr = original_stderr

@contextlib.contextmanager
def capture_output(stdout=True, stderr=True, muffle=False):

    class CapturedOutput:

        def __init__(self):
            self.stdout = ""
            self.stderr = ""

        def __contains__(self, phrase):
            return (phrase in self.stdout) or (phrase in self.stderr)

    class CapturedStream:

        def __init__(self, stream, write_callback):
            self.stream = stream
            self.write_callback = write_callback

        def write(self, string):
            if not muffle: self.stream.write(string)
            self.write_callback(string)

        def flush(self):
            self.stream.flush()


    captured_output = CapturedOutput()

    def write_to_stdout(message):
        captured_output.stdout += message

    def write_to_stderr(message):
        captured_output.stderr += message


    with swap_streams(
            stdout=CapturedStream(sys.stdout, write_to_stdout),
            stderr=CapturedStream(sys.stderr, write_to_stderr)):
        yield captured_output

@contextlib.contextmanager
def muffle(stdout=True, stderr=True):<commit_msg>Add helper functions for capturing stdout and stderr.<commit_after>#!/usr/bin/env python

# This module provides some tools for manipulating the position of the cursor.  
# This is useful for programs that need to update some simple pieces of 
# information (like a clock) but don't require a full-blown ncurses interface.

# This module defines a class which can be used to temporarily suppress stdout 
# and stderr, using the natural 'with-statement' syntax.  Anything written to 
# either file descriptor while it is being muffled can be accessed as a string.

from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import sys
import contextlib
from pprint import pprint

colors = {
    'normal'        : 0,
    'black'         : 30,
    'red'           : 31,
    'green'         : 32,
    'yellow'        : 33,
    'blue'          : 34,
    'magenta'       : 35,
    'cyan'          : 36,
    'white'         : 37 }

styles = {
    'normal'        : 0,
    'bold'          : 1,
    'reverse'       : 2 }


def printf(string, *args, **kwargs):
    print(string.format(*args, **kwargs))

def echo(*args, **kwargs):
    print(args, kwargs or '')

@contextlib.contextmanager
def swap_streams(stdout=None, stderr=None):
    original_stdout = sys.stdout
    original_stderr = sys.stderr

    try:
        if stdout: sys.stdout = stdout
        if stderr: sys.stderr = stderr
        yield

    finally:
        sys.stdout = original_stdout
        sys.stderr = original_stderr

@contextlib.contextmanager
def capture_output(stdout=True, stderr=True, muffle=False):

    class CapturedOutput:

        def __init__(self):
            self.stdout = ""
            self.stderr = ""

        def __contains__(self, phrase):
            return (phrase in self.stdout) or (phrase in self.stderr)

    class CapturedStream:

        def __init__(self, stream, write_callback):
            self.stream = stream
            self.write_callback = write_callback

        def write(self, string):
            if not muffle: self.stream.write(string)
            self.write_callback(string)

        def flush(self):
            self.stream.flush()


    captured_output = CapturedOutput()

    def write_to_stdout(message):
        captured_output.stdout += message

    def write_to_stderr(message):
        captured_output.stderr += message


    with swap_streams(
            stdout=CapturedStream(sys.stdout, write_to_stdout),
            stderr=CapturedStream(sys.stderr, write_to_stderr)):
        yield captured_output

@contextlib.contextmanager
def capture_stdout(muffle=False):
    with capture_output(stdout=True, stderr=False, muffle=muffle) as output:
        yield output.stdout

@contextlib.contextmanager
def capture_stderr(muffle=False):
    with capture_output(stdout=False, stderr=True, muffle=muffle) as output:
        yield output.stderr

@contextlib.contextmanager
def muffle(stdout=True, stderr=True):
{'fraction_non_alphanumeric': 0.0878864126527678, 'fraction_numerical': 0.007189072609633357, 'mean_word_length': 2.606610499027868, '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': '865266', 'n_tokens_mistral': 1647, 'n_tokens_neox': 1571, 'n_words': 470}