<s> import re <EOL> import collections <EOL> class Router ( object ) : <EOL> def __init__ ( self , handlers = None ) : <EOL> self . handlers = handlers or [ ] <EOL> def add_handler ( self , url_regex , handlers ) : <EOL> self . handlers . append ( <EOL> ( re . compile ( url_regex ) , <EOL> handlers if isinstance ( handlers , collections . Iterable ) <EOL> else [ handlers ] ) ) <EOL> def get_handler ( self , url ) : <EOL> for matcher , handler in self . handlers : <EOL> match = matcher . match ( url ) <EOL> if match : <EOL> return handler , match . groups ( ) <EOL> return None , None </s>
