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
@@ -121,13 +120,9 @@ def lookup_iregex(data, rhs):
121
120
}
122
121
123
122
124
- @dataclasses .dataclass (eq = False )
125
- class QueryList (Generic [T ]):
123
+ class QueryList (list [T ]):
126
124
"""Filter list of object/dicts. For small, local datasets. *Experimental, unstable*.
127
125
128
- :py:func:`dataclasses.dataclass` is only used for ``__repr__`` and pytest comparison
129
- details.
130
-
131
126
>>> query = QueryList(
132
127
... [
133
128
... {
@@ -144,44 +139,35 @@ class QueryList(Generic[T]):
144
139
... },
145
140
... ]
146
141
... )
147
- >>> query.filter(place="Chicago suburbs").data [0]['city']
142
+ >>> query.filter(place="Chicago suburbs")[0]['city']
148
143
'Elmhurst'
149
- >>> query.filter(place__icontains="chicago").data [0]['city']
144
+ >>> query.filter(place__icontains="chicago")[0]['city']
150
145
'Elmhurst'
151
- >>> query.filter(foods__breakfast="waffles").data [0]['city']
146
+ >>> query.filter(foods__breakfast="waffles")[0]['city']
152
147
'Elmhurst'
153
- >>> query.filter(foods__fruit__in="cantelope").data [0]['city']
148
+ >>> query.filter(foods__fruit__in="cantelope")[0]['city']
154
149
'Elmhurst'
155
- >>> query.filter(foods__fruit__in="orange").data [0]['city']
150
+ >>> query.filter(foods__fruit__in="orange")[0]['city']
156
151
'Tampa'
157
152
"""
158
153
159
- __slots__ = ("data" , "pk_key" )
160
154
data : Sequence [T ]
161
155
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
-
168
156
def items (self ):
169
157
data : Sequence [T ]
170
158
171
159
if self .pk_key is None :
172
160
raise 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 ]
174
162
175
163
def __eq__ (self , other ):
176
164
data = other
177
- if hasattr (data , "data" ):
178
- data = getattr (data , "data" )
179
165
180
- if not isinstance (self . data , list ) or not isinstance (data , list ):
166
+ if not isinstance (self , list ) or not isinstance (data , list ):
181
167
return False
182
168
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 ):
185
171
if isinstance (a , dict ):
186
172
a_keys = a .keys ()
187
173
if a .keys == b .keys ():
@@ -230,4 +216,4 @@ def val_match(obj):
230
216
else :
231
217
_filter = filter_lookup
232
218
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 ))
0 commit comments