Package web2py :: Package gluon :: Module dal :: Class Rows

[hide private]
[frames] | no frames]

# Class Rows

source code

    object --+
             |
            Rows

A wrapper for the return value of a select. It basically represents a table. It has an iterator and each row is represented as a dictionary.

Instance Methods | [hide private]

__init__(self, db=global_settings.applications_parent, records=`[``]`, colnames=`[``]`, compact=True, rawrows=global_settings.applications_parent) x.__init__(...) initializes x; see x.__class__.__doc__ for signature | source code

setvirtualfields(self, **keyed_virtualfields) db.define_table('x',Field('number','integer'))... | source code

__and__(self, other) | source code

__or__(self, other) | source code

__nonzero__(self) | source code

__len__(self) | source code

__getslice__(self, a, b) | source code

__getitem__(self, i) | source code

__iter__(self) iterator over records | source code

__str__(self) serializes the table into a csv file | source code

first(self) | source code

last(self) | source code

find(self, f) returns a new Rows object, a subset of the original object, filtered by the function f | source code

exclude(self, f) removes elements from the calling Rows object, filtered by the function f, and returns a new Rows object containing the removed elements | source code

sort(self, f, reverse=True) returns a list of sorted elements (not sorted in place) | source code

group_by_value(self, field) regroups the rows, by one of the fields | source code

as_list(self, compact=True, storage_to_dict=True, datetime_to_str=True) returns the data as a list or dictionary. | source code

as_dict(self, key=`'``id``'`, compact=True, storage_to_dict=True, datetime_to_str=True) returns the data as a dictionary of dictionaries (storage_to_dict=True) or records (False) | source code

export_to_csv_file(self, ofile, null=`'``<NULL>``'`, *args, **kwargs) export data to csv, the first line contains the column names... | source code

xml(self) serializes the table using sqlhtml.SQLTABLE (if present) | source code

json(self, mode=`'``object``'`, default=global_settings.applications_parent) serializes the table to a JSON list of objects | source code

Inherited from `object`: `__delattr__`, `__getattribute__`, `__hash__`, `__new__`, `__reduce__`, `__reduce_ex__`, `__repr__`, `__setattr__`

Properties | [hide private]

Inherited from `object`: `__class__`

Method Details | [hide private]

### __init__(self, db=global_settings.applications_parent, records=`[``]`, colnames=`[``]`, compact=True, rawrows=global_settings.applications_parent)
(Constructor)

source code

x.__init__(...) initializes x; see x.__class__.__doc__ for signature

Overrides: object.__init__
  (inherited documentation)

### setvirtualfields(self, **keyed_virtualfields)

source code

    db.define_table('x',Field('number','integer'))
    if db(db.x).isempty(): [db.x.insert(number=i) for i in range(10)]

    from gluon.dal import lazy_virtualfield

    class MyVirtualFields(object):
        # normal virtual field (backward compatible, discouraged)
        def normal_shift(self): return self.x.number+1
        # lazy virtual field (because of @staticmethod)
        @lazy_virtualfield
        def lazy_shift(instance,row,delta=4): return row.x.number+delta
    db.x.virtualfields.append(MyVirtualFields())

    for row in db(db.x).select():
        print row.number, row.normal_shift, row.lazy_shift(delta=7)

### __str__(self)
(Informal representation operator)

source code

serializes the table into a csv file

Overrides: object.__str__

### as_list(self, compact=True, storage_to_dict=True, datetime_to_str=True)

source code

returns the data as a list or dictionary. :param storage_to_dict: when True returns a dict, otherwise a list(default True) :param datetime_to_str: convert datetime fields as strings (default True)

### as_dict(self, key=`'``id``'`, compact=True, storage_to_dict=True, datetime_to_str=True)

source code

returns the data as a dictionary of dictionaries (storage_to_dict=True) or records (False)

:param key: the name of the field to be used as dict key, normally the id :param compact: ? (default True) :param storage_to_dict: when True returns a dict, otherwise a list(default True) :param datetime_to_str: convert datetime fields as strings (default True)

### export_to_csv_file(self, ofile, null=`'``<NULL>``'`, *args, **kwargs)

source code

    export data to csv, the first line contains the column names

    :param ofile: where the csv must be exported to
    :param null: how null values must be represented (default '<NULL>')
    :param delimiter: delimiter to separate values (default ',')
    :param quotechar: character to use to quote string values (default '"')
    :param quoting: quote system, use csv.QUOTE_*** (default csv.QUOTE_MINIMAL)
    :param represent: use the fields .represent value (default False)
    :param colnames: list of column names to use (default self.colnames)
                     This will only work when exporting rows objects!!!!
                     DO NOT use this with db.export_to_csv()