Skip to content

Commit 715439a

Browse files
authored
Merge pull request #2 from the-sam963/master
fixed type error for older version
2 parents 654d207 + 2fd4ae5 commit 715439a

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

.github/workflows/python-package.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ on:
1212
jobs:
1313
build:
1414

15-
runs-on: ubuntu-latest
15+
runs-on: windows-latest
1616
strategy:
1717
fail-fast: false
1818
matrix:
@@ -28,7 +28,6 @@ jobs:
2828
run: |
2929
python -m pip install --upgrade pip
3030
python -m pip install flake8 pytest
31-
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
3231
- name: Lint with flake8
3332
run: |
3433
# stop the build if there are Python syntax errors or undefined names

filexdb/collection.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ def __find_one(self, query: Mapping = None) -> Document | None:
133133
return _result
134134
"""
135135

136-
def find(self, query: Mapping = None, limit: tuple = None) -> List[Document | None]:
136+
def find(self, query=None, limit=None) -> List[Document]:
137137
"""
138138
Finds all ``Document`` of ``Collection``.
139139
@@ -145,17 +145,24 @@ def find(self, query: Mapping = None, limit: tuple = None) -> List[Document | No
145145
:param query: Condition to search Document
146146
:return: List of Document
147147
"""
148+
148149
# Default result
149150
_result = []
150151

151152
# 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')
154161

155162
# if limit, Check everything ok
156163
_limit_start = _limit_end = None
157164

158-
if limit:
165+
if limit and type(limit) == type((1, 3)):
159166
if len(limit) == 2:
160167

161168
_limit_start = limit[0]
@@ -175,15 +182,16 @@ def find(self, query: Mapping = None, limit: tuple = None) -> List[Document | No
175182

176183
# check if lower limit is valid or not
177184
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.")
179187
else:
180188
_result = self._collection[_limit_start: _limit_end]
181189
else:
182190
_result = self._collection
183191

184192
return _result
185193

186-
elif query is not None:
194+
elif query is not None and type(query) == type({}):
187195
if limit:
188196
for i in self._collection:
189197
_doc = self._find_document_by_query(query)
@@ -212,11 +220,14 @@ def find(self, query: Mapping = None, limit: tuple = None) -> List[Document | No
212220

213221
if _doc:
214222
_result += _doc
223+
else:
224+
_result = _result
225+
215226
self._reset_cursor()
216227

217228
return _result
218229

219-
def delete(self, query: Mapping = None) -> List[str]:
230+
def delete(self, query=None) -> List[str]:
220231
"""
221232
Delete single or multiple Document when meet the Conditions or ``query``.
222233
@@ -241,7 +252,7 @@ def delete(self, query: Mapping = None) -> List[str]:
241252

242253
return _doc_id
243254

244-
def update(self, document: Mapping, query: Mapping = None) -> List[str]:
255+
def update(self, document: Mapping, query=None) -> List[str]:
245256
"""
246257
Fetch all the Documents mathc the conditions and update them.
247258
@@ -277,8 +288,7 @@ def update(self, document: Mapping, query: Mapping = None) -> List[str]:
277288

278289
return _doc_id
279290

280-
281-
def count(self, query: Mapping = None, limit: tuple = None) -> int:
291+
def count(self, query=None, limit: tuple = None) -> int:
282292
"""
283293
Return amount of Document found.
284294
@@ -297,7 +307,7 @@ def _reset_cursor(self) -> None:
297307
"""
298308
self._cursor = 0
299309

300-
def _find_document_by_query(self, query: Mapping) -> List | None:
310+
def _find_document_by_query(self, query: Mapping) -> List:
301311
"""
302312
Finds a single ``Document`` of ``Collection``.
303313
@@ -309,7 +319,7 @@ def _find_document_by_query(self, query: Mapping) -> List | None:
309319
result = []
310320

311321
# Make sure the query implements the ``Mapping`` interface.
312-
if not isinstance(query, Mapping | None):
322+
if not isinstance(query, Mapping):
313323
raise ValueError('Document is not a Dictionary')
314324

315325
# Get the length on Collection
@@ -341,7 +351,6 @@ def _find_document_by_query(self, query: Mapping) -> List | None:
341351
# If both values are same, then update ``_bag_of_query[i]`` as 1.
342352
_bag_of_query[i] = 1
343353

344-
345354
else:
346355
continue
347356
else:
@@ -365,17 +374,17 @@ def _find_document_by_query(self, query: Mapping) -> List | None:
365374
return result
366375

367376
# ======================== #
368-
def _doc_is_exists(self, doc_id: str | int) -> bool:
377+
def _doc_is_exists(self, doc_id: str) -> bool:
369378
# Iterate over all Documents of Collection
370379
for doc in self._collection:
371380
if doc["_id_"] == doc_id:
372381
return True
373382

374383
return False
375384

376-
def _find_document_by_id(self, doc_id) -> Document | str:
385+
def _find_document_by_id(self, doc_id) -> Document:
377386
for doc in self._collection:
378387
if doc["_id_"] == doc_id:
379388
return doc
380389
else:
381-
return "none"
390+
return None

filexdb/document.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ def __init__(self, value: Mapping) -> None:
2020
self._doc = value
2121
self.id = value["_id_"]
2222
else:
23-
self._doc = (_id_obj | value)
23+
self._doc = _id_obj
24+
for k, v in value.items():
25+
self._doc[k] = v
2426
self.id = self._doc["_id_"]
2527

2628
super().__init__(self._doc)

0 commit comments

Comments
 (0)