Solution
# def register_handler(command_type: CommandType) -> typing.Callable: # -
def register_handler(command_type: CommandType, handlers: dict = cls_handlers) -> typing.Callable: # +
def decorator(function: typing.Callable):
# MessageProcessor.cls_handlers[command_type] = function # -
handlers[command_type] = function # +
return function
return decorator
def make_register_handler(handlers: dict):
def register_handler(command_type: CommandType) -> typing.Callable:
def decorator(function: typing.Callable):
handlers[command_type] = function
return function
return decorator
return register_handler
class MessageProcessor:
cls_handlers = {}
register_handler = make_register_handler(cls_handlers)
class MessageProcessor:
cls_handlers = {}
@classmethod
def register_handler(cls, command_type: CommandType) -> typing.Callable:
def decorator(function: typing.Callable):
cls.cls_handlers[command_type] = function
return function
return decorator
def process(self, message: str):
tokens = message.split()
command_type = CommandType[tokens[0]]
args = tokens[1:]
self.cls_handlers[command_type](args)
@MessageProcessor.register_handler(CommandType.DONE)
def handle_done(args):
print("Handler for DONE command. args: ", args)
@MessageProcessor.register_handler(CommandType.PASS)
def handle_pass(args):
print("Handler for PASS command. args: ", args)