@@ -17,12 +17,6 @@ class istr(str):
1717upstr = istr # for relaxing backward compatibility problems
1818
1919
20- def getversion (md ):
21- if not isinstance (md , _Base ):
22- raise TypeError ("Parameter should be multidict or proxy" )
23- return md ._impl ._version
24-
25-
2620_version = array ("Q" , [0 ])
2721
2822
@@ -45,6 +39,105 @@ def __sizeof__(self):
4539 return object .__sizeof__ (self ) + sys .getsizeof (self ._items )
4640
4741
42+ class _Iter :
43+ __slots__ = ("_size" , "_iter" )
44+
45+ def __init__ (self , size , iterator ):
46+ self ._size = size
47+ self ._iter = iterator
48+
49+ def __iter__ (self ):
50+ return self
51+
52+ def __next__ (self ):
53+ return next (self ._iter )
54+
55+ def __length_hint__ (self ):
56+ return self ._size
57+
58+
59+ class _ViewBase :
60+ def __init__ (self , impl ):
61+ self ._impl = impl
62+
63+ def __len__ (self ):
64+ return len (self ._impl ._items )
65+
66+
67+ class _ItemsView (_ViewBase , abc .ItemsView ):
68+ def __contains__ (self , item ):
69+ if not isinstance (item , (tuple , list )) or len (item ) != 2 :
70+ return False
71+ for i , k , v in self ._impl ._items :
72+ if item [0 ] == k and item [1 ] == v :
73+ return True
74+ return False
75+
76+ def __iter__ (self ):
77+ return _Iter (len (self ), self ._iter (self ._impl ._version ))
78+
79+ def _iter (self , version ):
80+ for i , k , v in self ._impl ._items :
81+ if version != self ._impl ._version :
82+ raise RuntimeError ("Dictionary changed during iteration" )
83+ yield k , v
84+
85+ def __repr__ (self ):
86+ lst = []
87+ for item in self ._impl ._items :
88+ lst .append ("{!r}: {!r}" .format (item [1 ], item [2 ]))
89+ body = ", " .join (lst )
90+ return "{}({})" .format (self .__class__ .__name__ , body )
91+
92+
93+ class _ValuesView (_ViewBase , abc .ValuesView ):
94+ def __contains__ (self , value ):
95+ for item in self ._impl ._items :
96+ if item [2 ] == value :
97+ return True
98+ return False
99+
100+ def __iter__ (self ):
101+ return _Iter (len (self ), self ._iter (self ._impl ._version ))
102+
103+ def _iter (self , version ):
104+ for item in self ._impl ._items :
105+ if version != self ._impl ._version :
106+ raise RuntimeError ("Dictionary changed during iteration" )
107+ yield item [2 ]
108+
109+ def __repr__ (self ):
110+ lst = []
111+ for item in self ._impl ._items :
112+ lst .append ("{!r}" .format (item [2 ]))
113+ body = ", " .join (lst )
114+ return "{}({})" .format (self .__class__ .__name__ , body )
115+
116+
117+ class _KeysView (_ViewBase , abc .KeysView ):
118+ def __contains__ (self , key ):
119+ for item in self ._impl ._items :
120+ if item [1 ] == key :
121+ return True
122+ return False
123+
124+ def __iter__ (self ):
125+ return _Iter (len (self ), self ._iter (self ._impl ._version ))
126+
127+ def _iter (self , version ):
128+ for item in self ._impl ._items :
129+ if version != self ._impl ._version :
130+ raise RuntimeError ("Dictionary changed during iteration" )
131+ yield item [1 ]
132+
133+ def __repr__ (self ):
134+ lst = []
135+ for item in self ._impl ._items :
136+ lst .append ("{!r}" .format (item [1 ]))
137+ body = ", " .join (lst )
138+ return "{}({})" .format (self .__class__ .__name__ , body )
139+
140+
48141class _Base :
49142 def _title (self , key ):
50143 return key
@@ -138,46 +231,6 @@ def __repr__(self):
138231 __class_getitem__ = classmethod (GenericAlias )
139232
140233
141- class MultiDictProxy (_Base , MultiMapping ):
142- """Read-only proxy for MultiDict instance."""
143-
144- def __init__ (self , arg ):
145- if not isinstance (arg , (MultiDict , MultiDictProxy )):
146- raise TypeError (
147- "ctor requires MultiDict or MultiDictProxy instance"
148- ", not {}" .format (type (arg ))
149- )
150-
151- self ._impl = arg ._impl
152-
153- def __reduce__ (self ):
154- raise TypeError ("can't pickle {} objects" .format (self .__class__ .__name__ ))
155-
156- def copy (self ):
157- """Return a copy of itself."""
158- return MultiDict (self .items ())
159-
160-
161- class CIMultiDictProxy (MultiDictProxy ):
162- """Read-only proxy for CIMultiDict instance."""
163-
164- def __init__ (self , arg ):
165- if not isinstance (arg , (CIMultiDict , CIMultiDictProxy )):
166- raise TypeError (
167- "ctor requires CIMultiDict or CIMultiDictProxy instance"
168- ", not {}" .format (type (arg ))
169- )
170-
171- self ._impl = arg ._impl
172-
173- def _title (self , key ):
174- return key .title ()
175-
176- def copy (self ):
177- """Return a copy of itself."""
178- return CIMultiDict (self .items ())
179-
180-
181234class MultiDict (_Base , MutableMultiMapping ):
182235 """Dictionary with the support for duplicate keys."""
183236
@@ -422,100 +475,47 @@ def _title(self, key):
422475 return key .title ()
423476
424477
425- class _Iter :
426- __slots__ = ("_size" , "_iter" )
427-
428- def __init__ (self , size , iterator ):
429- self ._size = size
430- self ._iter = iterator
431-
432- def __iter__ (self ):
433- return self
434-
435- def __next__ (self ):
436- return next (self ._iter )
437-
438- def __length_hint__ (self ):
439- return self ._size
440-
441-
442- class _ViewBase :
443- def __init__ (self , impl ):
444- self ._impl = impl
445-
446- def __len__ (self ):
447- return len (self ._impl ._items )
448-
449-
450- class _ItemsView (_ViewBase , abc .ItemsView ):
451- def __contains__ (self , item ):
452- if not isinstance (item , (tuple , list )) or len (item ) != 2 :
453- return False
454- for i , k , v in self ._impl ._items :
455- if item [0 ] == k and item [1 ] == v :
456- return True
457- return False
458-
459- def __iter__ (self ):
460- return _Iter (len (self ), self ._iter (self ._impl ._version ))
478+ class MultiDictProxy (_Base , MultiMapping ):
479+ """Read-only proxy for MultiDict instance."""
461480
462- def _iter (self , version ):
463- for i , k , v in self ._impl ._items :
464- if version != self ._impl ._version :
465- raise RuntimeError ("Dictionary changed during iteration" )
466- yield k , v
481+ def __init__ (self , arg ):
482+ if not isinstance (arg , (MultiDict , MultiDictProxy )):
483+ raise TypeError (
484+ "ctor requires MultiDict or MultiDictProxy instance"
485+ ", not {}" .format (type (arg ))
486+ )
467487
468- def __repr__ (self ):
469- lst = []
470- for item in self ._impl ._items :
471- lst .append ("{!r}: {!r}" .format (item [1 ], item [2 ]))
472- body = ", " .join (lst )
473- return "{}({})" .format (self .__class__ .__name__ , body )
488+ self ._impl = arg ._impl
474489
490+ def __reduce__ (self ):
491+ raise TypeError ("can't pickle {} objects" .format (self .__class__ .__name__ ))
475492
476- class _ValuesView (_ViewBase , abc .ValuesView ):
477- def __contains__ (self , value ):
478- for item in self ._impl ._items :
479- if item [2 ] == value :
480- return True
481- return False
493+ def copy (self ):
494+ """Return a copy of itself."""
495+ return MultiDict (self .items ())
482496
483- def __iter__ (self ):
484- return _Iter (len (self ), self ._iter (self ._impl ._version ))
485497
486- def _iter (self , version ):
487- for item in self ._impl ._items :
488- if version != self ._impl ._version :
489- raise RuntimeError ("Dictionary changed during iteration" )
490- yield item [2 ]
498+ class CIMultiDictProxy (MultiDictProxy ):
499+ """Read-only proxy for CIMultiDict instance."""
491500
492- def __repr__ (self ):
493- lst = []
494- for item in self . _impl . _items :
495- lst . append ( "{!r}" . format ( item [ 2 ]))
496- body = ", " . join ( lst )
497- return "{}({})" . format ( self . __class__ . __name__ , body )
501+ def __init__ (self , arg ):
502+ if not isinstance ( arg , ( CIMultiDict , CIMultiDictProxy )):
503+ raise TypeError (
504+ "ctor requires CIMultiDict or CIMultiDictProxy instance"
505+ ", not {}" . format ( type ( arg ) )
506+ )
498507
508+ self ._impl = arg ._impl
499509
500- class _KeysView (_ViewBase , abc .KeysView ):
501- def __contains__ (self , key ):
502- for item in self ._impl ._items :
503- if item [1 ] == key :
504- return True
505- return False
510+ def _title (self , key ):
511+ return key .title ()
506512
507- def __iter__ (self ):
508- return _Iter (len (self ), self ._iter (self ._impl ._version ))
513+ def copy (self ):
514+ """Return a copy of itself."""
515+ return CIMultiDict (self .items ())
509516
510- def _iter (self , version ):
511- for item in self ._impl ._items :
512- if version != self ._impl ._version :
513- raise RuntimeError ("Dictionary changed during iteration" )
514- yield item [1 ]
515517
516- def __repr__ (self ):
517- lst = []
518- for item in self ._impl ._items :
519- lst .append ("{!r}" .format (item [1 ]))
520- body = ", " .join (lst )
521- return "{}({})" .format (self .__class__ .__name__ , body )
518+ def getversion (md ):
519+ if not isinstance (md , _Base ):
520+ raise TypeError ("Parameter should be multidict or proxy" )
521+ return md ._impl ._version
0 commit comments