Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100470
| Path | csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail |
|---|---|
| From | Pavlos Parissis <pavlos.parissis@gmail.com> |
| Newsgroups | comp.lang.python |
| Subject | Re: subclassing collections.Counter |
| Date | Tue, 15 Dec 2015 17:18:31 +0100 |
| Lines | 70 |
| Message-ID | <mailman.30.1450196320.22044.python-list@python.org> (permalink) |
| References | <567036A5.6050205@gmail.com> <n4pdtg$mdb$1@ger.gmane.org> |
| 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 | <pavlos.parissis@gmail.com> |
| 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 | <n4pdtg$mdb$1@ger.gmane.org> |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.20+ |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:100470 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On 15/12/2015 05:08 μμ, 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: maximum
>> 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
> super().__getitem__(key) instead to invoke Counter.__getitem__().
>
>
>
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
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
Re: subclassing collections.Counter Pavlos Parissis <pavlos.parissis@gmail.com> - 2015-12-15 17:18 +0100
csiph-web