rows = db(query).select()
repr_row = rows.repr(0)
for row in rows.repr():
print row.myfield
for row in rows[0:10].repr():
print row.myfield
repr_row = row.repr(0, fields=[db.mytable.myfield])
def repr(self, i=None, fields=None):
"""
Takes an index and returns a copy of the indexed row with values
transformed via the "represent" attributes of the associated fields.
If no index is specified, a generator is returned for iteration
over all the rows.
fields -- a list of fields to transform (if None, all fields with
"represent" attributes will be transformed).
"""
if i is None:
return (self.repr(i, fields=fields) for i in range(len(self)))
import sqlhtml
row = copy.copy(self.records[i])
keys = row.keys()
tables = [f.tablename for f in fields] if fields \
else [k for k in keys if k != '_extra']
for table in tables:
repr_fields = [f.name for f in fields] if fields \
else [k for k in row[table].keys()
if (hasattr(self.db[table], k) and
isinstance(self.db[table][k], Field)
and self.db[table][k].represent)]
for field in repr_fields:
row[table][field] = sqlhtml.represent(self.db[table][field],
row[table][field], row[table])
if self.compact and len(keys) == 1 and keys[0] != '_extra':
return row[keys[0]]
return row
def repr(self, i=None, fields=None):
"""
Takes an index and returns a copy of the indexed row with values
transformed via the "represent" attributes of the associated fields.
If no index is specified, a generator is returned for iteration
over all the rows.
fields -- a list of fields to transform (if None, all fields with
"represent" attributes will be transformed).
"""
if i is None:
return (self.repr(i, fields=fields) for i in range(len(self)))
import sqlhtml
row = copy.copy(self.records[i])
keys = row.keys()
tables = [f.tablename for f in fields] if fields \
else [k for k in keys if k != '_extra']
for table in tables:
repr_fields = [f.name for f in fields if f.tablename == table] if fields \
else [k for k in row[table].keys()
if (hasattr(self.db[table], k) and
isinstance(self.db[table][k], Field)
and self.db[table][k].represent)]
for field in repr_fields:
row[table][field] = sqlhtml.represent(self.db[table][field],
row[table][field], row[table])
if self.compact and len(keys) == 1 and keys[0] != '_extra':
return row[keys[0]]
return row
Anthony
--
-- 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/groups/opt_out.
I like it! In trunk. :-)
El viernes, 21 de junio de 2013 03:52:00 UTC-3, Massimo Di Pierro escribió:
I like it! In trunk. :-)
Is this ok (or should they return the same)?
>>> repr(rows)
'<Rows (3)>'
>>> rows.repr()
<generator object <genexpr> at 0xa86ea04>
rows.repr()
rows.represent()
rows.repr_fields()
rows.field_repr()
rows.render()
rows.display()
row_copy = rows(1) # uses the "represent" attributes by default
row_copy = rows(1, represent_fields=False) # just a plain copy -- no represent used
row_copy = rows(1, represent_fields=[db.mytable.myfield1, db.mytable.myfield2]) # represent specific fields
for row_copy in rows(): # iterate through all the rows
def __call__(self, i=None, represent_fields=True):
"""
Takes an index and returns a copy of the indexed row, by default
transforming each value via the "represent" attribute of the
associated fields.
If no index is specified, a generator is returned for iteration
over all the rows.
:represent_fields (defaults to True): indicates field values are to be
transformed via their "represent" attributes. Can also be a list of
specific fields to transform.
"""
if i is None:
return (self.__call__(i, represent_fields=represent_fields)
for i in range(len(self)))
import
sqlhtml
row = copy.deepcopy(self.records[i])
keys = row.keys()
if represent_fields:
field_list = isinstance(represent_fields, (list, tuple))
tables = [f.tablename for f in represent_fields] if field_list \
else [k for k in keys if k != '_extra']
for table in tables:
if field_list:
fields = [f.name for f in represent_fields
if f in self.db[table].fields]
else:
fields = [k for k in row[table].keys()
if (hasattr(self.db[table], k)
and isinstance(self.db[table][k], Field)
and self.db[table][k].represent)]
for field in fields:
row[table][field] = sqlhtml.represent(self.db[table][field],
row[table][field], row[table])
if self.compact and len(keys) == 1 and keys[0] != '_extra':
return row[keys[0]]
return row
OK, here are some options for the name:
rows.repr()
rows.represent()
rows.repr_fields()
rows.field_repr()
rows.render()
rows.display()
Another option might be to use the __call__ method and generalize it to return a copy of a row, optionally (and by default) transformed via the "represent" attributes of its fields. So, you would do:
row_copy = rows(1) # uses the "represent" attributes by default
row_copy = rows(1, represent_fields=False) # just a plain copy -- no represent used
row_copy = rows(1, represent_fields=[db.mytable.myfield1, db.mytable.myfield2]) # represent specific fields
for row_copy in rows(): # iterate through all the rows
Here's the code for the __call__ method (note, this also fixes a couple problems with the original method, such as doing a deepcopy instead of a copy):
def __call__(self, i=None, represent_fields=True):
"""
Takes an index and returns a copy of the indexed row, by default
transforming each value via the "represent" attribute of the
associated fields.
If no index is specified, a generator is returned for iteration
over all the rows.
:represent_fields (defaults to True): indicates field values are to be
transformed via their "
represent" attributes. Can also be a list of
specific fields to transform.
"""
if i is None:
return (self.__call__(i, represent_fields=represent_fields)
for i in range(len(self)))
import
sqlhtml
row = copy.deepcopy(self.records[i])
keys = row.keys()
if represent_fields:
field_list = isinstance(represent_fields, (list, tuple))
tables = [f.tablename for f in represent_fields] if field_list \
else [k for k in keys if k != '_extra']
for table in tables:
if field_list:
fields = [f.name for f in represent_fields
if f in self.db[table].fields]
else:
fields = [k for k in row[table].keys()
if (hasattr(self.db[table], k)
and isinstance(self.db[table][k], Field)
and self.db[table][k].represent)]
for field in fields:
row[table][field] = sqlhtml.represent(self.db[table][field],
row[table][field], row[table])
if self.compact and len(keys) == 1 and keys[0] != '_extra':
return row[keys[0]]
return row
Anthony
On Wednesday, June 26, 2013 4:45:55 AM UTC-4, Niphlod wrote:
Don't like the __call__ idea?.render() seems OK, but I thought it might imply that the entire row would be rendered to an HTML structure. I was thinking maybe something like .display() might be less confusing.
I'd use Rows.represent and would clarify the difference everywhere needed (book, docstrings). IMO It's not expect that a Rows object supports assignment as with the Field class nor override the Field class represent settings.
What do you mean by "assignment" and overriding the represent settings?