Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Pavlos Parissis Newsgroups: comp.lang.python Subject: Re: subclassing collections.Counter Date: Tue, 15 Dec 2015 17:59:56 +0100 Lines: 78 Message-ID: References: <567036A5.6050205@gmail.com> <56703D57.1080209@gmail.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="8mkOx1h7evbrAlCLP1NCcmNkhEs86QkSn" X-Trace: news.uni-berlin.de qESbU8zbCcEcJoubsL+XywA8x6K48liiJcXohSo2H4Gw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.007 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'subject:skip:c 10': 0.07; 'metrics': 0.09; 'modifies': 0.09; 'def': 0.13; 'filename:fname piece:signature': 0.16; 'increment': 0.16; 'key):': 0.16; 'keys.': 0.16; 'received:192.168.0.103': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'self[key]': 0.16; 'wrote:': 0.16; 'attribute': 0.18; '>>>': 0.20; 'doc': 0.22; 'cheers,': 0.22; 'header:In-Reply-To:1': 0.24; 'header:User- Agent:1': 0.26; "doesn't": 0.26; 'change,': 0.27; 'idea': 0.28; 'values': 0.28; 'depth': 0.29; 'invoke': 0.29; 'thus,': 0.29; 'extend': 0.31; 'anyone': 0.32; "can't": 0.32; 'skip:_ 10': 0.32; 'class': 0.33; 'message-id:@gmail.com': 0.34; 'this?': 0.34; 'received:google.com': 0.35; 'could': 0.35; 'returning': 0.35; 'received:74.125.82': 0.35; 'instead': 0.36; 'to:addr:python- list': 0.36; 'subject:: ': 0.37; 'method': 0.37; 'suggestion': 0.37; 'thought': 0.37; 'thank': 0.38; 'does': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'your': 0.60; 'maximum': 0.61; 'sum': 0.69; 'exceeded': 0.83; 'otten': 0.84; 'average': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type; bh=2trqB8PBTzrhiFVZZjjy9hugaNhcLe6bZ0i3dQsCl3c=; b=ramD8i8sFd3DH0JatBhn/zBPTACJBUCCtPComvCxztapZD4dQusj+UZo+a3mb1sMMd bxRF+yL9DWOgIdLHozapliV/mQ6vGqkxWrJvayC/tTDdMTQ3dPb9AQMOwHm4bzWErt8h YQ7fIDLDvf70A4SQXXSDXJQrD9lftE1/SF/JMHV74UXFvNk21x9QKojfIyq0/K7KDs+9 NFT5X+weJPkU/WkKJlZFFlHDzPYsm6flNN+WqdRKb7byufUhBgJ8Y2HcXzGhwOXAFUtn xEQmAVnHYxdzUOndKMgxokHRkRNZkHK/o0StLaaMYnsQdUEX+RfJTnSh+DPBHIfROa+S iBSA== X-Received: by 10.28.183.198 with SMTP id h189mr5789157wmf.44.1450198799495; Tue, 15 Dec 2015 08:59:59 -0800 (PST) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0 In-Reply-To: <56703D57.1080209@gmail.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100473 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --8mkOx1h7evbrAlCLP1NCcmNkhEs86QkSn Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable On 15/12/2015 05:18 =CE=BC=CE=BC, Pavlos Parissis wrote: > On 15/12/2015 05:08 =CE=BC=CE=BC, Peter Otten wrote: >> Pavlos Parissis wrote: >> >>> I need to store values for metrics and return the average for some >>> and the sum for the rest. Thus, I thought I could extend >>> collections.Counter class by returning averages for some keys. >>> >>> My class modifies the update() to increment a counter and the >>> __getitem__ to perform the calculation. But, I get RuntimeError: maxi= mum >>> recursion depth exceeded as I access an attribute inside >>> __getitem__. >>> >>> Does anyone has an idea how I can achieve this? >> >>> class CounterExt(Counter): >> >>> def __getitem__(self, key): >>> if (self.avg_metrics is not None and key in self.avg_metrics)= : >>> return self[key] / self._counter >>> else: >>> return self[key] >> >> self[key] will call the CounterExt.__getitem__() method again. Use=20 >> super().__getitem__(key) instead to invoke Counter.__getitem__(). >> >> >> >=20 > I applied your suggestion and worked! > def __getitem__(self, key): > if (self.avg_metrics is not None and key in self.avg_metrics): > return super().__getitem__(key) / self.__counter > else: > return super().__getitem__(key) >=20 > Thank you very much, > Pavlos >=20 Calling items() over the object doesn't call __getitem__ and I can't find in the doc which method I need to change, any ideas? Cheers, Pavlos --8mkOx1h7evbrAlCLP1NCcmNkhEs86QkSn Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJWcEcMAAoJEIP8ktofcXa5oWoP/Ry8L/QlF67H93nMFhjhbYHc VMRTcqNbfc11K7M13XzYqKDi5lkj4LU9q/cwSa4IUDhNkYym0zWdCNLApXUS2a02 C3IKOZ/xIP1DPLqmrW9j/VPVvZ8GjqkjvNaMHNU1Yc/UE8XVVPYKMilyZ5VuUbHZ DFIBzfJRs1FDHeSrrB982wSUXRAffQp14lccgmv5FhVnX5qW8GBICfmayUFHUgSe 2wdg2NT8o1Rc3wPX5s984RjOUEJlPe0ybk9DeKRM73TTZ54YZgo0yZ6+16T3Tz8r yy9cJT1sLxVBjGENh13ciC0Qjvr7ya7QTMzr30fI66cbXSpMabGZmfc2hwjSBo5C O3qZc5uco+ufvY2chBE0XcT13ixp81ABwFEyPKrHVDboG8n5S/p/ZM4LqWWKTNoK /LA5FdymlfXlqHQTGeZLcSeu8+l3ld2aWc3qmh5bQHyujCBpudcfRPWkH7e5vZK6 66xXooThwADzVGasbEBXWU41PvYsgIXMgrNTeiCPAFkv1vscbrKF2SSOIIkWc8Nz EPTmWPqeEe3GsL3bLRSM3ddA+AcuyIIUV0XefXc4IO1j3PxeLgKbk9W0oN1xewhG snMD1Hs0iuW9di9m2cUP5Q1ecMweLeLFExR7xdIcGcMMwN9qTXrqBt3yGPiQYZWg E4ayjIIE+hjVA1+jEJ81 =CM7F -----END PGP SIGNATURE----- --8mkOx1h7evbrAlCLP1NCcmNkhEs86QkSn--