Method to extract a column from Rows

119 views
Skip to first unread message

Anthony

unread,
May 26, 2014, 1:36:34 PM5/26/14
to web2py-d...@googlegroups.com
How about we add a method like this to the dal.Rows class:

def column(self, column=None):
   
return [r[str(column) if column else self.colnames[0]] for r in self]

That way, you can do:

db(db.person).select().column('name')

and get a list of names. You can also do:

db().select(db.person.name).column()

and get the list of names without having to specify the "name" column as an argument. Is this useful?

Anthony

Massimo DiPierro

unread,
May 26, 2014, 1:41:55 PM5/26/14
to web2py-d...@googlegroups.com
I like it.

--
-- mail from:GoogleGroups "web2py-developers" mailing list
make speech: web2py-d...@googlegroups.com
unsubscribe: web2py-develop...@googlegroups.com
details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py-develop...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paolo Valleri

unread,
May 26, 2014, 2:27:26 PM5/26/14
to web2py-d...@googlegroups.com
I like it too.
Moreover regarding rows and row I have an annoying problem I would discuss to find out the right approach, my usual represents contain an in line if.
For example, given a table "student" with a field "name", the represent for the field name will be:

db.student.name.represent = lambda id, row: SPAN( row.name if 'name' in row else row.student.name), _class="a_special_class")

This code is the result of using the table student in a join or not. 
I've seen on the book that there is the possibility to set rows.compact =False, in this case my example will be something like:

db.student.name.represent = lambda id, row: SPAN( row.student.name ), _class="a_special_class")

However, it is not always feasible to set explicitly rows.compact = False. 
Currently I've in mind a new method, something like row.field('name') which will handle internally the issue I have reported. Or even better called column as the one proposed by Anthony for rows but in this case applied to row.
Thoughts?

Paolo

Anthony

unread,
May 27, 2014, 6:46:13 PM5/27/14
to web2py-d...@googlegroups.com
I can't easily generate a patch right now -- can you add that yourself, Massimo?

Anthony


On Monday, May 26, 2014 1:41:55 PM UTC-4, Massimo Di Pierro wrote:
I like it.

On May 26, 2014, at 12:36 PM, Anthony wrote:

How about we add a method like this to the dal.Rows class:

def column(self, column=None):
   
return [r[str(column) if column else self.colnames[0]] for r in self]

That way, you can do:

db(db.person).select().column('name')

and get a list of names. You can also do:

db().select(db.person.name).column()

and get the list of names without having to specify the "name" column as an argument. Is this useful?

Anthony


--
-- mail from:GoogleGroups "web2py-developers" mailing list

details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py-developers+unsubscribe@googlegroups.com.

Massimo DiPierro

unread,
May 27, 2014, 10:02:45 PM5/27/14
to web2py-d...@googlegroups.com

Done yesterday. :-)


details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py-develop...@googlegroups.com.

Anthony

unread,
May 28, 2014, 7:26:19 AM5/28/14
to web2py-d...@googlegroups.com
I believe row['student.name'] will work whether or not compact=True (in that case, when it fails to find the key "student.name", it looks for just "name"). Is that a sufficient workaround?

Anthony
I like it.


details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py-developers+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

--
-- mail from:GoogleGroups "web2py-developers" mailing list

details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py-developers+unsubscribe@googlegroups.com.

Paolo Valleri

unread,
May 28, 2014, 7:43:32 AM5/28/14
to web2py-d...@googlegroups.com
it words, thanks Anthony


 Paolo



details : http://groups.google.com/group/web2py-developers
the project: http://code.google.com/p/web2py/
official : http://www.web2py.com/
---
You received this message because you are subscribed to the Google Groups "web2py-developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to web2py-develop...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages