def match_duration(self, dur_functions, logic_bool='and'):
"""Return True if a duration matches all or any of the functions.
:param dur_functions: list of (function, value, logical_not)
:param logic_bool: (str) Apply a logical "and" or "or"
:returns: (bool)
- function: a function in python with 2 arguments: dur/value
- value: the expected value for the duration (int/float/sppasDuration)
- logical_not: boolean
:Example: Search if a duration is exactly 30ms
>>> d.match([(eq, 0.03, False)])
:Example: Search if a duration is not 30ms
>>> d.match([(eq, 0.03, True)])
>>> d.match([(ne, 0.03, False)])
:Example: Search if a duration is comprised between 0.3 and 0.7
>>> l.match([(ge, 0.03, False),
>>> (le, 0.07, False)], logic_bool="and")
See sppasDurationCompare() to get a list of functions.
"""
is_matching = False
for loc, score in self.__localizations:
dur = loc.duration()
matches = list()
for func, value, logical_not in dur_functions:
if logical_not is True:
matches.append(not func(dur, value))
else:
matches.append(func(dur, value))
if logic_bool == 'and':
is_matching = all(matches)
else:
is_matching = any(matches)
if is_matching is True:
return True
return is_matching