Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue13299] namedtuple row factory for sqlite3

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Aug 19, 2012, 8:20 PM

Post #1 of 5 (90 views)
Permalink
[issue13299] namedtuple row factory for sqlite3

Russell Sim added the comment:

Hi,

Here is an implementation using lru_cache to prevent regeneration of the named tuple each time.

Cheers,
Russell

----------
keywords: +patch
nosy: +Russell.Sim
Added file: http://bugs.python.org/file26909/issue_13299.patch

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13299>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Aug 21, 2012, 12:20 AM

Post #2 of 5 (91 views)
Permalink
[issue13299] namedtuple row factory for sqlite3 [In reply to]

Raymond Hettinger added the comment:

Caching based on the cursor going to be problematic because a single cursor can be used multiple times with different descriptions:

c = conn.cursor()
c.execute('select symbol from stocks')
print c.description
c.execute('select price from stocks')
print c.description # same cursor, different layout, needs a new named tuple

It might make more sense to cache the namedtuple() factory itself:

sql_namedtuple = lru_cache(maxsize=20)(namedtuple)

Also, the example in the docs is too lengthy and indirect. Cut-out the step for creating an populating the database -- just assume db created in the example at the top of the page:

For example::

>>> conn.row_factory = sqlite3.NamedTupleRow
>>> c = conn.cursor()
>>> for record in c.execute('select * from stocks'):
print record

Row(date='2006-01-05', trans='BUY', symbol='RHAT', qty=100.0, price=35.14)
Row(date='2006-01-05', trans='BUY', symbol='RHAT', qty=100, price=35.14)
Row(date='2006-03-28', trans='BUY', symbol='IBM', qty=1000, price-45.0)

No need to go into a further lesson on how to use named tuples.


Also, the patch uses star-unpacking: _namedtuple_row(cursor)(*row)

Instead, it should use _make: _namedtuple_row(cursor)._make(row)


(u'2006-04-05', u'BUY', u'MSFT', 1000, 72.0)

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13299>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Aug 21, 2012, 5:16 AM

Post #3 of 5 (88 views)
Permalink
[issue13299] namedtuple row factory for sqlite3 [In reply to]

Russell Sim added the comment:

Raymond, Thanks for the comprehensive feedback! It's fantastic! I have updated the patch with most of you feedback... but there was one part that I couldn't follow entirely. I am now using the _make method but I have had to use star unpacking to allow the method to be cached, lru_cache won't allow a key to be a list because they aren't hash-able.

----------
Added file: http://bugs.python.org/file26945/issue_13299.1.patch

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13299>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Aug 21, 2012, 5:35 AM

Post #4 of 5 (87 views)
Permalink
[issue13299] namedtuple row factory for sqlite3 [In reply to]

Nick Coghlan added the comment:

You should be able to just use "tuple(col[0] for col in cursor.description)" instead of the current list comprehension in order to make the argument hashable.

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13299>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Aug 21, 2012, 5:49 AM

Post #5 of 5 (87 views)
Permalink
[issue13299] namedtuple row factory for sqlite3 [In reply to]

Russell Sim added the comment:

Nick, Thanks for the tip. I have removed the star unpacking.

----------
Added file: http://bugs.python.org/file26946/issue_13299.2.patch

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue13299>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.