def omit(indices, from_, strict=False): """Returns a subsequence from given tuple, omitting specified indices. :param indices: Iterable of indices to exclude :param strict: Whether ``indices`` are required to exist in the tuple :return: Tuple without elements of specified indices :raise IndexError: If ``strict`` is True and one of ``indices`` is out of range. .. versionadded:: 0.0.3 """ from taipan.collections.sets import remove_subset ensure_iterable(indices) ensure_sequence(from_) if strict: remaining_indices = set(xrange(len(from_))) try: remove_subset(remaining_indices, indices) except KeyError as e: raise IndexError(int(str(e))) else: remaining_indices = set(xrange(len(from_))) - set(indices) return from_.__class__(from_[index] for index in remaining_indices)
def omit(keys, from_, strict=False): """Returns a subset of given dictionary, omitting specified keys. :param keys: Iterable of keys to exclude :param strict: Whether ``keys`` are required to exist in the dictionary :return: Dictionary filtered by omitting ``keys`` :raise KeyError: If ``strict`` is True and one of ``keys`` is not found in the dictionary .. versionadded:: 0.0.2 """ ensure_iterable(keys) ensure_mapping(from_) if strict: remaining_keys = set(iterkeys(from_)) remove_subset(remaining_keys, keys) # raises KeyError if necessary else: remaining_keys = set(iterkeys(from_)) - set(keys) return from_.__class__((k, from_[k]) for k in remaining_keys)
def test_set__singleton_subset(self): s = self.SET.copy() __unit__.remove_subset(s, self.SINGLETON) self.assertEquals(self.DIFFERENCE, s)
def test_set__same_one_as_subset(self): s = self.SET.copy() __unit__.remove_subset(s, self.SET) self.assertEmpty(s)
def test_singleton_set__too_big_a_subset(self): s = self.SINGLETON.copy() with self._assertKeyError(*self.DIFFERENCE): __unit__.remove_subset(s, self.SET)
def test_set__empty_subset(self): s = self.SET.copy() __unit__.remove_subset(s, set()) self.assertEquals(self.SET, s)
def test_singleton_set__empty_subset(self): s = self.SINGLETON.copy() __unit__.remove_subset(s, set()) self.assertEquals(self.SINGLETON, s)
def test_singleton_set__singleton_subset(self): s = self.SINGLETON.copy() __unit__.remove_subset(s, self.SINGLETON) self.assertEmpty(s)
def test_empty_set__nonempty_subset(self): s = set() with self._assertKeyError(*self.SINGLETON): __unit__.remove_subset(s, self.SINGLETON)
def test_empty_set__empty_iterable_subset(self): s = set() __unit__.remove_subset(s, ()) self.assertEmpty(s)
def test_empty_sets(self): s = set() __unit__.remove_subset(s, set()) self.assertEmpty(s)
def test_subset__some_object(self): with self.assertRaises(TypeError): __unit__.remove_subset(set(), object())
def test_subset__none(self): with self.assertRaises(TypeError): __unit__.remove_subset(set(), None)