1
- import dataclasses
2
1
import re
3
2
import traceback
4
- from typing import Any , Callable , Generic , Optional , Protocol , Sequence , TypeVar , Union
3
+ from typing import Any , Callable , Optional , Protocol , Sequence , TypeVar , Union
5
4
6
5
T = TypeVar ("T" , Any , Any )
7
6
@@ -119,13 +118,9 @@ def lookup_iregex(data, rhs):
119
118
}
120
119
121
120
122
- @dataclasses .dataclass (eq = False )
123
- class ListQuery (Generic [T ]):
121
+ class ListQuery (list [T ]):
124
122
"""Filter a list of dicts. *Experimental and unstable*.
125
123
126
- :py:func:`dataclasses.dataclass` is only used for ``__repr__`` and pytest comparison
127
- details.
128
-
129
124
>>> query = ListQuery(
130
125
... [
131
126
... {
@@ -142,44 +137,35 @@ class ListQuery(Generic[T]):
142
137
... },
143
138
... ]
144
139
... )
145
- >>> query.filter(place="Chicago suburbs").data [0]['city']
140
+ >>> query.filter(place="Chicago suburbs")[0]['city']
146
141
'Elmhurst'
147
- >>> query.filter(place__icontains="chicago").data [0]['city']
142
+ >>> query.filter(place__icontains="chicago")[0]['city']
148
143
'Elmhurst'
149
- >>> query.filter(foods__breakfast="waffles").data [0]['city']
144
+ >>> query.filter(foods__breakfast="waffles")[0]['city']
150
145
'Elmhurst'
151
- >>> query.filter(foods__fruit__in="cantelope").data [0]['city']
146
+ >>> query.filter(foods__fruit__in="cantelope")[0]['city']
152
147
'Elmhurst'
153
- >>> query.filter(foods__fruit__in="orange").data [0]['city']
148
+ >>> query.filter(foods__fruit__in="orange")[0]['city']
154
149
'Tampa'
155
150
"""
156
151
157
- __slots__ = ("data" , "pk_key" )
158
152
data : Sequence [T ]
159
153
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
-
166
154
def items (self ):
167
155
data : Sequence [T ]
168
156
169
157
if self .pk_key is None :
170
158
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 ]
172
160
173
161
def __eq__ (self , other ):
174
162
data = other
175
- if hasattr (data , "data" ):
176
- data = getattr (data , "data" )
177
163
178
- if not isinstance (self . data , list ) or not isinstance (data , list ):
164
+ if not isinstance (self , list ) or not isinstance (data , list ):
179
165
return False
180
166
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 ):
183
169
if isinstance (a , dict ):
184
170
a_keys = a .keys ()
185
171
if a .keys == b .keys ():
@@ -228,4 +214,4 @@ def val_match(obj):
228
214
else :
229
215
_filter = filter_lookup
230
216
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