def _unpack_args(args, nargs_spec):
    """Given an iterable of arguments and an iterable of nargs specifications,
    it returns a tuple with all the unpacked arguments at the first index
    and all remaining arguments as the second.

    The nargs specification is the number of arguments that should be consumed
    or `-1` to indicate that this position should eat up all the remainders.

    Missing items are filled with `None`.
    """
    args = deque(args)
    nargs_spec = deque(nargs_spec)
    rv = []
    spos = None

    def _fetch(c):
        try:
            if spos is None:
                return c.popleft()
            else:
                return c.pop()
        except IndexError:
            return None

    while nargs_spec:
        nargs = _fetch(nargs_spec)
        if nargs == 1:
            rv.append(_fetch(args))
        elif nargs > 1:
            x = [_fetch(args) for _ in range(nargs)]
            # If we're reversed, we're pulling in the arguments in reverse,
            # so we need to turn them around.
            if spos is not None:
                x.reverse()
            rv.append(tuple(x))
        elif nargs < 0:
            if spos is not None:
                raise TypeError('Cannot have two nargs < 0')
            spos = len(rv)
            rv.append(None)

    # spos is the position of the wildcard (star).  If it's not `None`,
    # we fill it with the remainder.
    if spos is not None:
        rv[spos] = tuple(args)
        args = []
        rv[spos + 1:] = reversed(rv[spos + 1:])

    return tuple(rv), list(args)
def _unpack_args ( args , nargs_spec ) : args = deque ( args ) nargs_spec = deque ( nargs_spec ) rv = [ ] spos = None def _fetch ( c ) : try : if spos is None : return c . popleft ( ) else : return c . pop ( ) except IndexError : return None while nargs_spec : nargs = _fetch ( nargs_spec ) if nargs == 1 : rv . append ( _fetch ( args ) ) elif nargs > 1 : x = [ _fetch ( args ) for _ in range ( nargs ) ] if spos is not None : x . reverse ( ) rv . append ( tuple ( x ) ) elif nargs < 0 : if spos is not None : raise TypeError ( 'Cannot have two nargs < 0' ) spos = len ( rv ) rv . append ( None ) if spos is not None : rv [ spos ] = tuple ( args ) args = [ ] rv [ spos + 1 : ] = reversed ( rv [ spos + 1 : ] ) return tuple ( rv ) , list ( args )
Given an iterable of arguments and an iterable of nargs specifications,
    it returns a tuple with all the unpacked arguments at the first index
    and all remaining arguments as the second.

    The nargs specification is the number of arguments that should be consumed
    or `-1` to indicate that this position should eat up all the remainders.

    Missing items are filled with `None`.