Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100473
| Path | csiph.com!news.swapon.de!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:59:56 +0100 |
| Lines | 78 |
| Message-ID | <mailman.34.1450198801.22044.python-list@python.org> (permalink) |
| References | <567036A5.6050205@gmail.com> <n4pdtg$mdb$1@ger.gmane.org> <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 | <pavlos.parissis@gmail.com> |
| 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 <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:100473 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
On 15/12/2015 05:18 μμ, Pavlos Parissis wrote: > 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 > 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
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:59 +0100
csiph-web