Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #78513
| Path | csiph.com!usenet.pasdenom.info!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.001 |
| X-Spam-Evidence | '*H*': 1.00; '*S*': 0.00; 'column': 0.07; 'python3': 0.07; 'cursor': 0.09; 'none)': 0.09; 'pointless': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'def': 0.12; '(none,': 0.16; 'bit)': 0.16; 'both,': 0.16; 'collections': 0.16; 'convey': 0.16; 'csv': 0.16; 'namedtuple': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'tuple': 0.16; 'wrote:': 0.18; 'code.': 0.18; 'variable': 0.18; 'meant': 0.20; 'example': 0.22; 'import': 0.22; 'print': 0.22; 'header:User-Agent:1': 0.23; 'title,': 0.24; 'least': 0.26; 'header:X-Complaints-To:1': 0.27; 'idea': 0.28; 'code': 0.31; 'department,': 0.31; 'description,': 0.31; 'skip:m 30': 0.32; 'skip:# 10': 0.33; 'actual': 0.34; 'subject:the': 0.34; 'subject:with': 0.35; 'basic': 0.35; 'skip:s 30': 0.35; 'something': 0.35; 'but': 0.35; 'add': 0.35; 'acceptable': 0.36; 'otherwise.': 0.36; 'surely': 0.36; 'possible': 0.36; 'should': 0.36; 'employee': 0.37; 'to:addr:python-list': 0.38; 'rather': 0.38; 'subject:can': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'easy': 0.60; 'most': 0.60; 'skip:n 10': 0.64; 'between': 0.67; 'bells': 0.84; 'subject:Using': 0.84; 'usage.': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Using namedtuple with sqlite, surely it can do better than the example |
| Date | Wed, 01 Oct 2014 01:06:51 +0200 |
| Organization | None |
| References | <n3krfb-mo1.ln1@chris.zbmc.eu> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd9641.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.15 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.14499.1412118425.18130.python-list@python.org> (permalink) |
| Lines | 61 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1412118425 news.xs4all.nl 2841 [2001:888:2000:d::a6]:37471 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:78513 |
Show key headers only | View raw
cl@isbd.net wrote:
> In the namedtuple documentation there's an example:-
>
> EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title,
> department, paygrade')
>
> import sqlite3
> conn = sqlite3.connect('/companydata')
> cursor = conn.cursor()
> cursor.execute('SELECT name, age, title, department, paygrade FROM
> employees') for emp in map(EmployeeRecord._make, cursor.fetchall()):
> print emp.name, emp.title
>
> (I've deleted the csv bit)
>
> Surely having to match up the "name, age, title, department,
> paygrade" between the tuple and the database can be automated, the
> example is rather pointless otherwise. At the very least one should
> use the same variable instance for both, but it should be possible to
> get the tuple names from the database.
It's an example meant to convey the basic idea -- you can add bells and
whistles in your actual code. Something like
#!/usr/bin/env python3
import sqlite3
import functools
from collections import namedtuple
def make_namedtuple_row_factory():
last_pair = (None, None) # description, rowtype
@functools.lru_cache(maxsize=500)
def make_rowtype(description):
return namedtuple("Row",
[column[0] for column in description])._make
def make_namedtuple_is_make(cursor, row):
nonlocal last_pair
this_description = cursor.description
last_description, last_rowtype = last_pair
if last_description is this_description:
return last_rowtype(row)
this_rowtype = make_rowtype(this_description)
last_pair = this_description, this_rowtype
return this_rowtype(row)
return make_namedtuple_is_make
db = sqlite3.connect("companydata")
cursor = db.cursor()
cursor.row_factory = make_namedtuple_row_factory()
for employee in cursor.execute(
'SELECT name, age, title, department, paygrade FROM employees'):
print(employee.name, employee.title)
might be easy to use and have acceptable performance, but most of the code
distracts from the namedtuple usage.
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Using namedtuple with sqlite, surely it can do better than the example cl@isbd.net - 2014-09-30 22:32 +0100 Re: Using namedtuple with sqlite, surely it can do better than the example Peter Otten <__peter__@web.de> - 2014-10-01 01:06 +0200 Re: Using namedtuple with sqlite, surely it can do better than the example Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-10-01 00:23 +0100
csiph-web