1- import dataclasses
21import re
32import traceback
4- from typing import Any ,Callable ,Generic , Optional ,Protocol ,Sequence ,TypeVar ,Union
3+ from typing import Any ,Callable ,Optional ,Protocol ,Sequence ,TypeVar ,Union
54
65T = TypeVar ("T" ,Any ,Any )
76
@@ -121,13 +120,9 @@ def lookup_iregex(data, rhs):
121120}
122121
123122
124- @dataclasses .dataclass (eq = False )
125- class QueryList (Generic [T ]):
123+ class QueryList (list [T ]):
126124"""Filter list of object/dicts. For small, local datasets. *Experimental, unstable*.
127125
128- :py:func:`dataclasses.dataclass` is only used for ``__repr__`` and pytest comparison
129- details.
130-
131126 >>> query = QueryList(
132127 ... [
133128 ... {
@@ -144,44 +139,35 @@ class QueryList(Generic[T]):
144139 ... },
145140 ... ]
146141 ... )
147- >>> query.filter(place="Chicago suburbs").data [0]['city']
142+ >>> query.filter(place="Chicago suburbs")[0]['city']
148143 'Elmhurst'
149- >>> query.filter(place__icontains="chicago").data [0]['city']
144+ >>> query.filter(place__icontains="chicago")[0]['city']
150145 'Elmhurst'
151- >>> query.filter(foods__breakfast="waffles").data [0]['city']
146+ >>> query.filter(foods__breakfast="waffles")[0]['city']
152147 'Elmhurst'
153- >>> query.filter(foods__fruit__in="cantelope").data [0]['city']
148+ >>> query.filter(foods__fruit__in="cantelope")[0]['city']
154149 'Elmhurst'
155- >>> query.filter(foods__fruit__in="orange").data [0]['city']
150+ >>> query.filter(foods__fruit__in="orange")[0]['city']
156151 'Tampa'
157152 """
158153
159- __slots__ = ("data" ,"pk_key" )
160154data :Sequence [T ]
161155
162- # def __init__(self, data, pk_key: Optional[str] = None):
163- # self.data: Sequence[T] = data
164- # #: Primary key for objects, optional.
165- # #: Use for .get(), .items()
166- # self.pk_key: Optional[Any] = pk_key
167-
168156def items (self ):
169157data :Sequence [T ]
170158
171159if self .pk_key is None :
172160raise Exception ("items() require a pk_key exists" )
173- return [(getattr (item ,self .pk_key ),item )for item in self . data ]
161+ return [(getattr (item ,self .pk_key ),item )for item in self ]
174162
175163def __eq__ (self ,other ):
176164data = other
177- if hasattr (data ,"data" ):
178- data = getattr (data ,"data" )
179165
180- if not isinstance (self . data ,list )or not isinstance (data ,list ):
166+ if not isinstance (self ,list )or not isinstance (data ,list ):
181167return False
182168
183- if len (self . data )== len (data ):
184- for (a ,b )in zip (self . data ,data ):
169+ if len (self )== len (data ):
170+ for (a ,b )in zip (self ,data ):
185171if isinstance (a ,dict ):
186172a_keys = a .keys ()
187173if a .keys == b .keys ():
@@ -230,4 +216,4 @@ def val_match(obj):
230216else :
231217_filter = filter_lookup
232218
233- return self .__class__ (data = [ k for k in self . data if _filter (k )] )
219+ return self .__class__ (k for k in self if _filter (k ))