Refactor/reduce shared 'ensure(task/retry)' code

These methods are nearly identical so we should refactor
them to use the same code for sanity and understanding
purposes.

Change-Id: Ibaf270bd451b6d02d7782901bc2327afe04d3847
This commit is contained in:
Joshua Harlow
2015-04-02 11:15:06 -07:00
committed by Joshua Harlow
parent 426d08f951
commit 530328a86c
2 changed files with 40 additions and 62 deletions

View File

@@ -88,14 +88,18 @@ def find_monotonic(allow_time_time=False):
return None
def match_type_handler(item, type_handlers):
"""Matches a given items type using the given match types + handlers.
def match_type(obj, matchers):
"""Matches a given object using the given matchers list/iterable.
Returns the handler if a type match occurs, otherwise none.
NOTE(harlowja): each element of the provided list/iterable must be
tuple of (valid types, result).
Returns the result (the second element of the provided tuple) if a type
match occurs, otherwise none if no matches are found.
"""
for (match_types, handler_func) in type_handlers:
if isinstance(item, match_types):
return handler_func
for (match_types, match_result) in matchers:
if isinstance(obj, match_types):
return match_result
else:
return None