[Bro] pybroker with 'optional' fields

Siwek, Jon jsiwek at illinois.edu
Tue May 19 08:45:53 PDT 2015


> On May 18, 2015, at 1:50 PM, Jeff Barber <jbarber at computer.org> wrote:
> 
> Yeah. Using valid() works but it makes the code clumsy and unpythonic. Below is my code. Uncomment the second "sub_recs = ..." line in ppkt() (in place of the first one) to see the issue.
> 
> def get_fields(fields, n_fields):
>     new_fields = []
>     for n in range(n_fields):
>         f = fields[n]
>         if f.valid():
>             new_fields.append(f.get())
>         else:
>             new_fields.append(None)
>     return new_fields
> 
> def ppkt(p):
>     rec = p.as_record()
>     sub_recs = get_fields(rec.fields(), rec.size())
>     #===>>> sub_recs = [f.get() for f in fields]
>     print sub_recs

Does something like the following work to transform it into the format you want?

	sub_recs = [f.get() if f.valid() else None for f in fields]

- Jon


More information about the Bro mailing list