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


Groups > comp.lang.python > #100478

Re: subclassing collections.Counter

Path csiph.com!news.swapon.de!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:55:05 -0700
Lines 32
Message-ID <mailman.38.1450202154.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> <56705129.2020004@gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset=UTF-8
X-Trace news.uni-berlin.de mHadLf8f1S1RlO//lWdWjwdA82UKxhvSiWgbpSmPnXcQ==
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.013
X-Spam-Evidence '*H*': 0.97; '*S*': 0.00; 'subject:skip:c 10': 0.07; '@property': 0.09; 'dict': 0.09; 'metrics': 0.09; 'pushed': 0.13; 'def': 0.13; 'add(self,': 0.16; 'correctly,': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'wrote:': 0.16; '2015': 0.20; 'work,': 0.21; 'saying': 0.22; 'am,': 0.23; 'dec': 0.23; 'this:': 0.23; 'header:In-Reply-To:1': 0.24; 'message-id:@mail.gmail.com': 0.27; 'function': 0.28; 'values': 0.28; 'container': 0.29; '15,': 0.30; 'skip:_ 10': 0.32; 'class': 0.33; 'right?': 0.33; 'tue,': 0.34; 'received:google.com': 0.35; 'something': 0.35; 'received:209.85': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'received:209.85.213': 0.37; 'suggestion': 0.37; 'received:209': 0.38; 'to:addr:python.org': 0.40; 'your': 0.60; 'times': 0.63; 'act': 0.67; '10:43': 0.84; 'dict,': 0.84; 'to:name:python': 0.84
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; bh=gaqk2JZu+0+XCoqzqM8lKOKD0uvvZ86FWps4PtEAVpc=; b=fAbcB2+V06jqFMygj8VqAJ9Y1cdxXZkm8Pew/FmlyMKQciQYdUFomevHYK/fB9CfMy JHIJI2EhkjYsO5uPZ6jMcRRQiPtDUiSy51mJTSJY/PQjS0DMjE+RBUP5mrSJkCbeKny0 JARwBVKZOR03lQsLgMTBTdYX8A3fhcXB3LxAqLCe5sLLTNZLiMVd5YqOx99Dy3gCP32a oOKImAY9MdXY85txF0x8b+GUQMykgQtbmusiyCxDW3hn9icsELB0HKZ3oBLpRJuT4rWQ kBLXLD3GjYhzFbMWnZYXmDK6abXNchTHlQUEfJgLLIpcVMCGVw+1ECJyxm/jn2XyV2S0 Fc/g==
X-Received by 10.50.134.137 with SMTP id pk9mr5423057igb.93.1450202145302; Tue, 15 Dec 2015 09:55:45 -0800 (PST)
In-Reply-To <56705129.2020004@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:100478

Show key headers only | View raw


On Tue, Dec 15, 2015 at 10:43 AM, Pavlos Parissis
<pavlos.parissis@gmail.com> wrote:
>> 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?

That would work, although I was actually thinking of something like this:

class SummedMetric:
    def __init__(self):
        self.total = 0
        self.count = 0

    @property
    def average(self):
        return self.total / self.count

    def add(self, value):
        self.total += value
        self.count += 1

metrics = {}
for metric_name in all_metrics:
    metrics[metric_name] = SummedMetric()

For averaged metrics, look at metrics['f'].average, otherwise look at
metrics['f'].total.

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:55 -0700

csiph-web