本文整理匯總了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)