Colorize stdout

def colorize(fg=None, bg=None, attr=None, stream=None):
    """Colorize stdout"""
    stdout = stream or sys.stdout
    ansi_codes = []

    if attr:
        ansi_codes.append(color_codes['attr'][attr])
    if fg:
        ansi_codes.append(color_codes['fg'][fg])
    if bg:
        ansi_codes.append(color_codes['bg'][bg])
    stdout.write("\033[%sm" % ';'.join(ansi_codes))

    yield

    stdout.write("\033[0m")