Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100466
| 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 | subclassing collections.Counter |
| Date | Tue, 15 Dec 2015 16:49:57 +0100 |
| Lines | 113 |
| Message-ID | <mailman.27.1450194613.22044.python-list@python.org> (permalink) |
| 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 | <pavlos.parissis@gmail.com> |
| 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 <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:100466 |
Show key headers only | View raw
[Multipart message — attachments visible in raw view] - view raw
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 = {'f':6, 'g':1}
In [8]: avg_metrics = ['f']
In [9]: a = CounterExt(avg_metrics=avg_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']
.........
RuntimeError: maximum recursion depth exceeded
Code::
from collections import Counter
from collections.abc import Mapping
class CounterExt(Counter):
def __init__(self, iterable=None, avg_metrics=None, **kwds):
self._counter = 1 # a zero value will raise ZeroDivisionError
self.avg_metrics = 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 = mapping.get
for elem in iterable:
mapping[elem] = mapping_get(elem, 0) + 1
"""
for elem in iterable:
self[elem] = 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=None, **kwds):
if iterable is not None:
if isinstance(iterable, Mapping):
if self:
self_get = self.get
self._counter +=1 # local modification
for elem, count in iterable.items():
self[elem] = count + self_get(elem, 0)
else:
super().update(iterable)
else:
self._count_elements(iterable)
if kwds:
self.update(kwds)
Back to comp.lang.python | Previous | Next | Find similar | Unroll thread
subclassing collections.Counter Pavlos Parissis <pavlos.parissis@gmail.com> - 2015-12-15 16:49 +0100
csiph-web