Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.002 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'transform': 0.07; 'cursor': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'self.data': 0.09; 'def': 0.12; 'constructs': 0.16; 'dict': 0.16; 'dictionary,': 0.16; 'factory': 0.16; 'initialiser': 0.16; 'readable': 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; 'recipe': 0.16; 'sqlite': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; 'questions:': 0.24; 'url:dev': 0.24; 'pass': 0.26; 'least': 0.26; 'header:X -Complaints-To:1': 0.27; "i'm": 0.30; 'code': 0.31; 'class': 0.32; 'open': 0.33; 'url:python': 0.33; 'skip:_ 10': 0.34; 'skip:d 20': 0.34; 'subject:from': 0.34; 'connection': 0.35; 'convert': 0.35; 'acceptable': 0.36; 'url:org': 0.36; 'two': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'enough': 0.39; 'either': 0.39; 'received:org': 0.40; 'read': 0.60; 'more': 0.64; 'construction': 0.72; 'idiom': 0.84; 'directly.': 0.95; 'unsolicited': 0.95 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Dictionary from sqlite3.Row and PyCharm unresolved reference Date: Fri, 13 Feb 2015 12:33:22 +0100 Organization: None References: <1a194e0a113d8d215970f15d910@nntp.aioe.org> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p57bd9d74.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 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 43 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1423827222 news.xs4all.nl 2884 [2001:888:2000:d::a6]:54655 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:85639 Mario Figueiredo wrote: > Currently i'm using the following code to transform a row fetched from an > sqlite database into a dictionary property: class Unknown: > def __init__(self, id_): > self.id = id_ > self.data = None > ... > conn = sqlite3.connect('data') > conn.row_factory = sqlite3.Row > row = conn.execute(query, {'id': str(id_)}).fetchone() > conn.close() > > if row: > self.data = dict(zip(row.keys(), tuple(row))) > > I have two questions: > > 1. Is this an acceptable idiom for the construction of self.data > dictionary, > or do I have a better and more readable option? If sqlite3.Row is not dict-like enough to use it directly self.data = row you can either convert the Row object with self.data = dict(row) or use the recipe for a row factory provided in the documentation at https://docs.python.org/dev/library/sqlite3.html#sqlite3.Connection.row_factory that constructs a dict directly. And now an unsolicited remark: if you have more than one instance of Unknown you might read the data outside the initialiser or at least keep the connection open and pass a connection or cursor object to the initialiser.