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 1kLw3n-0002oA-Eu for psycopg@arkaria.postgresql.org; Fri, 25 Sep 2020 22:19:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kLw3m-0001XY-Bm for psycopg@arkaria.postgresql.org; Fri, 25 Sep 2020 22:19:22 +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 1kLw3k-0001XR-Tm for psycopg@lists.postgresql.org; Fri, 25 Sep 2020 22:19:22 +0000 Received: from wout5-smtp.messagingengine.com ([64.147.123.21]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLw3h-0006hw-JT for psycopg@lists.postgresql.org; Fri, 25 Sep 2020 22:19:19 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 12494B15; Fri, 25 Sep 2020 18:19:15 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Fri, 25 Sep 2020 18:19:15 -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=W 6ryA+5O+H9Rxbp9DJ5S5v+ConMvV6bxcrsCKdakUxE=; b=E+ZqNf+qBAXGL/Hj3 nheDcG1A6oglWL57RH62WNx0vyF+7rYZPxo0RlRuolaGBYJfbHOAHtFrL3czWWRG XXNsVV6sK/7gBsQdBIYFTZkKZDt5eT2EJH7fzVdW8dNK/Rs8i0/9Az3eAR4mHoJO 7Uu30CFhjwXzNlgEHck6OdPW+B6HcWAV6Ni9ne2BhC6zlNRielfmB6Bl8MQeYzR1 KHveUkFQ6DIj6ASNu43pMGP11lmS2eYM5gxRGvU3W0cdfGaACBVi2NTrNYXffwP/ imycVMEb1Ck89aRm28GJKgT04VD+1580p2A7NbmlfBDmIvR+sL0TjQZiApd60h7W iy1Ng== 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=W6ryA+5O+H9Rxbp9DJ5S5v+ConMvV6bxcrsCKdakU xE=; b=n6jYjdJkFUbnaIT2d+eNPRJoA6vFqC2Jhiau0wvi0iHaHUc+YHqsdbOGq VGhtSEM7SJflnc4do/kjQj1wJTMURDDXT5/SpNc6emGwjchK7ygr/pBCx8CaMCK0 qeGc43f6Vk6WY8/owTiFiPGZAMRGefgRiYp71DYxu1znMp1QqawvyaGUd+3ldqct MxWw34ufKvfuIIzhYHvOIYjLkcKRS+KrEHCQZwOly45MQFGxjTh3HNWnkiA1SSwX H7QSn1vePmq/0RmUIB3JBSDC7lRZbu1xBCP+39PCQzBnRW+gv88VJlhuxxwpMrkw x6dRXEZp0ftvYfi/K77otbDCf3PeA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrvddugddtvdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpefuvfhfhffkffgfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgr nhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh eqnecuggftrfgrthhtvghrnhepteejvdetffejuedvleeifeefheeutdfhgfdvhffhhfek vdduveegvdejudefgfevnecukfhppeejhedrudejvddrhedrvdefudenucevlhhushhtvg hrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghv vghrsegrkhhlrghvvghrrdgtohhm 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 C4526328005A; Fri, 25 Sep 2020 18:19:13 -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> <4975719e-8981-c2ff-1408-c348730b31c5@aklaver.com> <20200925211635.GA1544@hermes.hilbert.loc> From: Adrian Klaver Message-ID: <65a4f678-4c7f-d88b-3709-e0686a561461@aklaver.com> Date: Fri, 25 Sep 2020 15:19:12 -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: <20200925211635.GA1544@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 2:16 PM, Karsten Hilbert wrote: > On Fri, Sep 25, 2020 at 09:06:43AM -0700, Adrian Klaver wrote: > >>> 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) >> >> for ky in r0._index: >> print(r0[ky]) >> >> Where _index is a substitute for *.keys(). > > Sure, there's a number of solutions to my immediate problem, > the fitting of which is > > for key in dict(DictRow): > > That's the best fit because my > > def _escape_dict(the_dict, ...): > > was inaptly named. It should have been (and now is) > > def _escape_dict_like(dict_like, ...): > > within which > > dict(dict_like) > > is quite the thing to do despite having to make something a > duck which already nearly quacks like one is somehwat > unfortunate. I'm pretty sure DictRow has had the same behavior for some time so: Are you migrating from Python 2? Or what changed that made this show up? > > Karsten > -- > GPG 40BE 5B0E C98E 1713 AFA6 5BC0 3BEA AC80 7D4F C89B > > -- Adrian Klaver adrian.klaver@aklaver.com