# Record list filters

Note

New in verision 0.5.11

Filtering example:

    report = ndb.interfaces.dump()
    report.select_fields('index', 'ifname', 'address', 'state')
    report.transform_fields(
        address=lambda r: '%s%s.%s%s.%s%s' % tuple(r.address.split(':'))
    )
    for record in report.format('csv'):
        print(record)

    'index','ifname','address','state'
    1,'lo','0000.0000.0000','up'
    2,'eth0','5254.0072.58b2','up'

class pyroute2.ndb.report.RecordSetConfig(prime)

class pyroute2.ndb.report.RecordSet(generator, config=None, ellipsis=True)

  NDB views return objects of this class with summary() and dump() methods. RecordSet objects are generator-based, they do not store the data in the memory, but transform them on the fly.

  RecordSet filters also return objects of this class, thus making possible to make chains of filters.

  select_fields(*fields)

    Select only chosen fields for every record:

        report = ndb.interfaces.dump()
        report.select_fields('index', 'ifname')
        for line in report.format('csv'):
            print(line)

        'index','ifname'
        1,'lo'
        2,'eth0'

  select_records(f=None, **spec)

    Select records based on a function f() or a spec match. A spec is dictionary of pairs field: constant or field: callable:

        report = ndb.addresses.summary()
        report.select_records(ifname=lambda x: x.startswith('eth'))
        for line in report.format('csv'):
            print(line)

        'target','tflags','ifname','address','prefixlen'
        'localhost',0,'eth0','192.168.122.28',24

  transform_fields(**kwarg)

    Transform fields with a function. Function must accept the record as the only argument:

        report = ndb.addresses.summary()
        report.transform_fields(
            address=lambda r: f'{r.address}/{r.prefixlen}'
        )
        report.select_fields('ifname', 'address')
        for line in report.format('csv'):
            print(line)

        'ifname','address'
        'lo','127.0.0.1/8'
        'eth0','192.168.122.28/24'

  format(kind)

    Return an iterator over text lines in the chosen format.

    Supported formats: ‘json’, ‘csv’.

  count()

    Return number of records.

    The method exhausts the generator.