(commands)
return grouped
Returns a list of tuples:

        [(command_to_run, [list, of, commands])]

    If the list of commands has a single item, the command was not grouped.
Returns a list of tuples:
Returns a list of tuples :
def regroup_commands(commands):
    """
    Returns a list of tuples:

        [(command_to_run, [list, of, commands])]

    If the list of commands has a single item, the command was not grouped.
    """
    grouped = []
    pending = []

    def group_pending():
        if not pending:
            return

        new_command = grouped_command(pending)
        result = []
        while pending:
            result.append(pending.pop(0))
        grouped.append((new_command, result))

    for command, next_command in peek(commands):
        # if the previous command was a get, and this is a set we must execute
        # any pending commands
        # TODO: unless this command is a get_multi and it matches the same option
        # signature
        if can_group_commands(command, next_command):
            # if previous command does not match this command
            if pending and not can_group_commands(pending[0], command):
                group_pending()

            pending.append(command)
        else:
            # if pending exists for this command, group it
            if pending and can_group_commands(pending[0], command):
                pending.append(command)
            else:
                grouped.append((command.clone(), [command]))

            # We couldn't group with previous command, so ensure we bubble up
            group_pending()

    group_pending()

    return grouped
def regroup_commands ( commands ) : grouped = [ ] pending = [ ] def group_pending ( ) : if not pending : return new_command = grouped_command ( pending ) result = [ ] while pending : result . append ( pending . pop ( 0 ) ) grouped . append ( ( new_command , result ) ) for command , next_command in peek ( commands ) : # if the previous command was a get, and this is a set we must execute # any pending commands # TODO: unless this command is a get_multi and it matches the same option # signature if can_group_commands ( command , next_command ) : # if previous command does not match this command if pending and not can_group_commands ( pending [ 0 ] , command ) : group_pending ( ) pending . append ( command ) else : # if pending exists for this command, group it if pending and can_group_commands ( pending [ 0 ] , command ) : pending . append ( command ) else : grouped . append ( ( command . clone ( ) , [ command ] ) ) # We couldn't group with previous command, so ensure we bubble up group_pending ( ) group_pending ( ) return grouped