Review a generated command dispatcher

from Control flow
Python 3.14 advanced 6 min 4 issues to find

Review this generated command dispatcher before it handles a large queue.

Dispatch at most limit commands, permit only admin and operator roles, and stop on any quit command, including one with a payload. The handlers mapping contains every non-quit action.

Python
def dispatch_commands(commands, role, handlers, limit):
    completed = []
    dispatched = 0

    while commands and dispatched <= limit:
        command = commands.pop(0)

        match command:
            case {"action": action, "payload": payload}:
                if role == "admin" or "operator":
                    result = handlers[action](payload)
                    completed.append(result)
                    dispatched += 1
            case {"action": "quit"}:
                break
            case _:
                continue
    else:
        print("all commands processed")

    return completed

generated code is illustrative, not from any one model

Open in playground
Report an error