From ddfaa3d788687b345b9bc6d89849d09a46c61b86 Mon Sep 17 00:00:00 2001 From: Ali RajabNezhad Date: Sun, 24 Mar 2024 23:19:40 +0330 Subject: [PATCH] Support single field in sort --- pantherdb/__init__.py | 2 +- pantherdb/pantherdb.py | 15 +++++++++------ setup.py | 2 +- tests/test_normal.py | 7 +++++++ 4 files changed, 18 insertions(+), 8 deletions(-) diff --git a/pantherdb/__init__.py b/pantherdb/__init__.py index 0d2a4d1..3b3fe4a 100644 --- a/pantherdb/__init__.py +++ b/pantherdb/__init__.py @@ -1,3 +1,3 @@ from pantherdb.pantherdb import PantherDB, PantherCollection, PantherDocument, PantherDBException, Cursor -__version__ = '2.0.0' +__version__ = '2.1.0' diff --git a/pantherdb/pantherdb.py b/pantherdb/pantherdb.py index 62398c9..b1a93c9 100644 --- a/pantherdb/pantherdb.py +++ b/pantherdb/pantherdb.py @@ -459,10 +459,10 @@ def __init__(self, documents: List[dict | PantherDocument], kwargs: dict): self._skip = None self.cls = None self.response_type = None - self._condition_applied = False + self._conditions_applied = False def next(self): - if not self._condition_applied: + if not self._conditions_applied: self._apply_conditions() self._cursor += 1 @@ -481,7 +481,7 @@ def next(self): __next__ = next def __getitem__(self, index: int | slice) -> Union[Cursor, dict, ...]: - if not self._condition_applied: + if not self._conditions_applied: self._apply_conditions() result = self.documents[index] @@ -489,8 +489,11 @@ def __getitem__(self, index: int | slice) -> Union[Cursor, dict, ...]: return self.response_type(result) return result - def sort(self, sorts: List[Tuple[str, int]]): - self._sorts = sorts + def sort(self, sorts: List[Tuple[str, int]] | str, sort_order: int = None): + if isinstance(sorts, str): + self._sorts = [(sorts, sort_order)] + else: + self._sorts = sorts return self def skip(self, skip): @@ -505,7 +508,7 @@ def _apply_conditions(self): self._apply_sort() self._apply_skip() self._apply_limit() - self._condition_applied = True + self._conditions_applied = True def _apply_sort(self): if self._sorts: diff --git a/setup.py b/setup.py index 4ea8045..db3693b 100644 --- a/setup.py +++ b/setup.py @@ -25,7 +25,7 @@ def pantherdb_version() -> str: python_requires='>=3.8', author='Ali RajabNezhad', author_email='alirn76@yahoo.com', - url='https://github.com/alirn76/pantherdb', + url='https://github.com/PantherPy/pantherdb', description='is a Simple, FileBase and Document Oriented database', long_description=DESCRIPTION, long_description_content_type='text/markdown', diff --git a/tests/test_normal.py b/tests/test_normal.py index 7f9153a..8457b1a 100644 --- a/tests/test_normal.py +++ b/tests/test_normal.py @@ -788,6 +788,13 @@ def test_find_with_sort(self): assert (objs[3].first_name, objs[3].last_name) == ('B', 1) # Find with single sort + objs = collection.find().sort('first_name', 1) + assert (objs[0].first_name, objs[0].last_name) == ('A', 0) + assert (objs[1].first_name, objs[1].last_name) == ('A', 1) + assert (objs[2].first_name, objs[2].last_name) == ('B', 0) + assert (objs[3].first_name, objs[3].last_name) == ('B', 1) + + # Find with single sort as a list objs = collection.find().sort([('first_name', 1)]) assert (objs[0].first_name, objs[0].last_name) == ('A', 0) assert (objs[1].first_name, objs[1].last_name) == ('A', 1)