From 29301ada615a0331fbcc1e1f5916bd0bd3c45d1c Mon Sep 17 00:00:00 2001 From: Kenneth Reitz Date: Fri, 12 Feb 2016 22:30:47 -0500 Subject: [PATCH] as_dict --- records.py | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/records.py b/records.py index c5b2d62..7cf69cd 100644 --- a/records.py +++ b/records.py @@ -72,6 +72,13 @@ class RecordsCursor(NamedTupleCursor): except KeyError: return default + def as_dict(self, ordered=False): + """Returns the row as a dictionary, as ordered.""" + if ordered: + return self._asdict() + else: + return dict(self._asdict()) + return Record @@ -155,13 +162,19 @@ class ResultSet(object): return data - - def all(self): + def all(self, as_dict=False, as_ordereddict=False): """Returns a list of all rows for the ResultSet. If they haven't been fetched yet, consume the iterator and cache the results.""" # By calling list it calls the __iter__ method - return list(self) + rows = list(self) + + if as_dict: + return [r.as_dict() for r in rows] + elif as_ordereddict: + return [r.as_dict(ordered=True) for r in rows] + + return rows class Database(object): """A Database connection."""