我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用collections.abc.Container()。
def test_Container(self): non_samples = [None, 42, 3.14, 1j, (lambda: (yield))(), (x for x in []), ] for x in non_samples: self.assertNotIsInstance(x, Container) self.assertFalse(issubclass(type(x), Container), repr(type(x))) samples = [bytes(), str(), tuple(), list(), set(), frozenset(), dict(), dict().keys(), dict().items(), ] for x in samples: self.assertIsInstance(x, Container) self.assertTrue(issubclass(type(x), Container), repr(type(x))) self.validate_abstract_methods(Container, '__contains__') self.validate_isinstance(Container, '__contains__')
def isdisjoint(self, other): r"""Return True if the set has no elements in common with other. Sets are disjoint iff their intersection is the empty set. >>> ms = Multiset('aab') >>> ms.isdisjoint('bc') False >>> ms.isdisjoint(Multiset('ccd')) True Args: other: The other set to check disjointedness. Can also be an :class:`~typing.Iterable`\[~T] or :class:`~typing.Mapping`\[~T, :class:`int`] which are then converted to :class:`Multiset`\[~T]. """ if isinstance(other, _sequence_types + (BaseMultiset, )): pass elif not isinstance(other, Container): other = self._as_multiset(other) return all(element not in other for element in self._elements.keys())
def test_direct_subclassing(self): for B in Hashable, Iterable, Iterator, Sized, Container, Callable: class C(B): pass self.assertTrue(issubclass(C, B)) self.assertFalse(issubclass(int, C))
def test_registration(self): for B in Hashable, Iterable, Iterator, Sized, Container, Callable: class C: __hash__ = None # Make sure it isn't hashable by default self.assertFalse(issubclass(C, B), B.__name__) B.register(C) self.assertTrue(issubclass(C, B))
def test_EmptyMapping(): marker = object() # It should be possible to 'construct' an instance.. assert EmptyMapping() is EmptyMapping # Must be passable to dict() assert dict(EmptyMapping) == {} # EmptyMapping['x'] raises in various forms. assert 'x' not in EmptyMapping with pytest.raises(KeyError): EmptyMapping['x'] with pytest.raises(KeyError): del EmptyMapping['x'] EmptyMapping['x'] = 4 # Shouldn't fail assert 'x' not in EmptyMapping # but it's a no-op with pytest.raises(KeyError): EmptyMapping['x'] # Check it's all empty check_empty_iterable(EmptyMapping, 'EmptyMapping') check_empty_iterable(EmptyMapping.keys(), 'keys()') check_empty_iterable(EmptyMapping.values(), 'values()') check_empty_iterable(EmptyMapping.items(), 'items()', item=('x', 'y')) # Dict methods assert EmptyMapping.get('x') is None assert EmptyMapping.setdefault('x') is None assert EmptyMapping.get('x', marker) is marker assert EmptyMapping.setdefault('x', marker) is marker assert EmptyMapping.pop('x', marker) is marker with pytest.raises(KeyError): EmptyMapping.popitem() with pytest.raises(KeyError): EmptyMapping.pop('x') assert not EmptyMapping assert len(EmptyMapping) == 0 # Should work, but do nothing and return None. assert EmptyMapping.update({1: 23, 'test': 34, }) is None assert EmptyMapping.update(other=5, a=1, b=3) is None # Can't give more than one mapping as a positional argument, # though. with pytest.raises(TypeError): EmptyMapping.update({3: 4}, {1: 2}) # Check it's registered in ABCs. from collections import abc assert isinstance(EmptyMapping, abc.Container) assert isinstance(EmptyMapping, abc.Sized) assert isinstance(EmptyMapping, abc.Mapping) assert isinstance(EmptyMapping, abc.MutableMapping)