librelist archives

« back to archive

How to calculate the average of an attribute?

How to calculate the average of an attribute?

From:
Clodoaldo Neto
Date:
2012-06-19 @ 15:43
Similarly to the sum filter I would like to have an average filter so I
created one but I suspect I'm over doing it and there is something simpler.
Is there? This is what I did:

{% for group in (rs['size'] | groupby('yw') | sort(True))[:20] -%}
...
   {%- for l in group.list %}
        {%- endfor -%}
   ...
        {% set am = (group.list | avg(attribute='am')) %}


@app.template_filter()
def avg(value, attribute):

    l = list()
    for d in value:
        if d[attribute] is not None:
            l.append(d[attribute])

    if len(l) == 0:
        return 0
    else:
        return float(sum(l)) / len(l)

Regards, Clodoaldo

Re: [flask] How to calculate the average of an attribute?

From:
Simon Sapin
Date:
2012-06-20 @ 07:41
Le 19/06/2012 17:43, Clodoaldo Neto a écrit :
> @app.template_filter()
> def  avg(value,  attribute):
>      l=  list()
>      for  din  value:
>          if  d[attribute]  is  not  None:
>              l.append(d[attribute])
>
>
>      if  len(l)  ==  0:
>          return  0
>      else:
>          return  float(sum(l))  /  len(l)

This filter looks alright to me. Does it work like you want? Do you have 
any problem with it?

Maybe you could use a generator to get the sum and length without 
copying a whole list of values in memory, but don’t worry about this 
until you have huge lists.

More generally, such of calculation is the kind of things I would do in 
Python before calling the template, and pass the result as parameter to 
the template. But this is really a matter of taste.

Regards,
-- 
Simon Sapin

Re: [flask] How to calculate the average of an attribute?

From:
Brice Leroy
Date:
2012-06-20 @ 15:48
Just thought, must of us are getting data from database, so why not using 
db AVG function (if available) when retrieving your data?

Brice

On Jun 20, 2012, at 12:41 AM, Simon Sapin <simon.sapin@exyr.org> wrote:

> Le 19/06/2012 17:43, Clodoaldo Neto a écrit :
>> @app.template_filter()
>> def  avg(value,  attribute):
>>     l=  list()
>>     for  din  value:
>>         if  d[attribute]  is  not  None:
>>             l.append(d[attribute])
>> 
>> 
>>     if  len(l)  ==  0:
>>         return  0
>>     else:
>>         return  float(sum(l))  /  len(l)
> 
> This filter looks alright to me. Does it work like you want? Do you have 
> any problem with it?
> 
> Maybe you could use a generator to get the sum and length without 
> copying a whole list of values in memory, but don’t worry about this 
> until you have huge lists.
> 
> More generally, such of calculation is the kind of things I would do in 
> Python before calling the template, and pass the result as parameter to 
> the template. But this is really a matter of taste.
> 
> Regards,
> -- 
> Simon Sapin
> 
> 

Re: [flask] How to calculate the average of an attribute?

From:
Clodoaldo Neto
Date:
2012-06-22 @ 11:30
2012/6/20 Brice Leroy <bbrriiccee@gmail.com>

> Just thought, must of us are getting data from database, so why not using
> db AVG function (if available) when retrieving your data?
>
>
The sql query is used to generate data for two presentations. One is a html
table where the average is shown and the other is a graphic line chart.
Both go on the same page so it is one only query. I want to keep
presentation specifics to the presentation layer.

Clodoaldo


> Brice
>
> On Jun 20, 2012, at 12:41 AM, Simon Sapin <simon.sapin@exyr.org> wrote:
>
> > Le 19/06/2012 17:43, Clodoaldo Neto a écrit :
> >> @app.template_filter()
> >> def  avg(value,  attribute):
> >>     l=  list()
> >>     for  din  value:
> >>         if  d[attribute]  is  not  None:
> >>             l.append(d[attribute])
> >>
> >>
> >>     if  len(l)  ==  0:
> >>         return  0
> >>     else:
> >>         return  float(sum(l))  /  len(l)
> >
> > This filter looks alright to me. Does it work like you want? Do you have
> > any problem with it?
> >
> > Maybe you could use a generator to get the sum and length without
> > copying a whole list of values in memory, but don’t worry about this
> > until you have huge lists.
> >
> > More generally, such of calculation is the kind of things I would do in
> > Python before calling the template, and pass the result as parameter to
> > the template. But this is really a matter of taste.
> >
> > Regards,
> > --
> > Simon Sapin
> >
> >
>