def echo(arg=None, sep=' ', end='\n'):
    '''
        "arg" is a string or None
        if "arg" is None : the terminal is reset to his default values.
        if "arg" is a string it must contain "sep" separated values.
        if args are found in globals "attrs" or "colors", or start with "@" \
    they are interpreted as ANSI commands else they are output as text.
        colors, if any, must be first (foreground first then background)
        you can not specify a background color alone ; \
    if you specify only one color, it will be the foreground one.
        @* commands handle the screen and the cursor :
            @x;y : go to xy
            @    : go to 1;1
            @@   : clear screen and go to 1;1

        examples:
    echo('red')                  : set red as the foreground color
    echo('red blue')             : red on blue
    echo('red blink')            : blinking red
    echo()                       : restore terminal default values
    echo('reverse')              : swap default colors
    echo('cyan blue reverse')    : blue on cyan <=> echo('blue cyan)
    echo('red reverse')          : a way to set up the background only
    echo('red reverse blink')    : you can specify any combinaison of \
            attributes in any order with or without colors
    echo('blink Python')         : output a blinking 'Python'
    echo('@@ hello')             : clear the screen and print 'hello' at 1;1
    '''

    cmd, txt = [reset], []
    if arg:
        arglist=arg.split(sep)
        for offset in (fgoffset, bgoffset):
            if arglist and arglist[0] in colors:
                cmd.append(format % (colors[arglist.pop(0)]+offset))
        for a in arglist:
            c=format % attrs[a] if a in attrs else None
            if c and c not in cmd:
                cmd.append(c)
            else:
                if a.startswith('@'):
                    a=a[1:]
                    if a=='@':
                        cmd.append('2J')
                        cmd.append('H')
                    else:
                        cmd.append('%sH'%a)
                else:
                    txt.append(a)
    if txt and end: txt[-1]+=end
    stdout.write(esc.join(cmd)+sep.join(txt))

        "arg" is a string or None
        if "arg" is None : the terminal is reset to his default values.
        if "arg" is a string it must contain "sep" separated values.
        if args are found in globals "attrs" or "colors", or start with "@"     they are interpreted as ANSI commands else they are output as text.
        colors, if any, must be first (foreground first then background)
        you can not specify a background color alone ;     if you specify only one color, it will be the foreground one.
        @* commands handle the screen and the cursor :
            @x;y : go to xy
            @    : go to 1;1
            @@   : clear screen and go to 1;1

        examples:
    echo('red')                  : set red as the foreground color
    echo('red blue')             : red on blue
    echo('red blink')            : blinking red
    echo()                       : restore terminal default values
    echo('reverse')              : swap default colors
    echo('cyan blue reverse')    : blue on cyan <=> echo('blue cyan)
    echo('red reverse')          : a way to set up the background only
    echo('red reverse blink')    : you can specify any combinaison of             attributes in any order with or without colors
    echo('blink Python')         : output a blinking 'Python'
    echo('@@ hello')             : clear the screen and print 'hello' at 1;1
    
recipes/Python/574451_Handling_ANSI_Terminal/recipe-574451.py