Commit 744148de authored by David Douard's avatar David Douard
Browse files

[rset] add convenient first() and last() methods on ResultSet

These make life easier for the heavy user of the CW shell.
Also aliases entities() with all(); so we end with a pretty consistant set:

- .one()
- .first()
- .last()
- .all()
parent 38058ce2a9ec
...@@ -393,6 +393,8 @@ class ResultSet(object): ...@@ -393,6 +393,8 @@ class ResultSet(object):
if self.rows[i][col] is not None: if self.rows[i][col] is not None:
yield self.get_entity(i, col) yield self.get_entity(i, col)
all = entities
def iter_rows_with_entities(self): def iter_rows_with_entities(self):
""" iterates over rows, and for each row """ iterates over rows, and for each row
eids are converted to plain entities eids are converted to plain entities
...@@ -459,6 +461,34 @@ class ResultSet(object): ...@@ -459,6 +461,34 @@ class ResultSet(object):
else: else:
raise MultipleResultsError("Multiple rows were found for one()") raise MultipleResultsError("Multiple rows were found for one()")
def first(self, col=0):
"""Retrieve the first entity from the query.
If the result set is empty, raises :exc:`NoResultError`.
:type col: int
:param col: The column localising the entity in the unique row
:return: the partially initialized `Entity` instance
"""
if len(self) == 0:
raise NoResultError("No row was found for first()")
return self.get_entity(0, col)
def last(self, col=0):
"""Retrieve the last entity from the query.
If the result set is empty, raises :exc:`NoResultError`.
:type col: int
:param col: The column localising the entity in the unique row
:return: the partially initialized `Entity` instance
"""
if len(self) == 0:
raise NoResultError("No row was found for last()")
return self.get_entity(-1, col)
def _make_entity(self, row, col): def _make_entity(self, row, col):
"""Instantiate an entity, and store it in the entity cache""" """Instantiate an entity, and store it in the entity cache"""
# build entity instance # build entity instance
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment