Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Pavlos Parissis Newsgroups: comp.lang.python Subject: subclassing collections.Counter Date: Tue, 15 Dec 2015 16:49:57 +0100 Lines: 113 Message-ID: Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="cma0F61spHAPtxSniHUC4kS9po3welfgD" X-Trace: news.uni-berlin.de JMYY3zGtxXo6mbm6F01usQ7Y8p/FfaXP5TnWyvMsC1CQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'else:': 0.03; '"""': 0.05; 'none:': 0.05; 'subject:skip:c 10': 0.07; 'collections': 0.09; 'metrics': 0.09; 'modifies': 0.09; 'def': 0.13; 'modification': 0.15; '.........': 0.16; '2})': 0.16; 'elem': 0.16; 'elem,': 0.16; 'filename:fname piece:signature': 0.16; 'increment': 0.16; 'iterable': 0.16; 'iterable:': 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.get': 0.16; 'self[elem]': 0.16; 'self[key]': 0.16; 'self_get': 0.16; 'attribute': 0.18; 'copied': 0.18; 'elements': 0.23; 'import': 0.24; 'header:User-Agent:1': 0.26; 'skip:_ 20': 0.26; 'idea': 0.28; 'values': 0.28; 'depth': 0.29; 'thus,': 0.29; 'raise': 0.29; 'extend': 0.31; 'skip:s 30': 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; 'skip:c 30': 0.35; 'could': 0.35; 'mapping': 0.35; 'returning': 0.35; 'received:74.125.82': 0.35; 'skip:i 20': 0.36; 'to:addr :python-list': 0.36; '12,': 0.37; 'thought': 0.37; 'skip:z 10': 0.38; 'hi,': 0.38; 'does': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'maximum': 0.61; 'sum': 0.69; 'exceeded': 0.83; '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 :content-type; bh=AQqezneMe0CGI8OmylRwJRaSaWc8b9vWWJPy0APoubI=; b=Z38QsNP2iZmSKrMnWymtGDKcvqaW8RmstiGU3KzWqM7lFsajJfJHq0b8h+shlsidx2 NNdSbwKkMONHM6948/MFxaQqtAWngeBQclnoxTB7qxMu0thOh8BvmtD/uTaZP+YahKly HIeH8D8N5TS4R6NMAdtRRrqd+x5YVUmKbHi31T6m2ynQmLuqsRwxETDeSJSBefkPyPCq 2KXkJT3fE8X+DngwRQTFawRplZ5pH6hhlMrc5gb3DBDMX+mmqsMlzqeQZRtDgwmTkpUg 2iY2RUuhcdwQkTPoJS9WYQyhDKFHWsJUO6xG1XbKmFgUolDHpOuNSRZZTlMjXITwtx+M ChmQ== X-Received: by 10.28.97.133 with SMTP id v127mr2843967wmb.73.1450194605276; Tue, 15 Dec 2015 07:50:05 -0800 (PST) User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0 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:100466 This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --cma0F61spHAPtxSniHUC4kS9po3welfgD Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi, 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? Example:: In [7]: metrics =3D {'f':6, 'g':1} In [8]: avg_metrics =3D ['f'] In [9]: a =3D CounterExt(avg_metrics=3Davg_metrics) In [10]: a.update(metrics) In [11]: a Out[11]: CounterExt({'f': 6, 'g': 1}) In [12]: a.update(metrics) In [13]: a Out[13]: CounterExt({'f': 12, 'g': 2}) In [14]: a['f'] =2E........ RuntimeError: maximum recursion depth exceeded Code:: from collections import Counter from collections.abc import Mapping class CounterExt(Counter): def __init__(self, iterable=3DNone, avg_metrics=3DNone, **kwds): self._counter =3D 1 # a zero value will raise ZeroDivisionError self.avg_metrics =3D avg_metrics super().__init__() self.update(iterable, **kwds) def _count_elements(self, iterable): """Tally elements from the iterable. This is copied from collections/__init__.py original code:: def _count_elements(mapping, iterable): 'Tally elements from the iterable.' mapping_get =3D mapping.get for elem in iterable: mapping[elem] =3D mapping_get(elem, 0) + 1 """ for elem in iterable: self[elem] =3D self.get(elem, 0) + 1 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] def update(self, iterable=3DNone, **kwds): if iterable is not None: if isinstance(iterable, Mapping): if self: self_get =3D self.get self._counter +=3D1 # local modification for elem, count in iterable.items(): self[elem] =3D count + self_get(elem, 0) else: super().update(iterable) else: self._count_elements(iterable) if kwds: self.update(kwds) --cma0F61spHAPtxSniHUC4kS9po3welfgD 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 iQIcBAEBCAAGBQJWcDaqAAoJEIP8ktofcXa5dggP/Rco88ZW7Eted83pZYOPiLFi JsBNBGrH5HQB4fUgQF31pdGKkttTcNQ/yKx/P7CpoISyuf1oz4AR1L+ugOFkpozi d1kUmnWDyk9Mk2YJyql2LZ0J12jobFJHgScouRXwqsgMTh2uWjySoxhDRrBHdhmM 8l88CDpJ85uCPZ+bvV9aDsHkwMRadYVC6nS/365ZlOvWheZZdisqLYf4K40k+o2a KcRx3vovWtRPGIWrwem0PjI8S9lClK8VyKsrCjb1c4nVURXyMZEHUxkKl7POjKKP MfNQXijwEhZW+A/muOAc2UtVTJcSHXzsmMa8q4XypU5f7ew5s2dDoz2WCE9FCIi3 RnTwxvgQUKohGzDTYRImpUqGzNM5u0sxOwh2GqCKFkn6XuxruVS6ooqZ5HMjx0VD 9NXad+GQLwQEF/2Vv6KPalWDdqMi64vmYK5JqIc9+8kJa0QwVMdz4tIM9M+I1S+X qmwVsDoWuqhhNLsgSj8giTUf+m+l+uleMvti6zZwERvNV/EQaDyvYhpSUuqkTzIA XO0qZAja7gh8BWf/NpjMMkY2Mxo75DHLLQEjLGYvsFK9S8qxEWYZ/W/McD5KR+yc GFL84oKj8b2VOA+UdlkZoDrAFCxbiGDmEWEDqb/DKZr/6UR9VDH6zJUsvpzDBbSR cyz6rPqT5YvcKGQUF8ei =OYQj -----END PGP SIGNATURE----- --cma0F61spHAPtxSniHUC4kS9po3welfgD--