Created
July 15, 2016 14:43
A Python Command Dispatcher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This is the list of recognized commands. | |
""" | |
class Dispatcher(): | |
def __init__(self): | |
self.cmd_list = { | |
'0': self.cmd_0, | |
'1': self.cmd_1, | |
'2': self.cmd_2, | |
'3': self.cmd_3 | |
} | |
def parser(self, data): | |
data = data.rstrip(';') | |
cmd, args = data.split(',')[:1], data.split(',')[1:] | |
self.cmd_list[cmd[0]](args) | |
def cmd_0(self, *args): | |
print 'cmd_0 args: {}'.format(args) | |
def cmd_1(self, *args): | |
print 'cmd_1 args: {}'.format(args) | |
def cmd_2(self, *args): | |
print 'cmd_2 args: {}'.format(args) | |
def cmd_3(self, *args): | |
print 'cmd_3 args: {}'.format(args) | |
if __name__ == '__main__': | |
d = Dispatcher() | |
d.parser("2,7,6,5,1;") | |
d.parser("1,2,3,4,5,6;") | |
d.parser("3,2,1;") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment