@@ -133,7 +133,7 @@ def __find_one(self, query: Mapping = None) -> Document | None:
133
133
return _result
134
134
"""
135
135
136
- def find (self , query : Mapping = None , limit : tuple = None ) -> List [Document | None ]:
136
+ def find (self , query = None , limit = None ) -> List [Document ]:
137
137
"""
138
138
Finds all ``Document`` of ``Collection``.
139
139
@@ -145,17 +145,24 @@ def find(self, query: Mapping = None, limit: tuple = None) -> List[Document | No
145
145
:param query: Condition to search Document
146
146
:return: List of Document
147
147
"""
148
+
148
149
# Default result
149
150
_result = []
150
151
151
152
# Make sure the query implements the ``Mapping`` interface.
152
- if not isinstance (query , Mapping | None ):
153
- raise ValueError ('Document is not a Dictionary' )
153
+ if query :
154
+ if not isinstance (query , Mapping ):
155
+ raise ValueError ('Document is not a Dictionary' )
156
+
157
+ # Make sure the query implements the ``Tuple`` interface.
158
+ if limit :
159
+ if not isinstance (limit , tuple ):
160
+ raise ValueError ('Document is not a Tuple' )
154
161
155
162
# if limit, Check everything ok
156
163
_limit_start = _limit_end = None
157
164
158
- if limit :
165
+ if limit and type ( limit ) == type (( 1 , 3 )) :
159
166
if len (limit ) == 2 :
160
167
161
168
_limit_start = limit [0 ]
@@ -175,15 +182,16 @@ def find(self, query: Mapping = None, limit: tuple = None) -> List[Document | No
175
182
176
183
# check if lower limit is valid or not
177
184
if _limit_start >= len (self ._collection ):
178
- raise ValueError (f"Lower limit should be smaller than Collection length.\n It must be less than `{ len (self ._collection )} `. `{ _limit_start } ` is given." )
185
+ raise ValueError (
186
+ f"Lower limit should be smaller than Collection length.\n It must be less than `{ len (self ._collection )} `. `{ _limit_start } ` is given." )
179
187
else :
180
188
_result = self ._collection [_limit_start : _limit_end ]
181
189
else :
182
190
_result = self ._collection
183
191
184
192
return _result
185
193
186
- elif query is not None :
194
+ elif query is not None and type ( query ) == type ({}) :
187
195
if limit :
188
196
for i in self ._collection :
189
197
_doc = self ._find_document_by_query (query )
@@ -212,11 +220,14 @@ def find(self, query: Mapping = None, limit: tuple = None) -> List[Document | No
212
220
213
221
if _doc :
214
222
_result += _doc
223
+ else :
224
+ _result = _result
225
+
215
226
self ._reset_cursor ()
216
227
217
228
return _result
218
229
219
- def delete (self , query : Mapping = None ) -> List [str ]:
230
+ def delete (self , query = None ) -> List [str ]:
220
231
"""
221
232
Delete single or multiple Document when meet the Conditions or ``query``.
222
233
@@ -241,7 +252,7 @@ def delete(self, query: Mapping = None) -> List[str]:
241
252
242
253
return _doc_id
243
254
244
- def update (self , document : Mapping , query : Mapping = None ) -> List [str ]:
255
+ def update (self , document : Mapping , query = None ) -> List [str ]:
245
256
"""
246
257
Fetch all the Documents mathc the conditions and update them.
247
258
@@ -277,8 +288,7 @@ def update(self, document: Mapping, query: Mapping = None) -> List[str]:
277
288
278
289
return _doc_id
279
290
280
-
281
- def count (self , query : Mapping = None , limit : tuple = None ) -> int :
291
+ def count (self , query = None , limit : tuple = None ) -> int :
282
292
"""
283
293
Return amount of Document found.
284
294
@@ -297,7 +307,7 @@ def _reset_cursor(self) -> None:
297
307
"""
298
308
self ._cursor = 0
299
309
300
- def _find_document_by_query (self , query : Mapping ) -> List | None :
310
+ def _find_document_by_query (self , query : Mapping ) -> List :
301
311
"""
302
312
Finds a single ``Document`` of ``Collection``.
303
313
@@ -309,7 +319,7 @@ def _find_document_by_query(self, query: Mapping) -> List | None:
309
319
result = []
310
320
311
321
# Make sure the query implements the ``Mapping`` interface.
312
- if not isinstance (query , Mapping | None ):
322
+ if not isinstance (query , Mapping ):
313
323
raise ValueError ('Document is not a Dictionary' )
314
324
315
325
# Get the length on Collection
@@ -341,7 +351,6 @@ def _find_document_by_query(self, query: Mapping) -> List | None:
341
351
# If both values are same, then update ``_bag_of_query[i]`` as 1.
342
352
_bag_of_query [i ] = 1
343
353
344
-
345
354
else :
346
355
continue
347
356
else :
@@ -365,17 +374,17 @@ def _find_document_by_query(self, query: Mapping) -> List | None:
365
374
return result
366
375
367
376
# ======================== #
368
- def _doc_is_exists (self , doc_id : str | int ) -> bool :
377
+ def _doc_is_exists (self , doc_id : str ) -> bool :
369
378
# Iterate over all Documents of Collection
370
379
for doc in self ._collection :
371
380
if doc ["_id_" ] == doc_id :
372
381
return True
373
382
374
383
return False
375
384
376
- def _find_document_by_id (self , doc_id ) -> Document | str :
385
+ def _find_document_by_id (self , doc_id ) -> Document :
377
386
for doc in self ._collection :
378
387
if doc ["_id_" ] == doc_id :
379
388
return doc
380
389
else :
381
- return "none"
390
+ return None
0 commit comments