<s> from test import test_support , mapping_tests <EOL> import UserDict <EOL> d0 = { } <EOL> d1 = { "<STR_LIT>" : <NUM_LIT:1> } <EOL> d2 = { "<STR_LIT>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:2> } <EOL> d3 = { "<STR_LIT>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:3> , "<STR_LIT>" : <NUM_LIT:5> } <EOL> d4 = { "<STR_LIT>" : None , "<STR_LIT>" : None } <EOL> d5 = { "<STR_LIT>" : <NUM_LIT:1> , "<STR_LIT>" : <NUM_LIT:1> } <EOL> class UserDictTest ( mapping_tests . TestHashMappingProtocol ) : <EOL> type2test = UserDict . IterableUserDict <EOL> def test_all ( self ) : <EOL> u = UserDict . UserDict ( ) <EOL> u0 = UserDict . UserDict ( d0 ) <EOL> u1 = UserDict . UserDict ( d1 ) <EOL> u2 = UserDict . IterableUserDict ( d2 ) <EOL> uu = UserDict . UserDict ( u ) <EOL> uu0 = UserDict . UserDict ( u0 ) <EOL> uu1 = UserDict . UserDict ( u1 ) <EOL> uu2 = UserDict . UserDict ( u2 ) <EOL> self . assertEqual ( UserDict . UserDict ( one = <NUM_LIT:1> , two = <NUM_LIT:2> ) , d2 ) <EOL> self . assertEqual ( UserDict . UserDict ( [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:2> ) ] ) , d2 ) <EOL> self . assertEqual ( UserDict . UserDict ( dict = [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:2> ) ] ) , d2 ) <EOL> self . assertEqual ( UserDict . UserDict ( [ ( '<STR_LIT>' , <NUM_LIT:1> ) , ( '<STR_LIT>' , <NUM_LIT:2> ) ] , two = <NUM_LIT:3> , three = <NUM_LIT:5> ) , d3 ) <EOL> self . assertEqual ( UserDict . UserDict . fromkeys ( '<STR_LIT>' . split ( ) ) , d4 ) <EOL> self . assertEqual ( UserDict . UserDict ( ) . fromkeys ( '<STR_LIT>' . split ( ) ) , d4 ) <EOL> self . assertEqual ( UserDict . UserDict . fromkeys ( '<STR_LIT>' . split ( ) , <NUM_LIT:1> ) , d5 ) <EOL> self . assertEqual ( UserDict . UserDict ( ) . fromkeys ( '<STR_LIT>' . split ( ) , <NUM_LIT:1> ) , d5 ) <EOL> self . assertTrue ( u1 . fromkeys ( '<STR_LIT>' . split ( ) ) is not u1 ) <EOL> self . assertIsInstance ( u1 . fromkeys ( '<STR_LIT>' . split ( ) ) , UserDict . UserDict ) <EOL> self . assertIsInstance ( u2 . fromkeys ( '<STR_LIT>' . split ( ) ) , UserDict . IterableUserDict ) <EOL> self . assertEqual ( str ( u0 ) , str ( d0 ) ) <EOL> self . assertEqual ( repr ( u1 ) , repr ( d1 ) ) <EOL> self . assertEqual ( repr ( u2 ) , repr ( d2 ) ) <EOL> all = [ d0 , d1 , d2 , u , u0 , u1 , u2 , uu , uu0 , uu1 , uu2 ] <EOL> for a in all : <EOL> for b in all : <EOL> self . assertEqual ( cmp ( a , b ) , cmp ( len ( a ) , len ( b ) ) ) <EOL> self . assertEqual ( u2 [ "<STR_LIT>" ] , <NUM_LIT:1> ) <EOL> self . assertRaises ( KeyError , u1 . __getitem__ , "<STR_LIT>" ) <EOL> u3 = UserDict . UserDict ( u2 ) <EOL> u3 [ "<STR_LIT>" ] = <NUM_LIT:2> <EOL> u3 [ "<STR_LIT>" ] = <NUM_LIT:3> <EOL> del u3 [ "<STR_LIT>" ] <EOL> self . assertRaises ( KeyError , u3 . __delitem__ , "<STR_LIT>" ) <EOL> u3 . clear ( ) <EOL> self . assertEqual ( u3 , { } ) <EOL> u2a = u2 . copy ( ) <EOL> self . assertEqual ( u2a , u2 ) <EOL> u2b = UserDict . UserDict ( x = <NUM_LIT> , y = <NUM_LIT> ) <EOL> u2c = u2b . copy ( ) <EOL> self . assertEqual ( u2b , u2c ) <EOL> class MyUserDict ( UserDict . UserDict ) : <EOL> def display ( self ) : print self <EOL> m2 = MyUserDict ( u2 ) <EOL> m2a = m2 . copy ( ) <EOL> self . assertEqual ( m2a , m2 ) <EOL> m2 [ '<STR_LIT:foo>' ] = '<STR_LIT:bar>' <EOL> self . assertNotEqual ( m2a , m2 ) <EOL> self . assertEqual ( u2 . keys ( ) , d2 . keys ( ) ) <EOL> self . assertEqual ( u2 . items ( ) , d2 . items ( ) ) <EOL> self . assertEqual ( u2 . values ( ) , d2 . values ( ) ) <EOL> for i in u2 . keys ( ) : <EOL> self . assertIn ( i , u2 ) <EOL> self . assertEqual ( i in u1 , i in d1 ) <EOL> self . assertEqual ( i in u0 , i in d0 ) <EOL> with test_support . check_py3k_warnings ( ) : <EOL> self . assertTrue ( u2 . has_key ( i ) ) <EOL> self . assertEqual ( u1 . has_key ( i ) , d1 . has_key ( i ) ) <EOL> self . assertEqual ( u0 . has_key ( i ) , d0 . has_key ( i ) ) <EOL> t = UserDict . UserDict ( ) <EOL> t . update ( u2 ) <EOL> self . assertEqual ( t , u2 ) <EOL> class Items : <EOL> def items ( self ) : <EOL> return ( ( "<STR_LIT:x>" , <NUM_LIT> ) , ( "<STR_LIT:y>" , <NUM_LIT> ) ) <EOL> t = UserDict . UserDict ( ) <EOL> t . update ( Items ( ) ) <EOL> self . assertEqual ( t , { "<STR_LIT:x>" : <NUM_LIT> , "<STR_LIT:y>" : <NUM_LIT> } ) <EOL> for i in u2 . keys ( ) : <EOL> self . assertEqual ( u2 . get ( i ) , u2 [ i ] ) <EOL> self . assertEqual ( u1 . get ( i ) , d1 . get ( i ) ) <EOL> self . assertEqual ( u0 . get ( i ) , d0 . get ( i ) ) <EOL> for i in xrange ( <NUM_LIT:20> ) : <EOL> u2 [ i ] = str ( i ) <EOL> ikeys = [ ] <EOL> for k in u2 : <EOL> ikeys . append ( k ) <EOL> keys = u2 . keys ( ) <EOL> self . assertEqual ( set ( ikeys ) , set ( keys ) ) <EOL> t = UserDict . UserDict ( ) <EOL> self . assertEqual ( t . setdefault ( "<STR_LIT:x>" , <NUM_LIT> ) , <NUM_LIT> ) <EOL> self . assertTrue ( t . has_key ( "<STR_LIT:x>" ) ) <EOL> self . assertEqual ( t . setdefault ( "<STR_LIT:x>" , <NUM_LIT> ) , <NUM_LIT> ) <EOL> t = UserDict . UserDict ( x = <NUM_LIT> ) <EOL> self . assertEqual ( t . pop ( "<STR_LIT:x>" ) , <NUM_LIT> ) <EOL> self . assertRaises ( KeyError , t . pop , "<STR_LIT:x>" ) <EOL> self . assertEqual ( t . pop ( "<STR_LIT:x>" , <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> t [ "<STR_LIT:x>" ] = <NUM_LIT> <EOL> self . assertEqual ( t . pop ( "<STR_LIT:x>" , <NUM_LIT:1> ) , <NUM_LIT> ) <EOL> t = UserDict . UserDict ( x = <NUM_LIT> ) <EOL> self . assertEqual ( t . popitem ( ) , ( "<STR_LIT:x>" , <NUM_LIT> ) ) <EOL> self . assertRaises ( KeyError , t . popitem ) <EOL> def test_missing ( self ) : <EOL> self . assertEqual ( hasattr ( UserDict , "<STR_LIT>" ) , False ) <EOL> class D ( UserDict . UserDict ) : <EOL> def __missing__ ( self , key ) : <EOL> return <NUM_LIT> <EOL> d = D ( { <NUM_LIT:1> : <NUM_LIT:2> , <NUM_LIT:3> : <NUM_LIT:4> } ) <EOL> self . assertEqual ( d [ <NUM_LIT:1> ] , <NUM_LIT:2> ) <EOL> self . assertEqual ( d [ <NUM_LIT:3> ] , <NUM_LIT:4> ) <EOL> self . assertNotIn ( <NUM_LIT:2> , d ) <EOL> self . assertNotIn ( <NUM_LIT:2> , d . keys ( ) ) <EOL> self . assertEqual ( d [ <NUM_LIT:2> ] , <NUM_LIT> ) <EOL> class E ( UserDict . UserDict ) : <EOL> def __missing__ ( self , key ) : <EOL> raise RuntimeError ( key ) <EOL> e = E ( ) <EOL> try : <EOL> e [ <NUM_LIT> ] <EOL> except RuntimeError , err : <EOL> self . assertEqual ( err . args , ( <NUM_LIT> , ) ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> class F ( UserDict . UserDict ) : <EOL> def __init__ ( self ) : <EOL> self . __missing__ = lambda key : None <EOL> UserDict . UserDict . __init__ ( self ) <EOL> f = F ( ) <EOL> try : <EOL> f [ <NUM_LIT> ] <EOL> except KeyError , err : <EOL> self . assertEqual ( err . args , ( <NUM_LIT> , ) ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> class G ( UserDict . UserDict ) : <EOL> pass <EOL> g = G ( ) <EOL> try : <EOL> g [ <NUM_LIT> ] <EOL> except KeyError , err : <EOL> self . assertEqual ( err . args , ( <NUM_LIT> , ) ) <EOL> else : <EOL> self . fail ( "<STR_LIT>" ) <EOL> class SeqDict ( UserDict . DictMixin ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , other = None , ** kwargs ) : <EOL> self . keylist = [ ] <EOL> self . valuelist = [ ] <EOL> if other is not None : <EOL> for ( key , value ) in other : <EOL> self [ key ] = value <EOL> for ( key , value ) in kwargs . iteritems ( ) : <EOL> self [ key ] = value <EOL> def __getitem__ ( self , key ) : <EOL> try : <EOL> i = self . keylist . index ( key ) <EOL> except ValueError : <EOL> raise KeyError <EOL> return self . valuelist [ i ] <EOL> def __setitem__ ( self , key , value ) : <EOL> try : <EOL> i = self . keylist . index ( key ) <EOL> self . valuelist [ i ] = value <EOL> except ValueError : <EOL> self . keylist . append ( key ) <EOL> self . valuelist . append ( value ) <EOL> def __delitem__ ( self , key ) : <EOL> try : <EOL> i = self . keylist . index ( key ) <EOL> except ValueError : <EOL> raise KeyError <EOL> self . keylist . pop ( i ) <EOL> self . valuelist . pop ( i ) <EOL> def keys ( self ) : <EOL> return list ( self . keylist ) <EOL> def copy ( self ) : <EOL> d = self . __class__ ( ) <EOL> for key , value in self . iteritems ( ) : <EOL> d [ key ] = value <EOL> return d <EOL> @ classmethod <EOL> def fromkeys ( cls , keys , value = None ) : <EOL> d = cls ( ) <EOL> for key in keys : <EOL> d [ key ] = value <EOL> return d <EOL> class UserDictMixinTest ( mapping_tests . TestMappingProtocol ) : <EOL> type2test = SeqDict <EOL> def test_all ( self ) : <EOL> s = SeqDict ( ) <EOL> s [ <NUM_LIT:10> ] = '<STR_LIT>' <EOL> s [ <NUM_LIT:20> ] = '<STR_LIT>' <EOL> s [ <NUM_LIT:30> ] = '<STR_LIT>' <EOL> del s [ <NUM_LIT:20> ] <EOL> self . assertEqual ( s [ <NUM_LIT:10> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( s . keys ( ) , [ <NUM_LIT:10> , <NUM_LIT:30> ] ) <EOL> self . assertTrue ( s . has_key ( <NUM_LIT:10> ) ) <EOL> self . assertTrue ( not s . has_key ( <NUM_LIT:20> ) ) <EOL> self . assertIn ( <NUM_LIT:10> , s ) <EOL> self . assertNotIn ( <NUM_LIT:20> , s ) <EOL> self . assertEqual ( [ k for k in s ] , [ <NUM_LIT:10> , <NUM_LIT:30> ] ) <EOL> self . assertEqual ( len ( s ) , <NUM_LIT:2> ) <EOL> self . assertEqual ( list ( s . iteritems ( ) ) , [ ( <NUM_LIT:10> , '<STR_LIT>' ) , ( <NUM_LIT:30> , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( list ( s . iterkeys ( ) ) , [ <NUM_LIT:10> , <NUM_LIT:30> ] ) <EOL> self . assertEqual ( list ( s . itervalues ( ) ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( s . values ( ) , [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> self . assertEqual ( s . items ( ) , [ ( <NUM_LIT:10> , '<STR_LIT>' ) , ( <NUM_LIT:30> , '<STR_LIT>' ) ] ) <EOL> self . assertEqual ( s . get ( <NUM_LIT:10> ) , '<STR_LIT>' ) <EOL> self . assertEqual ( s . get ( <NUM_LIT:15> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( s . get ( <NUM_LIT:15> ) , None ) <EOL> self . assertEqual ( s . setdefault ( <NUM_LIT> , '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> self . assertEqual ( s . setdefault ( <NUM_LIT:10> , '<STR_LIT:null>' ) , '<STR_LIT>' ) <EOL> del s [ <NUM_LIT> ] <EOL> self . assertEqual ( s . pop ( <NUM_LIT:10> ) , '<STR_LIT>' ) <EOL> self . assertNotIn ( <NUM_LIT:10> , s ) <EOL> s [ <NUM_LIT:10> ] = '<STR_LIT>' <EOL> self . assertEqual ( s . pop ( "<STR_LIT:x>" , <NUM_LIT:1> ) , <NUM_LIT:1> ) <EOL> s [ "<STR_LIT:x>" ] = <NUM_LIT> <EOL> self . assertEqual ( s . pop ( "<STR_LIT:x>" , <NUM_LIT:1> ) , <NUM_LIT> ) <EOL> k , v = s . popitem ( ) <EOL> self . assertNotIn ( k , s ) <EOL> s [ k ] = v <EOL> s . clear ( ) <EOL> self . assertEqual ( len ( s ) , <NUM_LIT:0> ) <EOL> self . assertRaises ( KeyError , s . popitem ) <EOL> s . update ( { <NUM_LIT:10> : '<STR_LIT>' , <NUM_LIT:20> : '<STR_LIT>' } ) <EOL> self . assertEqual ( s [ <NUM_LIT:10> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( s [ <NUM_LIT:20> ] , '<STR_LIT>' ) <EOL> self . assertEqual ( s , { <NUM_LIT:10> : '<STR_LIT>' , <NUM_LIT:20> : '<STR_LIT>' } ) <EOL> t = SeqDict ( ) <EOL> t [ <NUM_LIT:20> ] = '<STR_LIT>' <EOL> t [ <NUM_LIT:10> ] = '<STR_LIT>' <EOL> self . assertEqual ( s , t ) <EOL> def test_main ( ) : <EOL> test_support . run_unittest ( <EOL> UserDictTest , <EOL> UserDictMixinTest <EOL> ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> test_main ( ) </s>
