Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLqFJ-000536-9n for psycopg@arkaria.postgresql.org; Fri, 25 Sep 2020 16:06:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kLqFG-0001hS-GL for psycopg@arkaria.postgresql.org; Fri, 25 Sep 2020 16:06:50 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLqFE-0001g4-PR for psycopg@lists.postgresql.org; Fri, 25 Sep 2020 16:06:50 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLqFC-0003MK-9I for psycopg@lists.postgresql.org; Fri, 25 Sep 2020 16:06:47 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 6544C5C0041; Fri, 25 Sep 2020 12:06:45 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Fri, 25 Sep 2020 12:06:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=h CE8rrJTXFcArowN2n90HVmPmTo+96TShH50uYJk3bA=; b=dgy9yjaLp9nt70DSG cx+8eDwZIncOGf4Hy5BVtG0n3dXcZJFOGxrW/QLo1MlZ1hGLZ28wBLv/NCFhcvm2 Qy0GwShwr+LfhIxvUNeMFRgL7X6e3opNNpkG02/qqcOn2DVVqhZNo7iWpZ9vWCrL X5HJ90tX1XzZPiJxaSqvnkc6FKoGHAjgdP15QDAsW2VpPVrN/VNR/6JcIgc1fyiH qfdh6LmDj1UEuXLkk5ntnA5thVhxJs8oRum9O/dFu8XvS9LKI8s0T0Aem1GEkCgu /Dfx0Ezy4yJEzmS/2j+50+y9hBO2uK5eBXOQoyQraTJumRTTIHd5ZCjMxVYSBQAU L7RLg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm3; bh=hCE8rrJTXFcArowN2n90HVmPmTo+96TShH50uYJk3 bA=; b=Mg95DbndxubuIskHSa/UsdspaMHDnjjHwOM8mTVu7On4xQy7AATMkutuj UBaOa21+jpQiUv6+A9FAXOPhhTr/ZzbMt3/6M4djJran6rsMqeEdnzBnDQ2au9+j vELUlsMEedQKgST++BBo+1ic0TeFGNnAwHO+S2MtLtZVXMfOXeIjj4YDV7zDWGCN GQnoLTHmlLzYX1zQDiaFuBwRYE+V2IpAuf/5MzzQwPMGL30HkTGmKEXLDXekHmc1 AhKHiMElXCJKPU1ifL6JiffF1zCbHiEer2hdhuUtv3nqwy9gbalM0j6MyFLs+ZXE 6emWcs4YVnAokEVE8k2Lnru+0fvgA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrvddtgdeliecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuggftrfgrthhtvghrnhepgfffiefffffghedtueefleefueduffetleefgeelleet heegvdevfedvleelueeknecuffhomhgrihhnpehgihhthhhusgdrtghomhenucfkphepje ehrddujedvrdehrddvfedunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehm rghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Received: from [192.168.1.3] (75-172-5-231.tukw.qwest.net [75.172.5.231]) by mail.messagingengine.com (Postfix) with ESMTPA id 8546B3064682; Fri, 25 Sep 2020 12:06:44 -0400 (EDT) Subject: Re: iterating over DictRow To: Karsten Hilbert , psycopg@lists.postgresql.org References: <20200923215444.GA25406@hermes.hilbert.loc> <20200924211147.GA2089@hermes.hilbert.loc> <9f9f7536-9d5f-d95b-b1eb-eed7ec1b83f6@aklaver.com> <20200925074820.GC2138@hermes.hilbert.loc> From: Adrian Klaver Message-ID: <4975719e-8981-c2ff-1408-c348730b31c5@aklaver.com> Date: Fri, 25 Sep 2020 09:06:43 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.12.0 MIME-Version: 1.0 In-Reply-To: <20200925074820.GC2138@hermes.hilbert.loc> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 9/25/20 12:48 AM, Karsten Hilbert wrote: > Adrian, > > thanks for tracing my misunderstanding. I should have > confirmed my suspicion here: > >> https://github.com/psycopg/psycopg2/blob/fbba461052ae6ebc43167ab69ad91cadb7914c83/lib/extras.py > >> class DictRow(list): > >> def __getitem__(self, x): >> if not isinstance(x, (int, slice)): >> x = self._index[x] >> return super(DictRow, self).__getitem__(x) >> >> So if the value passed to __getitem__() is a integer or slice it does a list >> index. > > Indeed. I wonder whether that should be mentioned in the > psycopg2 docs somewhere as it might be considered to violate > the Principle Of Least Astonishment. > > Or rather, this issues seems unfortunate fallout from python3: > > In py2 one *had* to do DictRow.keys() to iterate over the > keys. In py3 > > for key in DictRow: > > is the suggested idiom for that which, however, iterates over > DictRow as a list (as it always did). > > DictRow.keys() still exists on dicts in py3 (and is not > deprec(i?)ated to my knowledge) but now returns a memoryview > (dict_keys, that is) rather than a list, which brings with it > its own set of issues (dict and keys "list" are not > independant objects anymore). > > So, neither using py2's > > for key in DictRow.keys(): > > under py3 nor changing to py3's > > for key in DictRow: # beep: variable wrongly named > > leads to fully equivalent code. So this is a py2/py3 Gotcha > in psycopg2. Well you can do, borrowing from previous example: for ky in r0._index: print(ky) line_id category cell_per ts_insert ts_update user_insert user_update plant_type season short_category for ky in r0._index: print(r0[ky]) 5 H PREM 3.5 18 None 2004-06-02 15:11:26 None postgres herb none HP3 Where _index is a substitute for *.keys(). > > Not that I complain, but worth a mention in the DictRow docs > somewhere ? > > Karsten > -- > GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B > > -- Adrian Klaver adrian.klaver@aklaver.com