Source

WebHelpers / unfinished / containers.py

"""Functions being considered for webhelpers.containers.

These functions are useful but they're hard to explain and their 
implementation is more cryptic than we'd like.
"""

from webhelpers.containers import distribute

def columnize_as_rows(lis, columns, horizontal=False, fill=None):
    """Like 'zip' but fill any missing elements."""
    data = distribute(lis, columns, horizontal, fill)
    rowcount = len(data)
    length = max(len(x) for x in data)
    for c, lis in enumerate(data):
        n = length - len(lis)
        if n > 0:
            extension = [fill] * n
            lis.extend(extension)
    return zip(*data)

def izip_fill(*iterables, **kw):
    """Like itertools.izip but use a default value for the missing elements
       in short lists rather than stopping at the end of the shortest list.

       ``*iterables`` are the iterables to zip.
       ``default`` is the default value (default ``None``, must be a keyword
       arg.
    """
    iterables = map(iter, iterables)
    default = kw.pop('default', None)
    if kw:
        raise TypeError("unrecognized keyword arguments")
    columns = len(iterables)
    columns_range = range(columns)
    while True:
        found_data = False
        row = [None] * columns
        for i in columns_range:
            try:
                row[i] = iterables[i].next()
                found_data = True
            except StopIteration:
                row[i] = default
        if not found_data:
            break
        yield tuple(row)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.