本文整理汇总了Python中collections.abc.Container方法的典型用法代码示例。如果您正苦于以下问题:Python abc.Container方法的具体用法?Python abc.Container怎么用?Python abc.Container使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类collections.abc
的用法示例。
在下文中一共展示了abc.Container方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_Container
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Container [as 别名]
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__')
示例2: test_Container
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Container [as 别名]
def test_Container(self):
non_samples = [None, 42, 3.14, 1j,
_test_gen(),
(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__')
示例3: _check_params
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Container [as 别名]
def _check_params(ignored_variables, ignored_interactions):
"""Helper for sample methods"""
if ignored_variables is None:
ignored_variables = set()
elif not isinstance(ignored_variables, abc.Container):
ignored_variables = set(ignored_variables)
if ignored_interactions is None:
ignored_interactions = set()
elif not isinstance(ignored_interactions, abc.Container):
ignored_interactions = set(ignored_interactions)
return ignored_variables, ignored_interactions
示例4: test_algorithms
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Container [as 别名]
def test_algorithms():
assert isinstance(hashlib.algorithms_guaranteed, Collection)
assert hashlib.algorithms_guaranteed
assert isinstance(hashlib.algorithms_available, Collection)
assert hashlib.algorithms_available
assert frozenset(hashlib.algorithms_guaranteed).issubset(
hashlib.algorithms_available
)
示例5: test_direct_subclassing
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Container [as 别名]
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))
示例6: test_registration
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Container [as 别名]
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))
示例7: parse_trusted_list
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Container [as 别名]
def parse_trusted_list(lst):
if isinstance(lst, str) or not isinstance(lst, Sequence):
raise TypeError(MSG)
out = []
has_ellipsis = False
for elem in lst:
if elem is ...:
has_ellipsis = True
new_elem = ...
else:
if has_ellipsis:
raise ValueError(
"Ellipsis is allowed only at the end of list")
if isinstance(elem, str) or not isinstance(elem, Container):
raise TypeError(MSG)
new_elem = []
for item in elem:
if isinstance(item, IP_CLASSES):
new_elem.append(item)
continue
try:
new_elem.append(ip_address(item))
except ValueError:
try:
new_elem.append(ip_network(item))
except ValueError:
raise ValueError(
"{!r} is not IPv4 or IPv6 address or network"
.format(item))
out.append(new_elem)
return out
示例8: __init__
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Container [as 别名]
def __init__(self, container):
assert isinstance(container, abc.Container), '`container` must support `in` operation'
self.container = container
# Name
if isinstance(self.container, Map):
self.name = self.container.name # Inherit name
else:
# Format
if isinstance(self.container, abc.Iterable):
cs = _(u',').join(map(get_literal_name, self.container))
else:
cs = get_primitive_name(self.container)
self.name = _(u'In({container})').format(container=cs)
示例9: test_direct_subclassing
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Container [as 别名]
def test_direct_subclassing(self):
for B in Hashable, Iterable, Iterator, Reversible, Sized, Container, Callable:
class C(B):
pass
self.assertTrue(issubclass(C, B))
self.assertFalse(issubclass(int, C))
示例10: test_registration
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Container [as 别名]
def test_registration(self):
for B in Hashable, Iterable, Iterator, Reversible, 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))
示例11: test_issue26915
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Container [as 别名]
def test_issue26915(self):
# Container membership test should check identity first
class CustomEqualObject:
def __eq__(self, other):
return False
class CustomSequence(Sequence):
def __init__(self, seq):
self._seq = seq
def __getitem__(self, index):
return self._seq[index]
def __len__(self):
return len(self._seq)
nan = float('nan')
obj = CustomEqualObject()
seq = CustomSequence([nan, obj, nan])
containers = [
seq,
ItemsView({1: nan, 2: obj}),
ValuesView({1: nan, 2: obj})
]
for container in containers:
for elem in container:
self.assertIn(elem, container)
self.assertEqual(seq.index(nan), 0)
self.assertEqual(seq.index(obj), 1)
self.assertEqual(seq.count(nan), 2)
self.assertEqual(seq.count(obj), 1)
示例12: apply
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Container [as 别名]
def apply(self, attrs=None, kattrs=None, merge=False):
""" Apply new attributes or classes to the target """
for attr in attrs:
kattrs = kattrs or {}
# Treat objects as assigned to their name
if hasattr(attr, "__name__"):
kattrs[attr.__name__] = attr
else:
kattrs[attr] = inspect.getattr_static(self.source, attr)
for attr, value in kattrs.items():
old_value = inspect.getattr_static(self.target, attr, None)
# If callable, preserve old func
if callable(value) and callable(old_value):
# Prevent duplicate patching
if value in patchy_records:
continue
patchy_records[value] = old_value
# Merge collections and classes instead of replacing
if merge:
if isinstance(old_value, abc.Container):
if isinstance(value, abc.Mapping) and isinstance(old_value, abc.MutableMapping):
old_value.update(value)
logger.info('Merging mapping {mod}.{attr}'.format(mod=self.target.__name__, attr=attr))
elif isinstance(value, abc.Sequence) and isinstance(old_value, abc.MutableSequence):
old_value.extend(value)
logger.info('Merging sequence {mod}.{attr}'.format(mod=self.target.__name__, attr=attr))
elif isinstance(value, abc.Set) and isinstance(old_value, abc.MutableSet):
old_value.update(value)
logger.info('Merging set {mod}.{attr}'.format(mod=self.target.__name__, attr=attr))
else:
setattr(self.target, attr, value)
logger.info("Couldn't merge collection {target}.{attr}, replaced instead".format(
target=self.target.__name__,
attr=attr))
continue
elif isinstance(old_value, type):
logger.info('Merging class for {target}.{attr}'.format(
target=self.target.__name__, attr=attr))
self.cls(old_value, value).auto()
continue
logger.info('Setting value {target}.{attr}'.format(target=self.target.__name__, attr=attr))
# Apply patched value
setattr(self.target, attr, value)
示例13: scale
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import Container [as 别名]
def scale(self, scalar, ignored_variables=None, ignored_interactions=None,
ignore_offset=False):
"""Multiply all the biases by the specified scalar.
Args:
scalar (number):
Value by which to scale the energy range of the binary
quadratic model.
ignored_variables (iterable, optional):
Biases associated with these variables are not scaled.
ignored_interactions (iterable[tuple], optional):
As an iterable of 2-tuples. Biases associated with these
interactions are not scaled.
ignore_offset (bool, default=False):
If True, the offset is not scaled.
"""
if ignored_variables is None:
ignored_variables = set()
elif not isinstance(ignored_variables, Container):
ignored_variables = set(ignored_variables)
if ignored_interactions is None:
ignored_interactions = set()
elif not isinstance(ignored_interactions, Container):
ignored_interactions = set(ignored_interactions)
linear = self.linear
for v in linear:
if v in ignored_variables:
continue
linear[v] *= scalar
quadratic = self.quadratic
for u, v in quadratic:
if (u, v) in ignored_interactions or (v, u) in ignored_interactions:
continue
quadratic[(u, v)] *= scalar
if not ignore_offset:
self.offset *= scalar