Skip to content

Commit

Permalink
Support single field in sort
Browse files Browse the repository at this point in the history
  • Loading branch information
AliRn76 committed Mar 24, 2024
1 parent 5961fb4 commit ddfaa3d
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pantherdb/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from pantherdb.pantherdb import PantherDB, PantherCollection, PantherDocument, PantherDBException, Cursor

__version__ = '2.0.0'
__version__ = '2.1.0'
15 changes: 9 additions & 6 deletions pantherdb/pantherdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -481,16 +481,19 @@ 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]
if isinstance(index, int) and self.response_type:
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):
Expand All @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
7 changes: 7 additions & 0 deletions tests/test_normal.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit ddfaa3d

Please sign in to comment.