Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100475

Re: subclassing collections.Counter

Path csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail
From Ian Kelly <ian.g.kelly@gmail.com>
Newsgroups comp.lang.python
Subject Re: subclassing collections.Counter
Date Tue, 15 Dec 2015 10:22:20 -0700
Lines 35
Message-ID <mailman.35.1450200183.22044.python-list@python.org> (permalink)
References <567036A5.6050205@gmail.com> <CALwzidnifp20WayShDXTGJ-PxdCk0LeZVb-o9fTQzFCN6i1dUQ@mail.gmail.com> <56703DE8.7010609@gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding quoted-printable
X-Trace news.uni-berlin.de S7+51AHe4IU0xJUwCbmWfwQpfRG54uYURZSyVWpqbGzg==
Return-Path <ian.g.kelly@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; 'indicated': 0.07; 'override': 0.07; 'subject:skip:c 10': 0.07; 'any.': 0.09; 'dict': 0.09; 'methods,': 0.09; 'metrics': 0.09; 'subclasses': 0.09; '9:20': 0.16; 'keys.': 0.16; 'magic': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'url:py': 0.16; 'wrote:': 0.16; 'attribute': 0.18; '>>>': 0.20; '2015': 0.20; 'class,': 0.22; 'doc': 0.22; 'maintains': 0.22; 'am,': 0.23; 'leave': 0.23; 'dec': 0.23; "haven't": 0.24; 'header:In-Reply-To:1': 0.24; "doesn't": 0.26; 'message-id:@mail.gmail.com': 0.27; 'change,': 0.27; 'values': 0.28; 'container': 0.29; 'extending': 0.29; 'far,': 0.29; 'thus,': 0.29; '15,': 0.30; 'extend': 0.31; 'probably': 0.31; "can't": 0.32; 'class': 0.33; 'problem': 0.33; 'source': 0.33; 'url:python': 0.33; 'items.': 0.33; 'right?': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'ones': 0.35; 'could': 0.35; 'returning': 0.35; 'but': 0.36; 'url:org': 0.36; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'method': 0.37; 'suggestion': 0.37; 'thought': 0.37; 'received:209': 0.38; 'hi,': 0.38; 'received:209.85.214': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'url:3': 0.60; 'your': 0.60; 'behavior': 0.61; "you'll": 0.61; 'real': 0.62; 'act': 0.67; 'sum': 0.69; 'dict,': 0.84; 'to:name:python': 0.84; 'url:cpython': 0.84; 'average.': 0.91; 'items,': 0.91; 'average': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=37ro0gvf97yQ9fjnmf5byH1nMjMAoncQi+ukpePVNrc=; b=AxpKNQK561Bfv19BzrgUlw0YuKPRvfGb7jf5LcNBZfAUVPGHdpaOy60IxJHp03WLTo eaKn/g1AEwUe4SS+LGDW1ZE4FPhNSBJGFkZs8KwKEsSyduobmSzl56aC7o3GT6Ta5HYK 7s7XBUF/J00JLrjGhbeqmh3DjH+ldFyWsrjFRkPhURL1lkgtN48QedPTvdLaB7pX9WOW bZYc1l+CbRxeLqkVHX1wapGdus7qXv80blq4mY0dQ1buNnCyDv2qe2HIQZdh2Ukp9zGB Q4kMYMMZ7If+4OtnKgZKvj8rtQa9pBOC4td/5ylFxSwyqlakoHAmZfp3e5nGiszo7Mq2 dlMQ==
X-Received by 10.182.220.162 with SMTP id px2mr30554655obc.56.1450200180249; Tue, 15 Dec 2015 09:23:00 -0800 (PST)
In-Reply-To <56703DE8.7010609@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:100475

Show key headers only | View raw


On Tue, Dec 15, 2015 at 9:20 AM, Pavlos Parissis
<pavlos.parissis@gmail.com> wrote:
> On 15/12/2015 05:11 μμ, Ian Kelly wrote:
>> On Tue, Dec 15, 2015 at 8:49 AM, Pavlos Parissis
>> <pavlos.parissis@gmail.com> wrote:
>>> 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.
>>
>> Leave Counter out of it, as this is not what it's designed for. Write
>> a custom Metrics class, with each attribute being a pseudo-collection
>> that maintains a sum or average.
>>
>
> But then I will have to override a lot of magic methods, right?
> What is the real problem of extending Counter in the way I did?

Only the ones that you have use for. So far, you haven't indicated
that you need any. All you said about your use case was "I need to
store values for metrics."

If you want your metrics container to act like a dict, then my
suggestion would be to just use a dict, with pseudo-collections for
the values as above.

> 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?

You can find the source at
https://hg.python.org/cpython/file/3.5/Lib/collections/__init__.py.
Counter subclasses dict but doesn't override items, so if you want to
change the behavior of items then you'll probably have to override
items.

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: subclassing collections.Counter Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-15 10:22 -0700

csiph-web