我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用UserDict.UserDict()。
def objdict(orig): return UserDict(orig) #class objdict(dict): # def __init__(self,initial={}): # for k,v in initial.items(): # self[k] = v # # def __getattr__(self, name): # if name in self: # return self[name] # else: # raise AttributeError("No such attribute: " + name) # # def __setattr__(self, name, value): # self[name] = value # # def __delattr__(self, name): # if name in self: # del self[name] # else: # raise AttributeError("No such attribute: " + name)
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)
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,), {})
def __getattr__(self, name): """ override the class attribute get method. Return the value from the Userdict """ try: return self.data[name] except KeyError: error = 'Attribute key error: %s' %(name) raise PBSError(error) ## Disabled for this moment, BvdV 16 July 2010 # #def __setattr__(self, name, value): # """ # override the class attribute set method only when the UserDict # has set its class attribute # """ # if self.__dict__.has_key('data'): # self.data[name] = value # else: # self.__dict__[name] = value
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)
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
def __init__(self, *args, **kw): 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)
def __init__(self,data=None,size=100): UserDict.UserDict.__init__(self,data) self.size = size
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__)
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)
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)
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)
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}')
def test_varkw(self): f = self.makeCallable('a, b=1, **c') self.assertEqualCallArgs(f, 'a=2') self.assertEqualCallArgs(f, '2, b=3, c=4') self.assertEqualCallArgs(f, 'b=3, a=2, c=4') self.assertEqualCallArgs(f, 'c=4, **{"a":2, "b":3}') self.assertEqualCallArgs(f, '2, c=4, **{"b":3}') self.assertEqualCallArgs(f, 'b=2, **{"a":3, "c":4}') self.assertEqualCallArgs(f, '**UserDict(a=2, b=3, c=4)') self.assertEqualCallArgs(f, '2, c=4, **UserDict(b=3)') self.assertEqualCallArgs(f, 'b=2, **UserDict(a=3, c=4)') # unicode keyword args self.assertEqualCallArgs(f, 'c=4, **{u"a":2, u"b":3}') self.assertEqualCallArgs(f, '2, c=4, **{u"b":3}') self.assertEqualCallArgs(f, 'b=2, **{u"a":3, u"c":4}')
def test_multiple_features(self): f = self.makeCallable('a, b=2, (c,(d,e))=(3,[4,5]), *f, **g') self.assertEqualCallArgs(f, '2, 3, (4,[5,6]), 7') self.assertEqualCallArgs(f, '2, 3, *[(4,[5,6]), 7], x=8') self.assertEqualCallArgs(f, '2, 3, x=8, *[(4,[5,6]), 7]') self.assertEqualCallArgs(f, '2, x=8, *[3, (4,[5,6]), 7], y=9') self.assertEqualCallArgs(f, 'x=8, *[2, 3, (4,[5,6])], y=9') self.assertEqualCallArgs(f, 'x=8, *UserList([2, 3, (4,[5,6])]), ' '**{"y":9, "z":10}') self.assertEqualCallArgs(f, '2, x=8, *UserList([3, (4,[5,6])]), ' '**UserDict(y=9, z=10)')