Path: csiph.com!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:18:31 +0100 Lines: 70 Message-ID: References: <567036A5.6050205@gmail.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="dt0hul9W3UIKgk1q91U3cqJ0tHIavtxTc" X-Trace: news.uni-berlin.de 2XNX41e+2rvfOdJtpFskiwzQfcJSYAfyUb5ooBoZn+aQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.011 X-Spam-Evidence: '*H*': 0.98; '*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; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'idea': 0.28; 'values': 0.28; 'depth': 0.29; 'invoke': 0.29; 'thus,': 0.29; 'extend': 0.31; 'anyone': 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=KZIfpEDw7xvvFV+m0kxslkDeEheERA+uFgFLk3cjtOI=; b=lK2oxKbSxCoKinlZZzIJLNsT9vCAXiSFPuW8F+VDTpb9mfy8FICcWEeeEJ50dq6uTh gOJEiAhb5rDGlh2RiMYg90UBu8t+jafO52BWhSh++TiAqkrmRnRQuPq5PhYzUQbjrJCP CnfgykcPWzP0Cj7trWplAi1DVLXC2jv8ofzN03n5AzLs4b5kvqLZ5TtdC11OX0zqqxsq /MdNflKJHipiX0Xkb0K9azrDmnWThIS+8HbSP5onE7RJTsoN9Sl9e7BQOvmtxNaIJ6lh pNFbsgcqyJkEp+/kwVBKjxzB1h68ZjaO5YedL4lvdK4W/1gFDxaFQ4HdwWBC5fqkiIW3 o5Dg== X-Received: by 10.194.5.74 with SMTP id q10mr44840666wjq.138.1450196314671; Tue, 15 Dec 2015 08:18:34 -0800 (PST) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0 In-Reply-To: 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:100470 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --dt0hul9W3UIKgk1q91U3cqJ0tHIavtxTc Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On 15/12/2015 05:08 =CE=BC=CE=BC, Peter Otten wrote: > Pavlos Parissis wrote: >=20 >> 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: maxim= um >> recursion depth exceeded as I access an attribute inside >> __getitem__. >> >> Does anyone has an idea how I can achieve this? >=20 >> class CounterExt(Counter): >=20 >> 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] >=20 > self[key] will call the CounterExt.__getitem__() method again. Use=20 > super().__getitem__(key) instead to invoke Counter.__getitem__(). >=20 >=20 >=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) Thank you very much, Pavlos --dt0hul9W3UIKgk1q91U3cqJ0tHIavtxTc 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 iQIcBAEBCAAGBQJWcD1YAAoJEIP8ktofcXa5tW0P/1X4NWe337F8BFhXa+jftgme W+b3SusnNKul0mPSAp2j7dBuNH8up8Prx3P8ysZM+G11DYlpbSIB/cBNsPsuQQ9g M7nIQoih4HWt076nrGAgX5ocDfakaN6CM2ceFA3FYoMEBPqSUm2RM6VMwaYAFmy1 hlWFT3T7W5SLyAjuxFV9gEOl9js5g5CpjtJSwWPwqiiGRzMSYBvKX6UKW/Oomefn xulxGlbOTanC6VOsWDw/+cSYsPZW5lpKeHz1R+UoYz7eo5fdvQgi/LJMCnrtogZ6 +jDCk0h8mISYGNsYeJhbay0YGEFyEZ7FfQttbame7A8dGj6Yj5F53pUf/X+eFcxd xnuz2cuyu4eQl9wjSmFeD3EOVld8Yk9nR+oxhI7lqkwYryx9K2cg2AWeSMqgpzuU ZtiBK+/sSdXKV0X1QczpyKVmezvlGHpN97p8SneMgm0Y0VsbwF6+GQj1DM4rbPTN vxpXtA6T06NMNFoqOvTWBLVddvzA+OQwKvIntVEEnf8sH6kzmtMLExX5LkVB8rGL HOQIyGoC+fiRY14KNxwz92en+CwlBmq9TuPiolCrrDihuZMP3LWd0VvV4PCtABHp W34QLfV0iFsPy5FZcPFedBEclXgQuJe0yYPWnfDL5R5ULs+Oy+N/BJ/kHZmvtMMF QNioCATw9598uncBEtqG =INNy -----END PGP SIGNATURE----- --dt0hul9W3UIKgk1q91U3cqJ0tHIavtxTc--