本文整理汇总了Python中UserDict.UserDict方法的典型用法代码示例。如果您正苦于以下问题:Python UserDict.UserDict方法的具体用法?Python UserDict.UserDict怎么用?Python UserDict.UserDict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类UserDict
的用法示例。
在下文中一共展示了UserDict.UserDict方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def __init__(*args, **kw):
if not args:
raise TypeError("descriptor '__init__' of 'WeakValueDictionary' "
"object needs an argument")
self = args[0]
args = args[1:]
if len(args) > 1:
raise TypeError('expected at most 1 arguments, got %d' % len(args))
def remove(wr, selfref=ref(self), _atomic_removal=_remove_dead_weakref):
self = selfref()
if self is not None:
if self._iterating:
self._pending_removals.append(wr.key)
else:
# Atomic removal is necessary since this function
# can be called asynchronously by the GC
_atomic_removal(self.data, wr.key)
self._remove = remove
# A list of keys to be removed
self._pending_removals = []
self._iterating = set()
UserDict.UserDict.__init__(self, *args, **kw)
示例2: test_bad_args
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def test_bad_args(self):
with self.assertRaises(TypeError):
type()
with self.assertRaises(TypeError):
type('A', ())
with self.assertRaises(TypeError):
type('A', (), {}, ())
with self.assertRaises(TypeError):
type('A', (), dict={})
with self.assertRaises(TypeError):
type('A', [], {})
with self.assertRaises(TypeError):
type('A', (), UserDict.UserDict())
with self.assertRaises(TypeError):
type('A', (None,), {})
with self.assertRaises(TypeError):
type('A', (bool,), {})
with self.assertRaises(TypeError):
type('A', (int, str), {})
class B:
pass
with self.assertRaises(TypeError):
type('A', (B,), {})
示例3: test_missing
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def test_missing(self):
# Make sure UserDict doesn't have a __missing__ method
self.assertEqual(hasattr(UserDict, "__missing__"), False)
# Test several cases:
# (D) subclass defines __missing__ method returning a value
# (E) subclass defines __missing__ method raising RuntimeError
# (F) subclass sets __missing__ instance variable (no effect)
# (G) subclass doesn't define __missing__ at all
class D(UserDict.UserDict):
def __missing__(self, key):
return 42
d = D({1: 2, 3: 4})
self.assertEqual(d[1], 2)
self.assertEqual(d[3], 4)
self.assertNotIn(2, d)
self.assertNotIn(2, d.keys())
self.assertEqual(d[2], 42)
class E(UserDict.UserDict):
def __missing__(self, key):
raise RuntimeError(key)
e = E()
try:
e[42]
except RuntimeError, err:
self.assertEqual(err.args, (42,))
示例4: test_missing
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def test_missing(self):
# Make sure UserDict doesn't have a __missing__ method
self.assertEqual(hasattr(UserDict, "__missing__"), False)
# Test several cases:
# (D) subclass defines __missing__ method returning a value
# (E) subclass defines __missing__ method raising RuntimeError
# (F) subclass sets __missing__ instance variable (no effect)
# (G) subclass doesn't define __missing__ at a all
class D(UserDict.UserDict):
def __missing__(self, key):
return 42
d = D({1: 2, 3: 4})
self.assertEqual(d[1], 2)
self.assertEqual(d[3], 4)
self.assertNotIn(2, d)
self.assertNotIn(2, d.keys())
self.assertEqual(d[2], 42)
class E(UserDict.UserDict):
def __missing__(self, key):
raise RuntimeError(key)
e = E()
try:
e[42]
except RuntimeError, err:
self.assertEqual(err.args, (42,))
示例5: __init__
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def __init__(*args, **kw):
if not args:
raise TypeError("descriptor '__init__' of 'WeakValueDictionary' "
"object needs an argument")
self = args[0]
args = args[1:]
if len(args) > 1:
raise TypeError('expected at most 1 arguments, got %d' % len(args))
def remove(wr, selfref=ref(self)):
self = selfref()
if self is not None:
if self._iterating:
self._pending_removals.append(wr.key)
else:
del self.data[wr.key]
self._remove = remove
# A list of keys to be removed
self._pending_removals = []
self._iterating = set()
UserDict.UserDict.__init__(self, *args, **kw)
示例6: modifies_known_mutable
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [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
示例7: __init__
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def __init__(self, *args, **kw):
def remove(wr, selfref=ref(self)):
self = selfref()
if self is not None:
del self.data[wr.key]
self._remove = remove
UserDict.UserDict.__init__(self, *args, **kw)
示例8: to_String_for_signature
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def to_String_for_signature(obj, to_String_for_subst=to_String_for_subst,
AttributeError=AttributeError):
try:
f = obj.for_signature
except AttributeError:
if isinstance(obj, dict):
# pprint will output dictionary in key sorted order
# with py3.5 the order was randomized. In general depending on dictionary order
# which was undefined until py3.6 (where it's by insertion order) was not wise.
return pprint.pformat(obj, width=1000000)
else:
return to_String_for_subst(obj)
else:
return f()
# The SCons "semi-deep" copy.
#
# This makes separate copies of lists (including UserList objects)
# dictionaries (including UserDict objects) and tuples, but just copies
# references to anything else it finds.
#
# A special case is any object that has a __semi_deepcopy__() method,
# which we invoke to create the copy. Currently only used by
# BuilderDict to actually prevent the copy operation (as invalid on that object).
#
# The dispatch table approach used here is a direct rip-off from the
# normal Python copy module.
示例9: semi_deepcopy
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def semi_deepcopy(x):
copier = _semi_deepcopy_dispatch.get(type(x))
if copier:
return copier(x)
else:
if hasattr(x, '__semi_deepcopy__') and callable(x.__semi_deepcopy__):
return x.__semi_deepcopy__()
elif isinstance(x, UserDict):
return x.__class__(semi_deepcopy_dict(x))
elif isinstance(x, UserList):
return x.__class__(_semi_deepcopy_list(x))
return x
示例10: test_UserDict_is_object
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def test_UserDict_is_object(self):
# A UserDict (and similar classes) are not dicts, but they're dict-like
# and should be treated as objects
try:
# Python 2
from UserDict import UserDict
except ImportError:
# Python 3
from collections import UserDict
valids = [UserDict({"a": "b"})]
invalids = []
self._test_type('object', valids, invalids)
示例11: test_init
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def test_init(self):
for kw in 'self', 'other', 'iterable':
self.assertEqual(list(UserDict.UserDict(**{kw: 42}).items()),
[(kw, 42)])
self.assertEqual(list(UserDict.UserDict({}, dict=42).items()),
[('dict', 42)])
self.assertEqual(list(UserDict.UserDict({}, dict=None).items()),
[('dict', None)])
with test_support.check_warnings((".*'dict'.*",
PendingDeprecationWarning)):
self.assertEqual(list(UserDict.UserDict(dict={'a': 42}).items()),
[('a', 42)])
self.assertRaises(TypeError, UserDict.UserDict, 42)
self.assertRaises(TypeError, UserDict.UserDict, (), ())
self.assertRaises(TypeError, UserDict.UserDict.__init__)
示例12: test_update
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def test_update(self):
for kw in 'self', 'other', 'iterable':
d = UserDict.UserDict()
d.update(**{kw: 42})
self.assertEqual(list(d.items()), [(kw, 42)])
d = UserDict.UserDict()
with test_support.check_warnings((".*'dict'.*",
PendingDeprecationWarning)):
d.update(dict={'a': 42})
self.assertEqual(list(d.items()), [('a', 42)])
self.assertRaises(TypeError, UserDict.UserDict().update, 42)
self.assertRaises(TypeError, UserDict.UserDict().update, {}, {})
self.assertRaises(TypeError, UserDict.UserDict.update)
示例13: test_fromkeys
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def test_fromkeys(self):
TestMappingProtocol.test_fromkeys(self)
class mydict(self.type2test):
def __new__(cls):
return UserDict.UserDict()
ud = mydict.fromkeys('ab')
self.assertEqual(ud, {'a':None, 'b':None})
self.assertIsInstance(ud, UserDict.UserDict)
示例14: test_setting_dict_to_invalid
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def test_setting_dict_to_invalid(self):
self.cannot_set_attr(self.b, '__dict__', None, TypeError)
self.cannot_set_attr(self.b, 'func_dict', None, TypeError)
from UserDict import UserDict
d = UserDict({'known_attr': 7})
self.cannot_set_attr(self.f.a.im_func, '__dict__', d, TypeError)
self.cannot_set_attr(self.fi.a.im_func, '__dict__', d, TypeError)
示例15: test_plain
# 需要导入模块: import UserDict [as 别名]
# 或者: from UserDict import UserDict [as 别名]
def test_plain(self):
f = self.makeCallable('a, b=1')
self.assertEqualCallArgs(f, '2')
self.assertEqualCallArgs(f, '2, 3')
self.assertEqualCallArgs(f, 'a=2')
self.assertEqualCallArgs(f, 'b=3, a=2')
self.assertEqualCallArgs(f, '2, b=3')
# expand *iterable / **mapping
self.assertEqualCallArgs(f, '*(2,)')
self.assertEqualCallArgs(f, '*[2]')
self.assertEqualCallArgs(f, '*(2, 3)')
self.assertEqualCallArgs(f, '*[2, 3]')
self.assertEqualCallArgs(f, '**{"a":2}')
self.assertEqualCallArgs(f, 'b=3, **{"a":2}')
self.assertEqualCallArgs(f, '2, **{"b":3}')
self.assertEqualCallArgs(f, '**{"b":3, "a":2}')
# expand UserList / UserDict
self.assertEqualCallArgs(f, '*UserList([2])')
self.assertEqualCallArgs(f, '*UserList([2, 3])')
self.assertEqualCallArgs(f, '**UserDict(a=2)')
self.assertEqualCallArgs(f, '2, **UserDict(b=3)')
self.assertEqualCallArgs(f, 'b=2, **UserDict(a=3)')
# unicode keyword args
self.assertEqualCallArgs(f, '**{u"a":2}')
self.assertEqualCallArgs(f, 'b=3, **{u"a":2}')
self.assertEqualCallArgs(f, '2, **{u"b":3}')
self.assertEqualCallArgs(f, '**{u"b":3, u"a":2}')