def skipIfHasattr(obj, attr): """Decorator that will cause a test to be skipped if given ``object`` contains given ``attr``\ ibute. """ if hasattr(obj, attr): return skip("%r has attribute %r" % (obj, attr)) return identity()
def skipUnlessHasattr(obj, attr): """Decorator that will cause a test to be skipped unless given ``object`` contains given ``attr``\ ibute. """ if not hasattr(obj, attr): return skip("%r does not have attribute %r" % (obj, attr)) return identity()
def batch(iterable, n, fillvalue=None): """Batches the elements of given iterable. Resulting iterable will yield tuples containing at most ``n`` elements (might be less if ``fillvalue`` isn't specified). :param n: Number of items in every batch :param fillvalue: Value to fill the last batch with. If None, last batch might be shorter than ``n`` elements :return: Iterable of batches .. note:: This is an extended version of grouper() recipe from the :module:`itertools` module documentation. """ ensure_iterable(iterable) if not isinstance(n, Integral): raise TypeError("invalid number of elements in a batch") if not (n > 0): raise ValueError("number of elements in a batch must be positive") # since we must use ``izip_longest`` # (``izip`` fails if ``n`` is greater than length of ``iterable``), # we will apply some 'trimming' to resulting tuples if necessary if fillvalue is None: fillvalue = object() trimmer = lambda item: tuple(x for x in item if x is not fillvalue) else: trimmer = identity() args = [iter(iterable)] * n zipped = izip_longest(*args, fillvalue=fillvalue) return imap(trimmer, zipped)
def skipUnlessReturnsTrue(predicate): """Decorator that will cause a test to be skipped unless given ``predicate`` callable evaluates to true. """ if not predicate(): desc = getattr(predicate, '__doc__', None) or repr(predicate) return skip("predicate evaluated to false: %s" % desc) return identity()
def mapvalues(function, dict_): """Return a new dictionary where the values come from applying ``function`` to the values of given dictionary. :param function: Function taking a dictionary value, or None (corresponding to identity function) .. versionadded:: 0.0.2 """ ensure_mapping(dict_) function = identity() if function is None else ensure_callable(function) return dict_.__class__((k, function(v)) for k, v in iteritems(dict_))
def mapkeys(function, dict_): """Return a new dictionary where the keys come from applying ``function`` to the keys of given dictionary. .. warning:: If ``function`` returns the same value for more than one key, it is undefined which key will be chosen for the resulting dictionary. :param function: Function taking a dictionary key, or None (corresponding to identity function) .. versionadded:: 0.0.2 """ ensure_mapping(dict_) function = identity() if function is None else ensure_callable(function) return dict_.__class__((function(k), v) for k, v in iteritems(dict_))
def mapitems(function, dict_): """Return a new dictionary where the keys and values come from applying ``function`` to key-value pairs from given dictionary. .. warning:: If ``function`` returns a key-value pair with the same key more than once, it is undefined which value will be chosen for that key in the resulting dictionary. :param function: Function taking a key-value pair as a single argument, and returning a new key-value pair; or None (corresponding to identity function) .. versionadded:: 0.0.2 """ ensure_mapping(dict_) function = identity() if function is None else ensure_callable(function) return dict_.__class__(imap(function, iteritems(dict_)))
def test_object(self): identity = __unit__.identity() self.assertIs(self.OBJECT, identity(self.OBJECT)) self.assertIsNot(self.DIFFERENT_OBJECT, identity(self.OBJECT))
def test_dicts(self): identity = __unit__.identity() self.assertIs(self.DICT, identity(self.DICT)) self.assertIsNot(self.DICT_COPY, identity(self.DICT))
def test_empty_dicts(self): identity = __unit__.identity() self.assertIs(self.EMPTY_DICT, identity(self.EMPTY_DICT)) self.assertIsNot(self.DIFFERENT_EMPTY_DICT, identity(self.EMPTY_DICT))
def test_lists(self): identity = __unit__.identity() self.assertIs(self.LIST, identity(self.LIST)) self.assertIsNot(self.LIST_COPY, identity(self.LIST))
def test_empty_lists(self): identity = __unit__.identity() self.assertIs(self.EMPTY_LIST, identity(self.EMPTY_LIST)) self.assertIsNot(self.DIFFERENT_EMPTY_LIST, identity(self.EMPTY_LIST))
def test_values(self): identity = __unit__.identity() self.assertIsNone(identity(None)) self.assertIs(0, identity(0)) self.assertIs(self.EMPTY_TUPLE, identity(self.EMPTY_TUPLE))