import re class CommandParser: @staticmethod def parse(command_string): if not command_string: raise ValueError("Empty command received!") if not isinstance(command_string, str): raise ValueError("Command must be a sting!") if re.search('[^a-zA-Z0-9_\-]', command_string): raise ValueError('Only [a-zA-Z0-9_\-] allowed') if re.search('^GET_.*', command_string): parsed_command = command_string.split('_', 1) command = parsed_command[0] value = parsed_command[1] return {'command': command, 'value': value} else: raise ValueError("Unknown command: " + command_string)