

def group(by_col_names, cur=None, rows=None, col_names=None, to_dict=False):
    "Groups the rows in application-level.\n\n    If `col_names` or `rows` is provided, it will be used first.\n\n    :rtype: row generator\n\n    Assume we have a cursor named ``cur`` has the data:\n\n    ::\n\n        col_names = ['id', 'email']\n        rows = [\n            ('alice', 'alice@gmail.com'),\n            ('mosky', 'mosky.tw@gmail.com'),\n            ('mosky', 'mosky.liu@pinkoi.com')\n        ]\n\n    Group the rows in ``cur`` by id.\n\n    ::\n\n        for row in group(['id'], cur):\n            print row\n\n    The output:\n\n    ::\n\n        ('alice', ['alice@gmail.com'])\n        ('mosky', ['mosky.tw@gmail.com', 'mosky.liu@pinkoi.com'])\n\n    "
    if (col_names is None):
        assert (cur is not None), 'You must specify cur or col_names.'
        col_names = extract_col_names(cur)
    if (rows is None):
        assert (cur is not None), 'You must specify cur or rows.'
        rows = cur
    name_index_map = dict(((name, idx) for (idx, name) in enumerate(col_names)))
    key_indexes = tuple((name_index_map.get(name) for name in by_col_names))
    key_func = (lambda row: tuple((row[i] for i in key_indexes)))
    for (key_values, rows_islice) in groupby(rows, key_func):
        row = [list(col) for col in izip(*rows_islice)]
        for (key_index, key_value) in izip(key_indexes, key_values):
            row[key_index] = key_value
        if to_dict:
            (yield one_to_dict(row=row, col_names=col_names))
        else:
            (yield tuple(row))

dataset/ETHPy150Open moskytw/mosql/mosql/db.py group/original