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


Groups > comp.lang.python > #100478 > unrolled thread

Re: subclassing collections.Counter

Started byIan Kelly <ian.g.kelly@gmail.com>
First post2015-12-15 10:55 -0700
Last post2015-12-15 10:55 -0700
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

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

#100478 — Re: subclassing collections.Counter

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-12-15 10:55 -0700
SubjectRe: subclassing collections.Counter
Message-ID<mailman.38.1450202154.22044.python-list@python.org>
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.

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web