示例#1
0
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)
示例#2
0
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)
示例#3
0
 def test_set__singleton_subset(self):
     s = self.SET.copy()
     __unit__.remove_subset(s, self.SINGLETON)
     self.assertEquals(self.DIFFERENCE, s)
示例#4
0
 def test_set__same_one_as_subset(self):
     s = self.SET.copy()
     __unit__.remove_subset(s, self.SET)
     self.assertEmpty(s)
示例#5
0
 def test_singleton_set__too_big_a_subset(self):
     s = self.SINGLETON.copy()
     with self._assertKeyError(*self.DIFFERENCE):
         __unit__.remove_subset(s, self.SET)
示例#6
0
 def test_set__empty_subset(self):
     s = self.SET.copy()
     __unit__.remove_subset(s, set())
     self.assertEquals(self.SET, s)
示例#7
0
 def test_singleton_set__empty_subset(self):
     s = self.SINGLETON.copy()
     __unit__.remove_subset(s, set())
     self.assertEquals(self.SINGLETON, s)
示例#8
0
 def test_singleton_set__singleton_subset(self):
     s = self.SINGLETON.copy()
     __unit__.remove_subset(s, self.SINGLETON)
     self.assertEmpty(s)
示例#9
0
 def test_empty_set__nonempty_subset(self):
     s = set()
     with self._assertKeyError(*self.SINGLETON):
         __unit__.remove_subset(s, self.SINGLETON)
示例#10
0
 def test_empty_set__empty_iterable_subset(self):
     s = set()
     __unit__.remove_subset(s, ())
     self.assertEmpty(s)
示例#11
0
 def test_empty_sets(self):
     s = set()
     __unit__.remove_subset(s, set())
     self.assertEmpty(s)
示例#12
0
 def test_subset__some_object(self):
     with self.assertRaises(TypeError):
         __unit__.remove_subset(set(), object())
示例#13
0
 def test_subset__none(self):
     with self.assertRaises(TypeError):
         __unit__.remove_subset(set(), None)