mirror of
				https://github.com/python/cpython.git
				synced 2025-11-04 03:44:55 +00:00 
			
		
		
		
	Bring UserDict in-sync with changes to dict.
Constructor accepts optional keyword arguments after a optional items list. Add fromkeys() as an alternate constructor from an iterable over keys. Expand related unittests.
This commit is contained in:
		
							parent
							
								
									e33d3df030
								
							
						
					
					
						commit
						e4827eb2a2
					
				
					 2 changed files with 25 additions and 6 deletions
				
			
		| 
						 | 
					@ -2,13 +2,13 @@
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class UserDict:
 | 
					class UserDict:
 | 
				
			||||||
    def __init__(self, dict=None, **kwargs):
 | 
					    def __init__(self, dict=None, **kwargs):
 | 
				
			||||||
        self.data = kwargs              # defaults to {}
 | 
					        self.data = {}
 | 
				
			||||||
        if dict is not None:
 | 
					        if dict is not None:
 | 
				
			||||||
            if hasattr(dict,'keys'):    # handle mapping (possibly a UserDict)
 | 
					            if not hasattr(dict,'keys'):
 | 
				
			||||||
 | 
					                dict = type({})(dict)   # make mapping from a sequence
 | 
				
			||||||
            self.update(dict)
 | 
					            self.update(dict)
 | 
				
			||||||
            else:                       # handle sequence
 | 
					        if len(kwargs):
 | 
				
			||||||
                DICT = type({})  # because builtin dict is locally overridden
 | 
					            self.update(kwargs)
 | 
				
			||||||
                self.data.update(DICT(dict))
 | 
					 | 
				
			||||||
    def __repr__(self): return repr(self.data)
 | 
					    def __repr__(self): return repr(self.data)
 | 
				
			||||||
    def __cmp__(self, dict):
 | 
					    def __cmp__(self, dict):
 | 
				
			||||||
        if isinstance(dict, UserDict):
 | 
					        if isinstance(dict, UserDict):
 | 
				
			||||||
| 
						 | 
					@ -61,6 +61,12 @@ class UserDict:
 | 
				
			||||||
        return self.data.popitem()
 | 
					        return self.data.popitem()
 | 
				
			||||||
    def __contains__(self, key):
 | 
					    def __contains__(self, key):
 | 
				
			||||||
        return key in self.data
 | 
					        return key in self.data
 | 
				
			||||||
 | 
					    def fromkeys(cls, iterable, value=None):
 | 
				
			||||||
 | 
					        d = cls()
 | 
				
			||||||
 | 
					        for key in iterable:
 | 
				
			||||||
 | 
					            d[key] = value
 | 
				
			||||||
 | 
					        return d
 | 
				
			||||||
 | 
					    fromkeys = classmethod(fromkeys)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
class IterableUserDict(UserDict):
 | 
					class IterableUserDict(UserDict):
 | 
				
			||||||
    def __iter__(self):
 | 
					    def __iter__(self):
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -6,6 +6,9 @@ from UserDict import UserDict, IterableUserDict
 | 
				
			||||||
d0 = {}
 | 
					d0 = {}
 | 
				
			||||||
d1 = {"one": 1}
 | 
					d1 = {"one": 1}
 | 
				
			||||||
d2 = {"one": 1, "two": 2}
 | 
					d2 = {"one": 1, "two": 2}
 | 
				
			||||||
 | 
					d3 = {"one": 1, "two": 3, "three": 5}
 | 
				
			||||||
 | 
					d4 = {"one": None, "two": None}
 | 
				
			||||||
 | 
					d5 = {"one": 1, "two": 1}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Test constructors
 | 
					# Test constructors
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					@ -21,6 +24,16 @@ uu2 = UserDict(u2)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
verify(UserDict(one=1, two=2) == d2)            # keyword arg constructor
 | 
					verify(UserDict(one=1, two=2) == d2)            # keyword arg constructor
 | 
				
			||||||
verify(UserDict([('one',1), ('two',2)]) == d2)  # item sequence constructor
 | 
					verify(UserDict([('one',1), ('two',2)]) == d2)  # item sequence constructor
 | 
				
			||||||
 | 
					verify(UserDict(dict=[('one',1), ('two',2)]) == d2)
 | 
				
			||||||
 | 
					verify(UserDict([('one',1), ('two',2)], two=3, three=5) == d3) # both together
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					verify(UserDict.fromkeys('one two'.split()) == d4)  # alternate constructor
 | 
				
			||||||
 | 
					verify(UserDict().fromkeys('one two'.split()) == d4)
 | 
				
			||||||
 | 
					verify(UserDict.fromkeys('one two'.split(), 1) == d5)
 | 
				
			||||||
 | 
					verify(UserDict().fromkeys('one two'.split(), 1) == d5)
 | 
				
			||||||
 | 
					verify(u1.fromkeys('one two'.split()) is not u1)
 | 
				
			||||||
 | 
					verify(isinstance(u1.fromkeys('one two'.split()), UserDict))
 | 
				
			||||||
 | 
					verify(isinstance(u2.fromkeys('one two'.split()), IterableUserDict))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
# Test __repr__
 | 
					# Test __repr__
 | 
				
			||||||
 | 
					
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue