本文整理汇总了Python中collections.MutableSet方法的典型用法代码示例。如果您正苦于以下问题:Python collections.MutableSet方法的具体用法?Python collections.MutableSet怎么用?Python collections.MutableSet使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类collections
的用法示例。
在下文中一共展示了collections.MutableSet方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: discard
# 需要导入模块: import collections [as 别名]
# 或者: from collections 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: remove_key
# 需要导入模块: import collections [as 别名]
# 或者: from collections import MutableSet [as 别名]
def remove_key(self, key):
# The parts of the ConfigSection that might contain "key"
trackers = [self.validation_methods,
self._destinations,
self._restricted,
self._required_keys,
self.defaults,
self._values,
self._unvalidated_keys,
]
for tracker in trackers:
if key in tracker:
if isinstance(tracker, collections.MutableMapping):
del tracker[key]
elif isinstance(tracker, collections.MutableSequence):
tracker.remove(key)
elif isinstance(tracker, collections.MutableSet):
tracker.discard(key)
示例3: modifies_known_mutable
# 需要导入模块: import collections [as 别名]
# 或者: from collections import MutableSet [as 别名]
def modifies_known_mutable(obj, attr):
"""This function checks if an attribute on a builtin mutable object
(list, dict, set or deque) would modify it if called. It also supports
the "user"-versions of the objects (`sets.Set`, `UserDict.*` etc.) and
with Python 2.6 onwards the abstract base classes `MutableSet`,
`MutableMapping`, and `MutableSequence`.
>>> modifies_known_mutable({}, "clear")
True
>>> modifies_known_mutable({}, "keys")
False
>>> modifies_known_mutable([], "append")
True
>>> modifies_known_mutable([], "index")
False
If called with an unsupported object (such as unicode) `False` is
returned.
>>> modifies_known_mutable("foo", "upper")
False
"""
for typespec, unsafe in _mutable_spec:
if isinstance(obj, typespec):
return attr in unsafe
return False
示例4: test_MutableSet
# 需要导入模块: import collections [as 别名]
# 或者: from collections import MutableSet [as 别名]
def test_MutableSet(self):
self.assertIsInstance(set(), MutableSet)
self.assertTrue(issubclass(set, MutableSet))
self.assertNotIsInstance(frozenset(), MutableSet)
self.assertFalse(issubclass(frozenset, MutableSet))
self.validate_abstract_methods(MutableSet, '__contains__', '__iter__', '__len__',
'add', 'discard')
示例5: test_issue_5647
# 需要导入模块: import collections [as 别名]
# 或者: from collections import MutableSet [as 别名]
def test_issue_5647(self):
# MutableSet.__iand__ mutated the set during iteration
s = WithSet('abcd')
s &= WithSet('cdef') # This used to fail
self.assertEqual(set(s), set('cd'))
示例6: test_issue_4920
# 需要导入模块: import collections [as 别名]
# 或者: from collections import MutableSet [as 别名]
def test_issue_4920(self):
# MutableSet.pop() method did not work
class MySet(collections.MutableSet):
__slots__=['__s']
def __init__(self,items=None):
if items is None:
items=[]
self.__s=set(items)
def __contains__(self,v):
return v in self.__s
def __iter__(self):
return iter(self.__s)
def __len__(self):
return len(self.__s)
def add(self,v):
result=v not in self.__s
self.__s.add(v)
return result
def discard(self,v):
result=v in self.__s
self.__s.discard(v)
return result
def __repr__(self):
return "MySet(%s)" % repr(list(self))
s = MySet([5,43,2,1])
self.assertEqual(s.pop(), 1)
示例7: is_mutable
# 需要导入模块: import collections [as 别名]
# 或者: from collections import MutableSet [as 别名]
def is_mutable(obj):
return isinstance(obj, (collections.MutableSequence,
collections.MutableSet,
collections.MutableMapping))
示例8: clear
# 需要导入模块: import collections [as 别名]
# 或者: from collections import MutableSet [as 别名]
def clear(self):
self._items.clear()
# From this point on, only tests.
# Perhaps there are more test cases than really required,
# but the MutableSet ABC isn't tested very much in the
# standard regression tests.