import functools
import sys
import os

force = 'COLORS' in os.environ

_colors = ['red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white']

_pairs = zip(_colors, range(31, 38))

def _make_color(code, text):
    if force or sys.stdout.isatty():
        return "\033[{}m{}\033[0m".format(code, text)
    else:
        return text

for _color, _num in _pairs:
    locals()[_color] = functools.partial(_make_color, _num)

import sys
import os

force = 'COLORS' in os.environ

def _make_color(code, text):
    if force or sys.stdout.isatty():
        return "\033[{}m{}\033[0m".format(code, text)
    else:
        return text

red     = lambda text: _make_color(31, text)
green   = lambda text: _make_color(32, text)
yellow  = lambda text: _make_color(33, text)
blue    = lambda text: _make_color(34, text)
magenta = lambda text: _make_color(35, text)
cyan    = lambda text: _make_color(36, text)
white   = lambda text: _make_color(37, text)

make colors more explicit