

def iteration(obj, num_keys):
    '\n    Jade iteration supports "for \'value\' [, key]?" iteration only.\n    PyJade has implicitly supported value unpacking instead, without\n    the list indexes. Trying to not break existing code, the following\n    rules are applied:\n\n      1. If the object is a mapping type, return it as-is, and assume\n         the caller has the correct set of keys defined.\n\n      2. If the object\'s values are iterable (and not string-like):\n         a. If the number of keys matches the cardinality of the object\'s\n            values, return the object as-is.\n         b. If the number of keys is one more than the cardinality of\n            values, return a list of [v(0), v(1), ... v(n), index]\n\n      3. Else the object\'s values are not iterable, or are string like:\n         a. if there\'s only one key, return the list\n         b. otherwise return a list of (value,index) tuples\n\n    '
    if is_mapping(obj):
        return obj
    _marker = []
    iter_obj = iter(obj)
    head = next(iter_obj, _marker)
    iter_obj = chain([head], iter_obj)
    if (head is _marker):
        return []
    if is_iterable(head):
        if (num_keys == (get_cardinality(head) + 1)):
            return ((tuple(item) + (ix,)) for (ix, item) in enumerate(iter_obj))
        else:
            return iter_obj
    elif (num_keys == 2):
        return ((item, ix) for (ix, item) in enumerate(iter_obj))
    else:
        return iter_obj

dataset/ETHPy150Open syrusakbary/pyjade/pyjade/runtime.py iteration/original