Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # ----------------------------------------------------------------------------
- # Split a list into a number of sublists, e.g. sublist([1,2,3,4,5,6,7],3) ->
- # [[1,2,3],[4,5,6],[7]]. This probably doesn't need to be a method at all
- # since it's basically just a simple list comprehension but I can remember it
- # easier this way.
- # ----------------------------------------------------------------------------
- def sublist(arg, size):
- i = 0
- # List comprehensions ftw!
- return [arg[i:i+size] for i in range(0, len(arg), size)]
- # ----------------------------------------------------------------------------
- # Columnize a list of strings using evtable. Handles unicode and markup,
- # including ANSI.
- # ----------------------------------------------------------------------------
- def columnize(items, width=80, cols=3):
- # Initialize a new table. No fancy border or headers, we just want a grid
- # of plain text (with any markup intact, of course).
- table = evtable.EvTable(border='none', width=width)
- # Split our list into a number of sublists. The length of each sublist
- # will be equal to the number of desired columns. This allows us to add
- # an entire row at once without needing a second loop to add the columns
- # for each row.
- out = sublist(items, cols)
- # Make sure that we were given enough data to fill at least one row
- # with the desired number of columns. Without this check, the calls to
- # reformat_column may fail if you try to format more columns than you
- # passed data to fill.
- actual_len = len(out[0])
- # Add each row.
- for x in out:
- table.add_row(*x)
- # Calculate the width of each column.
- col_width = width / cols
- # Configure the columns to the desired width.
- for x in range(0, actual_len-1):
- table.reformat_column(x, width=col_width)
- # Return the table.
- return unicode(table)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement