From e6bda7547dca142718ae84a57885f0aa07ed146e 2008-07-30 20:08:19 From: Fernando Perez Date: 2008-07-30 20:08:19 Subject: [PATCH] Turn little test into proper doctest. Cleanup and document. --- diff --git a/IPython/strdispatch.py b/IPython/strdispatch.py old mode 100755 new mode 100644 index 8516528..55e2eab --- a/IPython/strdispatch.py +++ b/IPython/strdispatch.py @@ -1,13 +1,32 @@ +"""String dispatch class to match regexps and dispatch commands. +""" + +# Stdlib imports +import re + +# Our own modules from IPython.hooks import CommandChainDispatcher import IPython.hooks -import re +# Code begins class StrDispatch(object): - """ Dispatch (lookup) a set of strings / regexps for match """ + """Dispatch (lookup) a set of strings / regexps for match. + + Example: + + >>> dis = StrDispatch() + >>> dis.add_s('hei',34, priority = 4) + >>> dis.add_s('hei',123, priority = 2) + >>> dis.add_re('h.i', 686) + >>> print list(dis.flat_matches('hei')) + [123, 34, 686] + """ + def __init__(self): self.strs = {} self.regexs = {} + def add_s(self, s, obj, priority= 0 ): """ Adds a target 'string' for dispatching """ @@ -31,9 +50,8 @@ class StrDispatch(object): if re.match(r, key): yield obj else: - #print "nomatch",key + #print "nomatch",key # dbg pass - def __repr__(self): return "" % (self.strs, self.regexs) @@ -44,22 +62,9 @@ class StrDispatch(object): for el in self.strs[key]: yield el[1] - def flat_matches(self, key): """ Yield all 'value' targets, without priority """ for val in self.dispatch(key): for el in val: yield el[1] # only value, no priority return - - -def test(): - d = StrDispatch() - d.add_s('hei',34, priority = 4) - d.add_s('hei',123, priority = 2) - print list(d.dispatch('hei')) - d.add_re('h.i', 686) - print list(d.flat_matches('hei')) - -if __name__ == '__main__': - test() \ No newline at end of file