Some row questions

46 views
Skip to first unread message

Dave S

unread,
Jun 23, 2017, 12:29:00 PM6/23/17
to web2py-users
I have a fairly straight forward table, and a fairly easy db query:
db(db.segment.parent == request.vars.id).select()

For my use case, this returns a Rows object of 3 to 10 Row objects, and I do some simple calculations.
for row in rows:
 
if row.x:
     term1
= row.term
 
if row.y:
     term2  
= row.term
return dict(rows=rows, mycalc = calc(term1,term2))



(ok, x is really "isstart" and y is "isstop", flags to mark endpoints, but that's being overly specific)

I'd also like to compute a delta between the row.term value of each row and it's predecessor (by data entry convention, the table is ordered by id).  So the 2 questions are:
  • can you tell the index in rows of the row you are working on?  (I guess this is a Python question, not just web2py).  In js, the map and reduce functions know the index in the array they have as input; is something similar available in a Python iteration?
  • can you add fields to a Row object, or to the parent Rows object, and have the view display it, or is a virtual field definition needed?
Thanks.

Dave S
/dps

villas

unread,
Jun 23, 2017, 4:24:30 PM6/23/17
to web2py-users
Hi Dave,

Open your app on the commandline:

python web2py.py -S yourapp -M

Experiment a little...

1.  for i, row in enumerate(rows)
2.  rows[0].test = 'whatever'

Dave S

unread,
Jun 23, 2017, 8:35:28 PM6/23/17
to web2py-users


On Friday, June 23, 2017 at 1:24:30 PM UTC-7, villas wrote:
Hi Dave,

Open your app on the commandline:

python web2py.py -S yourapp -M

Experiment a little...

1.  for i, row in enumerate(rows)
2.  rows[0].test = 'whatever'

I've kinda already done that.  No tee-shirt appeared.

/dps
 

villas

unread,
Jun 24, 2017, 10:19:37 AM6/24/17
to web...@googlegroups.com

I've kinda already done that.  No tee-shirt appeared.


I guess we don't always get what we'd like.  :)

So why doesn't enumerate() provide an index?  Or, maybe that's not the kind of index you wanted?

If you add something to the rows object, why isn't it available in the view?

Try this:

def test():
    rows = db(db.yourtable.id < 20).select(db.yourtable.id
    for i, r in enumerate(rows):   
        rows[i].test = 'my_extra_value_%s' % i
    tbl = TABLE([ TR(j.id, j.test) for j in rows ])
    return dict(tbl=tbl)

Reply all
Reply to author
Forward
0 new messages