3.8.1. sciexp2.common.filter.Filter
Methods
|
Check if the given source matches this filter. |
|
Validate that variables in the filter are present in the given set. |
- class Filter(expression=None)
Bases:
objectBoolean expression to check against a dict-like object.
The filter contains an arbitrary Python expression, where every variable will be taken from the dict we are matching the filter against.
- Parameters:
- expressionFilter or dict or str, optional
Expression to use in the filter.
- Raises:
- SyntaxError
The expression is not valid.
See also
Notes
If expression is a dict-like object, it will define an expression that exactly matches its items.
Every filter will have the following global names defined:
re_match(var, str)Check if
varmatches the regular expressionstr.Examples
Filters can be easily composed together:
>>> f1 = Filter("a < 3") >>> f2 = Filter("b == 4") >>> and_filters(f1, f2) Filter("(a < 3) and (b == 4)") >>> or_filters(f1, f2) Filter("(a < 3) or (b == 4)")
Filter objects can be later matched against dict-like objects:
>>> f = Filter("a < 3 and b == 4") >>> f.match(dict(a=2, b=4)) True >>> f.match(dict(a=3, b=4)) False
Using a dict as an expression is equivalent to building a perfect match for the dict’s items:
>>> Filter({"VAR1": 1, "VAR2": 2}) Filter("VAR1 == 1 and VAR2 == 2")
- validate(allowed)
Validate that variables in the filter are present in the given set.
- Parameters:
- allowedset of variable names
Set of variable names to allow on the filter.
- Raises:
- NameError
Filter contains a variable name not present in allowed.