Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ on:
jobs:
build:

runs-on: ubuntu-latest
runs-on: windows-latest
strategy:
fail-fast: false
matrix:
Expand All @@ -28,7 +28,6 @@ jobs:
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
Expand Down
41 changes: 25 additions & 16 deletions filexdb/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def __find_one(self, query: Mapping = None) -> Document | None:
return _result
"""

def find(self, query: Mapping = None, limit: tuple = None) -> List[Document | None]:
def find(self, query=None, limit=None) -> List[Document]:
"""
Finds all ``Document`` of ``Collection``.

Expand All @@ -145,17 +145,24 @@ def find(self, query: Mapping = None, limit: tuple = None) -> List[Document | No
:param query: Condition to search Document
:return: List of Document
"""

# Default result
_result = []

# Make sure the query implements the ``Mapping`` interface.
if not isinstance(query, Mapping | None):
raise ValueError('Document is not a Dictionary')
if query:
if not isinstance(query, Mapping):
raise ValueError('Document is not a Dictionary')

# Make sure the query implements the ``Tuple`` interface.
if limit:
if not isinstance(limit, tuple):
raise ValueError('Document is not a Tuple')

# if limit, Check everything ok
_limit_start = _limit_end = None

if limit:
if limit and type(limit) == type((1, 3)):
if len(limit) == 2:

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

# check if lower limit is valid or not
if _limit_start >= len(self._collection):
raise ValueError(f"Lower limit should be smaller than Collection length.\n It must be less than `{len(self._collection)}`. `{_limit_start}` is given.")
raise ValueError(
f"Lower limit should be smaller than Collection length.\n It must be less than `{len(self._collection)}`. `{_limit_start}` is given.")
else:
_result = self._collection[_limit_start: _limit_end]
else:
_result = self._collection

return _result

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

if _doc:
_result += _doc
else:
_result = _result

self._reset_cursor()

return _result

def delete(self, query: Mapping = None) -> List[str]:
def delete(self, query=None) -> List[str]:
"""
Delete single or multiple Document when meet the Conditions or ``query``.

Expand All @@ -241,7 +252,7 @@ def delete(self, query: Mapping = None) -> List[str]:

return _doc_id

def update(self, document: Mapping, query: Mapping = None) -> List[str]:
def update(self, document: Mapping, query=None) -> List[str]:
"""
Fetch all the Documents mathc the conditions and update them.

Expand Down Expand Up @@ -277,8 +288,7 @@ def update(self, document: Mapping, query: Mapping = None) -> List[str]:

return _doc_id


def count(self, query: Mapping = None, limit: tuple = None) -> int:
def count(self, query=None, limit: tuple = None) -> int:
"""
Return amount of Document found.

Expand All @@ -297,7 +307,7 @@ def _reset_cursor(self) -> None:
"""
self._cursor = 0

def _find_document_by_query(self, query: Mapping) -> List | None:
def _find_document_by_query(self, query: Mapping) -> List:
"""
Finds a single ``Document`` of ``Collection``.

Expand All @@ -309,7 +319,7 @@ def _find_document_by_query(self, query: Mapping) -> List | None:
result = []

# Make sure the query implements the ``Mapping`` interface.
if not isinstance(query, Mapping | None):
if not isinstance(query, Mapping):
raise ValueError('Document is not a Dictionary')

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


else:
continue
else:
Expand All @@ -365,17 +374,17 @@ def _find_document_by_query(self, query: Mapping) -> List | None:
return result

# ======================== #
def _doc_is_exists(self, doc_id: str | int) -> bool:
def _doc_is_exists(self, doc_id: str) -> bool:
# Iterate over all Documents of Collection
for doc in self._collection:
if doc["_id_"] == doc_id:
return True

return False

def _find_document_by_id(self, doc_id) -> Document | str:
def _find_document_by_id(self, doc_id) -> Document:
for doc in self._collection:
if doc["_id_"] == doc_id:
return doc
else:
return "none"
return None
4 changes: 3 additions & 1 deletion filexdb/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ def __init__(self, value: Mapping) -> None:
self._doc = value
self.id = value["_id_"]
else:
self._doc = (_id_obj | value)
self._doc = _id_obj
for k, v in value.items():
self._doc[k] = v
self.id = self._doc["_id_"]

super().__init__(self._doc)
Expand Down