Skip to content

Instantly share code, notes, and snippets.

@iwillig
Created December 9, 2010 03:18
Show Gist options
  • Select an option

  • Save iwillig/734285 to your computer and use it in GitHub Desktop.

Select an option

Save iwillig/734285 to your computer and use it in GitHub Desktop.
import re
from collections import namedtuple
from dispatch import consumer
class Message(object):
"""
"""
def __init__(self,text ):
"""
"""
self.text = text
msg = Message(text="bal.12324")
match = namedtuple('Match','matcher module function langauge')
matchers = [
match('^bal','consumer','get_balance','en'),
match('^solde','consumer','get_balance','fr'),
match('^prim','consumer','set_primary_number','en'),
match('^tel','consumer','get_primary_number','fr'),
match('^add','consumer','add_credit','en'),
match('^recharge','consumer','add_credit','fr'),
match('^on','consumer','turn_circuit_on','fr'),
match('^off','consumer','turn_circuit_off','fr'),
]
def match_message(message):
for match in matchers:
if re.match(match.matcher,message.text):
module = getattr(__import__("dispatch"),match.module)
getattr(module, match.function)(message)
else:
pass # do something with un matched messages
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment