Skip to content

Instantly share code, notes, and snippets.

@ilovetogetspamed
Created July 15, 2016 14:43
Show Gist options
  • Save ilovetogetspamed/045523ab5c01fc17910c765a4b120832 to your computer and use it in GitHub Desktop.
Save ilovetogetspamed/045523ab5c01fc17910c765a4b120832 to your computer and use it in GitHub Desktop.
A Python Command Dispatcher
"""
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