Skip to content

Commit 82240bb

Browse files
Added .dicts() method to results
1 parent 7e547b7 commit 82240bb

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

README.rst

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,9 @@ leftmost column serving as key, for unique values.
109109
In [15]: result['richard2']
110110
Out[15]: (u'richard2', u'Richard II', u'History of Richard II', 1595, u'h', None, u'Moby', 22411, 628)
111111
112+
Results can also be retrieved as an iterator of dictionaries (``result.dicts()``)
113+
or a single dictionary with a tuple of scalar values per key (``result.dict()``)
114+
112115
Assignment
113116
----------
114117

@@ -276,7 +279,7 @@ Credits
276279
- Andrés Celis for SQL Server bugfix
277280
- Michael Erasmus for DataFrame truth bugfix
278281
- Noam Finkelstein for README clarification
279-
- Xiaochuan Yu for `<<` operator
282+
- Xiaochuan Yu for `<<` operator, syntax colorization
280283

281284
.. _Distribute: http://pypi.python.org/pypi/distribute
282285
.. _Buildout: http://www.buildout.org/

src/sql/run.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,22 @@ def __getitem__(self, key):
140140
raise KeyError('%d results for "%s"' % (len(result), key))
141141
return result[0]
142142
def dict(self):
143-
"Returns a dict built from the result set, with column names as keys"
143+
"""Returns a single dict built from the result set
144+
145+
Keys are column names; values are a tuple"""
144146
return dict(zip(self.keys, zip(*self)))
145147

148+
def dicts(self):
149+
"Iterator yielding a dict for each row"
150+
for row in self:
151+
yield dict(zip(self.keys, row))
152+
146153
def DataFrame(self):
147154
"Returns a Pandas DataFrame instance built from the result set."
148155
import pandas as pd
149156
frame = pd.DataFrame(self, columns=(self and self.keys) or [])
150157
return frame
158+
151159
def pie(self, key_word_sep=" ", title=None, **kwargs):
152160
"""Generates a pylab pie chart from the result set.
153161

src/tests/test_magic.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,3 +191,23 @@ def test_csv_to_file():
191191
for row in content.splitlines():
192192
assert row.count(',') == 1
193193
assert len(content.splitlines()) == 3
194+
195+
@with_setup(_setup_writer, _teardown_writer)
196+
def test_dict():
197+
result = ip.run_line_magic('sql', "sqlite:// SELECT * FROM writer;")
198+
result = result.dict()
199+
assert isinstance(result, dict)
200+
assert 'first_name' in result
201+
assert 'last_name' in result
202+
assert 'year_of_death' in result
203+
assert len(result['last_name']) == 2
204+
205+
@with_setup(_setup_writer, _teardown_writer)
206+
def test_dicts():
207+
result = ip.run_line_magic('sql', "sqlite:// SELECT * FROM writer;")
208+
for row in result.dicts():
209+
assert isinstance(row, dict)
210+
assert 'first_name' in row
211+
assert 'last_name' in row
212+
assert 'year_of_death' in row
213+

0 commit comments

Comments
 (0)