本文整理汇总了Python中collections.abc.MutableSet方法的典型用法代码示例。如果您正苦于以下问题:Python abc.MutableSet方法的具体用法?Python abc.MutableSet怎么用?Python abc.MutableSet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类collections.abc
的用法示例。
在下文中一共展示了abc.MutableSet方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: discard
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import MutableSet [as 别名]
def discard(self, key):
"""
Remove an element. Do not raise an exception if absent.
The MutableSet mixin uses this to implement the .remove() method, which
*does* raise an error when asked to remove a non-existent item.
Example:
>>> oset = OrderedSet([1, 2, 3])
>>> oset.discard(2)
>>> print(oset)
OrderedSet([1, 3])
>>> oset.discard(2)
>>> print(oset)
OrderedSet([1, 3])
"""
if key in self:
i = self.map[key]
del self.items[i]
del self.map[key]
for k, v in self.map.items():
if v >= i:
self.map[k] = v - 1
示例2: discard
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import MutableSet [as 别名]
def discard(self, key):
"""
Remove an element. Do not raise an exception if absent.
The MutableSet mixin uses this to implement the .remove() method, which
*does* raise an error when asked to remove a non-existent item.
"""
if key in self:
i = self.items.index(key)
del self.items[i]
del self.map[key]
for k, v in self.map.items():
if v >= i:
self.map[k] = v - 1
示例3: iter_characters
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import MutableSet [as 别名]
def iter_characters(self):
return map(chr, self.__iter__())
#
# MutableSet's abstract methods implementation
示例4: discard
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import MutableSet [as 别名]
def discard(self, value):
start_cp, end_cp = check_code_point(value)
code_points = self._code_points
for k in reversed(range(len(code_points))):
cp = code_points[k]
if isinstance(cp, int):
cp = cp, cp + 1
if start_cp >= cp[1]:
break
elif end_cp >= cp[1]:
if start_cp <= cp[0]:
del code_points[k]
elif start_cp - cp[0] > 1:
code_points[k] = cp[0], start_cp
else:
code_points[k] = cp[0]
elif end_cp > cp[0]:
if start_cp <= cp[0]:
if cp[1] - end_cp > 1:
code_points[k] = end_cp, cp[1]
else:
code_points[k] = cp[1] - 1
else:
if cp[1] - end_cp > 1:
code_points.insert(k + 1, (end_cp, cp[1]))
else:
code_points.insert(k + 1, cp[1] - 1)
if start_cp - cp[0] > 1:
code_points[k] = cp[0], start_cp
else:
code_points[k] = cp[0]
#
# MutableSet's mixin methods override
示例5: apply
# 需要导入模块: from collections import abc [as 别名]
# 或者: from collections.abc import MutableSet [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)