News for hackers. Stuff that matters.
Re:Playing with Strings and Colors in Python by RyanMcGreal on Friday December 28, 2012 @08:13AM (#4977555)
Python is a great language for iterating over collections - lists, tuples, dicts, sets, etc. - and all those elifs don't look very Pythonic to me. Let's clean it up a little:

    colors = ('grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan')
    for char in string:
        print termcolor.colored(char, colors[string.index(char)%7])
Re:Playing with Strings and Colors in Python by absherwin on Friday December 28, 2012 @08:22AM (#4977573)
Better yet use enumerate to save looking up the character index:

    for i,char in enumerate(string):
        print termcolor.colored(char, colors[i%7])
Re:Playing with Strings and Colors in Python by hythloday on Friday December 28, 2012 @11:00AM (#4978121)
You can get rid of that unpleasant indexing:

(3.2)

    from itertools import cycle
    for colour, character in zip(cycle(colours), string):
      print(termcolor.colored(character, colour))
(2.7)

    from itertools import cycle, izip
    for colour, character in izip(cycle(colours, string):
      print termcolor.colored(character, colour)
Re:Playing with Strings and Colors in Python by barakstout on Friday December 28, 2012 @11:13AM (#4978176)
Sweet. Thanks.
Re:Playing with Strings and Colors in Python by barakstout on Friday December 28, 2012 @09:03AM (#4977671)
Both would work just fine. Good Points.
Re:Playing with Strings and Colors in Python by andrewcooke on Friday December 28, 2012 @10:30AM (#4978015)
the first doesn't work correctly for all strings (those with duplicate characters). the second is the right way to do it.
Re:Playing with Strings and Colors in Python by RyanMcGreal on Friday December 28, 2012 @11:00AM (#4978125)
Good point. I should know better than to dash off a quick code sample on HN without first running a test suite. :)
Re:Playing with Strings and Colors in Python by barakstout on Friday December 28, 2012 @10:57AM (#4978108)
Both versions will work just fine. And number mod 7 will give an index of 0 to 6, which covers any color index from the colors tuple. Also, I tested it out just in case.

EDIT: The only issue is that if the string has a repeated letter it will be the same color.

Re:Playing with Strings and Colors in Python by Osmose on Friday December 28, 2012 @12:39PM (#4978543)
I prefer Blessings[1] for terminal colors/positioning/etc (except it's limited to platforms that support curses):

    from blessings import Terminal
    t = Terminal()
    print t.bold, 'Hi there!', t.normal
    print t.bold_red_on_bright_green('It hurts my eyes!')
    with t.location(0, t.height - 1):
        print 'This is at the bottom.'
[1] http://pypi.python.org/pypi/blessings/
Re:Playing with Strings and Colors in Python by ses4j on Friday December 28, 2012 @09:51AM (#4977866)
This is much much much more entry-level than most of the content posted to HN. Maybe a "Beginners: " label would be appropriate...
Re:Playing with Strings and Colors in Python by jevinskie on Friday December 28, 2012 @01:27PM (#4978791)
I would hardly consider myself a python beginner and I was delighted to read this article. I now know how to add some useful "syntax highlighting" to my personal CLI utilities!
Re:Playing with Strings and Colors in Python by barakstout on Friday December 28, 2012 @10:10AM (#4977948)
Maybe it will. Noted for future reference.
Re:Playing with Strings and Colors in Python by mmcnickle on Friday December 28, 2012 @01:13PM (#4978725)
I'm not sure if you were looking for any criticism. I found the way that the way the ATTRIBUTES, HIGHLIGHTS and COLORS dicts are defined to be unnecessarily difficult to read:

    ATTRIBUTES = dict(
        list(zip([
            'bold',
            'dark',
            '',
            'underline',
            'blink',
            '',
            'reverse',
            'concealed'
            ],
            list(range(1, 9))
            ))
        )
    del ATTRIBUTES['']
It may feel like you are being more DRY by not typing the numerical values out. But this is data and it's perfectly fine to explicitly define them. The result is straightforward, more compact and easier to read as a result:

    ATTRIBUTES = {
        'bold': 1,
        'dark': 2,
        'underline': 4,
        'blink': 5,
        'reverse': 7,
        'concealed': 8
    }
Re:Playing with Strings and Colors in Python by viraptor on Friday December 28, 2012 @01:44PM (#4978891)
Or even this if you don't want explicit numbers, but don't want that many zip/list/dict/range/etc.:

    attrs = ['bold', 'dark', '', 'underline', 'blink', '', 'reverse', 'concealed']
    
    ATTRIBUTES = dict((k,v) for v,k in enumerate(attrs, 1) if k)
    # or
    ATTRIBUTES = dict(zip(itertools.count(1), attrs))
Re:Playing with Strings and Colors in Python by Falling3 on Friday December 28, 2012 @01:06PM (#4978681)
The first paragraph read strangely to me... like bot generated content. Like the sentence:

"Python can be written as an algorithm that you can execute."