recipemd.filter module¶
Defines boolean predicates that can be evaluated against a recipe.
Filters allow easy searching within the structure of a recipemd.data.Recipe
. They can be created from strings
with the FilterParser
or in code with the FilterBuilder
which can be imported as f
from this
module.
- class recipemd.filter.FilterParser[source]¶
Bases:
object
Allows parsing filter strings into ASTs
- filter_expression_parser: ParserElement¶
- parse_filter_string(filter_string)[source]¶
Parses a filter string into an evaluateable AST.
>>> fp = FilterParser() >>> fp.parse_filter_string('vegan and summer') BooleanAndOperation(operands=[AnyFilterTerm(filter_string=FuzzyFilterString(string='vegan')), AnyFilterTerm(filter_string=FuzzyFilterString(string='summer'))])
- Parameters:
filter_string (
str
) – A string representing a recipe filter- Return type:
_FilterElement
- Returns:
filter_element: A filter ast, subclass of FilterElement
- Raises:
ParseBaseException – If string is not a valid filter
re.error – If a regular expression used in a filter term is not syntactically correct
- recipemd.filter.f(string) = FilterBuilder(term=None)¶
A preconfigured
FilterBuilder
for convenient use- Parameters:
term (
Optional
[Type
[FilterTerm
]]) –
- class recipemd.filter.FilterBuilder(term=None)[source]¶
Bases:
_FilterTermBuilder
Creates filter expressions in code
Allows using a language similar to the expressions parsed by recipemd.filter.FilterParser directly in python code. This makes filters usable as an embedded domain specific language.
A preconfigured Filter builder is available as f in this module:
>>> f('Cheese') AnyFilterTerm(filter_string=FuzzyFilterString(string='Cheese'))
>>> f.ingr('Cheese') IngredientFilterTerm(filter_string=FuzzyFilterString(string='Cheese'))
>>> f.re('Cheese|Bacon') AnyFilterTerm(filter_string=RegexFilterString(regex=re.compile('Cheese|Bacon')))
>>> f.tag.ex('vegan') TagFilterTerm(filter_string=ExactFilterString(string='vegan'))
Filters can be combined with logical operators “&” (and), “|” (or), “^” (xor), and “~” (not):
>>> f('Cheese') | f('Bacon') BooleanOrOperation(operands=[AnyFilterTerm(filter_string=FuzzyFilterString(string='Cheese')), AnyFilterTerm(filter_string=FuzzyFilterString(string='Bacon'))])
- Parameters:
term (
Optional
[Type
[FilterTerm
]]) –
- property any: _FilterTermBuilder¶
Create a filter that matches any supported fields
- Return type:
_FilterTermBuilder
- property tag: _FilterTermBuilder¶
Create a filter that matches tags
- Return type:
_FilterTermBuilder
- property ingr: _FilterTermBuilder¶
Create a filter that matches ingredient names
- Return type:
_FilterTermBuilder
- property unit: _FilterTermBuilder¶
Create a filter that matches units in ingredients or in yields
- Return type:
_FilterTermBuilder
- class recipemd.filter.BooleanAndOperation(operands)[source]¶
Bases:
_BooleanBinaryOperation
- Parameters:
operands (
List
[_FilterElement
]) –
- class recipemd.filter.BooleanOrOperation(operands)[source]¶
Bases:
_BooleanBinaryOperation
- Parameters:
operands (
List
[_FilterElement
]) –
- class recipemd.filter.BooleanNotOperation(operands)[source]¶
Bases:
_BooleanUnaryOperation
- Parameters:
operands (
List
[_FilterElement
]) –
- class recipemd.filter.BooleanXorOperation(operands)[source]¶
Bases:
_BooleanBinaryOperation
- Parameters:
operands (
List
[_FilterElement
]) –
- OPERATOR = 'xor'¶
- class recipemd.filter.AnyFilterTerm(filter_string)[source]¶
Bases:
IngredientFilterTerm
,TagFilterTerm
,UnitFilterTerm
- Parameters:
filter_string (
_FilterString
) –
- class recipemd.filter.TagFilterTerm(filter_string)[source]¶
Bases:
FilterTerm
- Parameters:
filter_string (
_FilterString
) –
- class recipemd.filter.IngredientFilterTerm(filter_string)[source]¶
Bases:
FilterTerm
- Parameters:
filter_string (
_FilterString
) –
- class recipemd.filter.UnitFilterTerm(filter_string)[source]¶
Bases:
FilterTerm
- Parameters:
filter_string (
_FilterString
) –
- class recipemd.filter.FuzzyFilterString(string)[source]¶
Bases:
_FilterString
- Parameters:
string (
str
) –
- string: str¶