Custom filters
These filters can be used to implement any arbitrary logic when matching elements by passing a simple function.
For example, e.filters.and_(mytable, e.filters.custom((it, ..) => it.cells.len() > 5))
will match any mytable
with more than 5 cells.
The filtering function must return a boolean (true
to match this element), and receives the following parameters for each potential element:
- Fields (positional).
eid: "..."
: the element's eid. Useful if more than one kind of element is being tested by this filter. Usee.eid(elem)
to retrieve theeid
of an element for comparison.ancestry: (...)
: the ancestors of the current element, including(fields: (...), eid: "...")
- Extra named arguments which must be ignored with
..
for forwards-compatibility with future elembic versions.
Don't forget the ..
at the parameter list to ignore any potentially unused parameters (there can be more in future elembic updates).
Similarly to NOT filters, custom filters may be applied to any elements in principle. Therefore, they must be used within filter operators which restrict which elements they may apply to, usually AND.
For example, the following filter only applies to elem1
and elem2
instances, and checks different fields depending on which one it is:
e.filters.and_(
e.filters.or_(elem1, elem2),
e.filters.custom((it, eid: "", ..) => (
eid == e.eid(elem1) and it.count > 5
or eid == e.eid(elem2) and it.cells.len() < 10
))
)