def suite_iter(suite): """suite_iter(suite) -> an iterator on its direct sub-suites. For compatibility with Python versions before 2.4""" def add_extra_description_field(test): test._testoob_extra_description = "" return True try: return _ifilter(add_extra_description_field, iter(suite)) except TypeError: # Before 2.4, test suites weren't iterable return _ifilter(add_extra_description_field, iter(suite._tests))
def full_extractor(suite, recursive_iterator=_breadth_first): """Extract the text fixtures from a suite. Descends recursively into sub-suites.""" import unittest def test_children(node): if isinstance(node, unittest.TestSuite): return suite_iter(node) return [] return _ifilter(lambda test: isinstance(test, unittest.TestCase), recursive_iterator(suite, children=test_children))
def __and__(self, other): if not isinstance(other, Counter): return NotImplemented result = Counter() if len(self) < len(other): self, other = other, self for elem in _ifilter(self.__contains__, other): p, q = self[elem], other[elem] newcount = p if p < q else q if newcount > 0: result[elem] = newcount return result
def itruthy(iterable): """Returns an iterator to for an iterable with only the truthy values. Example:: tuple(itruthy((0, 1, 2, False, None, True))) -> (1, 2, True) :param iterable: Iterable sequence. :yields: Iterator for an iterable with truthy values. """ return _ifilter(bool, iterable)
def iselect(predicate, iterable): """Select all items from the sequence for which the predicate is true. iselect(function or None, sequence) --> iterator :param predicate: Predicate function. If ``None``, select all truthy items. :param iterable: Iterable. :yields: A iterable of all items for which the predicate is true. """ return _ifilter(predicate, iterable)
def select_dict(predicate, dictionary): """ Select a dictionary. :param predicate: Predicate function that accepts two arguments ``key, value`` and returns ``True`` for selectable elements. :returns: New dictionary of selected ``key=value`` pairs. """ def _pred(tup): return predicate(*tup) _predicate = _pred if predicate else all return dict(_ifilter(_predicate, dictionary.items()))
def idifference(iterable1, iterable2): """Difference between one iterable and another. Items from the first iterable are included in the difference. iterable1 - iterable2 = difference :param iterable1: Iterable sequence. :param iterable2: Iterable sequence. :yields: Generator for the difference between the two given iterables. """ return _ifilter(functools.partial(omits, iterable2), iterable1)
def select_dict(predicate, dictionary): """Select from a dictionary. :param predicate: Predicate function that accepts two arguments ``key, value`` and returns ``True`` for selectable elements. :returns: New dictionary of selected ``key=value`` pairs. """ def _pred(tup): """Apply arguments to predicate.""" return predicate(*tup) _predicate = _pred if predicate else all return dict(_ifilter(_predicate, dictionary.items()))
def __and__(self, other): """ Intersection is the minimum of corresponding counts. >>> Counter('abbb') & Counter('bcc') Counter({'b': 1}) """ if not isinstance(other, Counter): return NotImplemented _min = min result = Counter() if len(self) < len(other): self, other = other, self for elem in _ifilter(self.__contains__, other): newcount = _min(self[elem], other[elem]) if newcount > 0: result[elem] = newcount return result
def __and__(self, other): ''' Intersection is the minimum of corresponding counts. >>> Counter('abbb') & Counter('bcc') Counter({'b': 1}) ''' if not isinstance(other, Counter): return NotImplemented result = Counter() if len(self) < len(other): self, other = other, self for elem in _ifilter(self.__contains__, other): p, q = self[elem], other[elem] newcount = p if p < q else q if newcount > 0: result[elem] = newcount return result
def predicate(pred): return _iterable_decorator(lambda iterable: _ifilter(pred, iterable))
def suite(): result = unittest.TestSuite() for test_case in _ifilter(_is_test_case, globals_dict.values()): result.addTest(unittest.makeSuite(test_case)) return result