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