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


Groups > comp.lang.python > #100476

Re: subclassing collections.Counter

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 18:43:05 +0100
Lines 84
Message-ID <mailman.36.1450201396.22044.python-list@python.org> (permalink)
References <567036A5.6050205@gmail.com> <CALwzidnifp20WayShDXTGJ-PxdCk0LeZVb-o9fTQzFCN6i1dUQ@mail.gmail.com> <56703DE8.7010609@gmail.com> <CALwzid=DCLsBXMtWYtScP9pZYzXPU6rRFa8hzC3GD8hru3iQSQ@mail.gmail.com>
Mime-Version 1.0
Content-Type multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="N0QOECxw3vGgQjmhTCp5K2dkofSoMdOoJ"
X-Trace news.uni-berlin.de FO0Whygwi5920b4peDEMSQqiYj1C0SfVdIGYozG5RDFA==
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.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'indicated': 0.07; 'method,': 0.07; 'override': 0.07; 'subject:skip:c 10': 0.07; 'any.': 0.09; 'dict': 0.09; 'highlight': 0.09; 'methods,': 0.09; 'metrics': 0.09; 'subclasses': 0.09; 'itself.': 0.11; 'pushed': 0.13; '9:20': 0.16; 'correctly,': 0.16; 'filename:fname piece:signature': 0.16; 'keys.': 0.16; 'magic': 0.16; 'received:192.168.0.103': 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; 'saying': 0.22; 'class,': 0.22; 'doc': 0.22; 'maintains': 0.22; 'cheers,': 0.22; 'am,': 0.23; 'leave': 0.23; 'dec': 0.23; "haven't": 0.24; 'header :In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; "doesn't": 0.26; 'change,': 0.27; 'function': 0.28; 'this.': 0.28; '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; 'message- id:@gmail.com': 0.34; 'tue,': 0.34; 'received:google.com': 0.35; 'ones': 0.35; 'could': 0.35; 'returning': 0.35; 'received:74.125.82': 0.35; 'but': 0.36; 'needed': 0.36; 'url:org': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'being': 0.37; 'method': 0.37; 'thanks': 0.37; 'suggestion': 0.37; 'thought': 0.37; 'hi,': 0.38; 'received:192': 0.39; 'rather': 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; 'times': 0.63; 'act': 0.67; 'sum': 0.69; 'dict,': 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=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type; bh=4qx9IY5JUyGNTB0efyGaffTnYd/lxlqBeB1D1usCUL4=; b=j10DIs72nmC4LSNoJJeFu5f4n7aPD+4MscZ4fx8zwZNCotu54qfUdIGPZ2tduOvl+1 vnd/8sV4eO0YMNE02r0uzekfzTyT1wZZIJLBeKlVceS7X0T9SHa+1+bViPvpqoEV4gwF Y50JlBi5rLJaBdRhbMr/9ImAQ4TtCtWVqPul0AYaCT1Egx4oDgPsbuTdF1VlsHwqq0Cc 4xJdgECcRTTgcW2hSJmcSSN5U/EvHjyGO4K+4wOGM8F/OvSEFOxMULt2FgFc4ntxJj/T xhsfKOU/48J2I5D7AOYasKTMCHQ0KzUnzQOJ1QwP4qHyWYJobyPOvNOSVFrgEn5XeGKs +KYg==
X-Received by 10.194.79.201 with SMTP id l9mr51270338wjx.151.1450201389133; Tue, 15 Dec 2015 09:43:09 -0800 (PST)
User-Agent Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Icedove/31.8.0
In-Reply-To <CALwzid=DCLsBXMtWYtScP9pZYzXPU6rRFa8hzC3GD8hru3iQSQ@mail.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:100476

Show key headers only | View raw


[Multipart message — attachments visible in raw view] - view raw

On 15/12/2015 06:22 μμ, Ian Kelly wrote:
> 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."
> 

I needed the update() to do the addition rather the override.

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

If I understood you correctly, you are saying store all metrics in a
dict and have a counter key as well to store the times metrics are
pushed in, and then have a function to do the math. Am I right?

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

I thought that items() calls a magic method, but it is actually a method
itself. Thanks for highlight this.

Cheers,
Pavlos

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


Thread

Re: subclassing collections.Counter Pavlos Parissis <pavlos.parissis@gmail.com> - 2015-12-15 18:43 +0100

csiph-web