Something that would work like .count() and .first() and .last()

select_avg =
db(db.star_ratings.post_date>"2009-10-01").select(db.star_ratings.score).avg(star_ratings.score)

In rows, there will be a reference to the aggregator function.

self.avg, self.sum, self.max... etc.

These will be set, based on what backend the database is using. so

if db == "a_db_that_does_not_support_average_backend":
    self.avg = python_avg

and

#this is just an example for one field, of course it would need to be able
to avg multiple rows together perhaps?
def python_avg(rows, field):
    avg = 0
    total = 0
    count = 0

    for row in rows:
        total += row[field]
        count += 1

    if count == 0:
        avg = -1
    else:
        avg = total / count
    return avg

and then when you select the row, and call .avg() it will reference the
correct function for that db,. which might alter the SQL statement.

This allows for easy polymorphism, that allows us to take advantage of the
db functions, but still have python fallbacks to use this functionality.

-Thadeus




On Wed, Oct 14, 2009 at 8:09 AM, mr.freeze <[email protected]> wrote:

>
> Sorry, bad example.  I mean implement the common functions (avg and
> round) then add a generic function like this so that developers would
> have the option of using unsupported functions for their particular
> database:
>
> test = db(db.test.id > 0).select(db.test.number.sql
> ("FORMAT","date","YYYY-MM-DD"))
>
> On Oct 14, 7:42 am, mdipierro <[email protected]> wrote:
> > Why not implement it as the others?
> >
> > db.test.number.average()
> >
> > On Oct 13, 11:59 pm, "mr.freeze" <[email protected]> wrote:
> >
> > > What about a generic sql function for Field? Here is a proof of
> > > concept:
> > > -------------------------
> > > def sql(self, name, data_type, *args):
> > >     out = ''
> > >     if args:
> > >         out = "," + ",".join([str(a) for a in args])
> > >     return Expression('%s(%s%s)' % (name, str(self), out), data_type,
> > > self._db)
> > > -------------------------
> > > test = db(db.test.id > 0).select(db.test.number.sql("AVG",'double'))
> > > -------------------------
> > > {{for t in test:}}
> > > {{=str(t._extra['AVG(test.number)'])}}
> > > <br />
> > > {{pass}}
> > > -------------------------
> >
> > > The syntax needs to be cleaned up and integrated better but you get
> > > the idea. What do you think?
> >
> > > On Oct 13, 10:12 pm, mdipierro <[email protected]> wrote:
> >
> > > > On Oct 13, 9:02 pm, "mr.freeze" <[email protected]> wrote:
> >
> > > > > This has probably been asked before but why doesn't the Field class
> > > > > have these?:
> >
> > > > no
> >
> > > > > avg, format, round, mid, len, first, last
> >
> > > > > Is it because they are not common to all databases?
> >
> > > > yes
> >
> > > > probably avg and round should be implemented since they are common.
> >
> >
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/web2py?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to