This post originated from an RSS feed registered with PHP Buzz
by Jan Schneider.
Original Post: width = str(len(str(len(lines))))
Feed Title: Horde News
Feed URL: http://janschneider.de/horde/jonah/delivery/rss.php?channel_id=25
Feed Description: News around the Horde Project from my personal point of view
The above monstrosity came up today while writing a function to add zero padded line numbers to a chunk of text:
def linenumbers(text):
"Add zero padded line numbers to text"
lines = text.split('\n')
# Find the maximum 'width' of the line count
width = str(len(str(len(lines))))
for i, line in enumerate(lines):
lines[i] = ("%0" + width + "d. %s") % (i + 1, line)
return '\n'.join(lines)