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
@@ -119,13 +118,9 @@ def lookup_iregex(data, rhs):
119118}
120119
121120
122- @dataclasses .dataclass (eq = False )
123- class ListQuery (Generic [T ]):
121+ class ListQuery (list [T ]):
124122 """Filter a list of dicts. *Experimental and unstable*.
125123
126- :py:func:`dataclasses.dataclass` is only used for ``__repr__`` and pytest comparison
127- details.
128-
129124 >>> query = ListQuery(
130125 ... [
131126 ... {
@@ -142,44 +137,35 @@ class ListQuery(Generic[T]):
142137 ... },
143138 ... ]
144139 ... )
145- >>> query.filter(place="Chicago suburbs").data [0]['city']
140+ >>> query.filter(place="Chicago suburbs")[0]['city']
146141 'Elmhurst'
147- >>> query.filter(place__icontains="chicago").data [0]['city']
142+ >>> query.filter(place__icontains="chicago")[0]['city']
148143 'Elmhurst'
149- >>> query.filter(foods__breakfast="waffles").data [0]['city']
144+ >>> query.filter(foods__breakfast="waffles")[0]['city']
150145 'Elmhurst'
151- >>> query.filter(foods__fruit__in="cantelope").data [0]['city']
146+ >>> query.filter(foods__fruit__in="cantelope")[0]['city']
152147 'Elmhurst'
153- >>> query.filter(foods__fruit__in="orange").data [0]['city']
148+ >>> query.filter(foods__fruit__in="orange")[0]['city']
154149 'Tampa'
155150 """
156151
157- __slots__ = ("data" , "pk_key" )
158152 data : Sequence [T ]
159153
160- # def __init__(self, data, pk_key: Optional[str] = None):
161- # self.data: Sequence[T] = data
162- # #: Primary key for objects, optional.
163- # #: Use for .get(), .items()
164- # self.pk_key: Optional[Any] = pk_key
165-
166154 def items (self ):
167155 data : Sequence [T ]
168156
169157 if self .pk_key is None :
170158 raise Exception ("items() require a pk_key exists" )
171- return [(getattr (item , self .pk_key ), item ) for item in self . data ]
159+ return [(getattr (item , self .pk_key ), item ) for item in self ]
172160
173161 def __eq__ (self , other ):
174162 data = other
175- if hasattr (data , "data" ):
176- data = getattr (data , "data" )
177163
178- if not isinstance (self . data , list ) or not isinstance (data , list ):
164+ if not isinstance (self , list ) or not isinstance (data , list ):
179165 return False
180166
181- if len (self . data ) == len (data ):
182- for (a , b ) in zip (self . data , data ):
167+ if len (self ) == len (data ):
168+ for (a , b ) in zip (self , data ):
183169 if isinstance (a , dict ):
184170 a_keys = a .keys ()
185171 if a .keys == b .keys ():
@@ -228,4 +214,4 @@ def val_match(obj):
228214 else :
229215 _filter = filter_lookup
230216
231- return self .__class__ (data = [ k for k in self . data if _filter (k )] )
217+ return self .__class__ (k for k in self if _filter (k ))
0 commit comments