def run_for_match(candidate, patterns, search_lists, handlers, default_handler):
for pattern, search_list, handler in zip(patterns, search_lists, handlers):
if match(pattern, candidate):
if candidate not in search_list:
handler(candidate)
break
else:
default_handler(candidate)
run_for_match(string, [a, b], [a_list, b_list], [do_something, do_something], do_something_else)
patterns
これを、、、、をカプセル化するオブジェクトに入れてもsearch_lists
、非常に理handlers
にdefault_handler
かなっています。何かのようなもの:
class StringHandlerManager(object):
def __init__(self, default_handler):
self.handler_registry = []
self.default_handler = default_handler
def add_handler(self, pattern, search_list, handler):
self.handler_registry.append((pattern, search_list, handler))
def __call__(self, candidate):
for pattern, search_list, handler in self.handler_registry:
if match(pattern, candidate):
if candidate not in search_list:
handler(candidate)
break
else:
default_handler(candidate)
handle_string = StringHandlerManager(do_something_else)
handle_string.add_handler(a, a_list, do_something)
handle_string.add_handler(b, b_list, do_something)
handle_string(string)