Filter Expressions
Learn about implementing filter expressions in your custom book project.
Overview
Filter expressions enable you to define filter criteria for your procedures using the whose keyword. These expressions allow you to filter data based on conditions such as equality, comparisons, and more. For example:
get users from office365 whose email is "[email protected]"Implementation
1. Include the filter_expression parameter
filter_expression parameterTo implement a filter expression, you need to provide the special filter_expression parameter in your procedure method definition. For example:
@procedure("to read some (*SMS* messages)")
def read_sms_messages(
self,
offset: Optional[int],
limit: Optional[int],
filter_expression: Optional[FilterExpression],
) -> List[SMSMessage]:
"""
Read some SMS messages using the Twilio API.
"""2. Implement Visitors
The FilterExpressionVisitor class is an abstract base class that defines methods for visiting different types of filter expressions. Each type of filter expression (binary, unary, value, noun phrase) is defined as a subclass of FilterExpression. You will need to define a class that implements these methods to handle filter expressions. For example:
Below is an example implementation of the FilterExpressionVisitor class in the Twilio book:
3. Processing Filter Expressions
Once you’ve defined your visitor class, you need to pass an instance of it to the filter expression. This is done by calling accept onfilter_expression. For example:
Example
In this example, a filter expression is used in the read some SMS messages procedure:
Last updated
Was this helpful?
