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 = None
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: Optional[Type[recipemd.filter.FilterTerm]] = None)[source]

Bases: recipemd.filter._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

Create a filter that matches any supported fields

Return type

_FilterTermBuilder

property ingr

Create a filter that matches ingredient names

Return type

_FilterTermBuilder

property tag

Create a filter that matches tags

Return type

_FilterTermBuilder

property unit

Create a filter that matches units in ingredients or in yields

Return type

_FilterTermBuilder

class recipemd.filter.BooleanAndOperation(operands: List[recipemd.filter._FilterElement])[source]

Bases: recipemd.filter._BooleanBinaryOperation

Parameters

operands (List[_FilterElement]) –

evaluate(recipe)[source]

Evaluate filter against given recipe

Parameters

recipe (Recipe) –

Return type

bool

operands = None
class recipemd.filter.BooleanOrOperation(operands: List[recipemd.filter._FilterElement])[source]

Bases: recipemd.filter._BooleanBinaryOperation

Parameters

operands (List[_FilterElement]) –

evaluate(recipe)[source]

Evaluate filter against given recipe

Parameters

recipe (Recipe) –

Return type

bool

operands = None
class recipemd.filter.BooleanNotOperation(operands: List[recipemd.filter._FilterElement])[source]

Bases: recipemd.filter._BooleanUnaryOperation

Parameters

operands (List[_FilterElement]) –

evaluate(recipe)[source]

Evaluate filter against given recipe

Parameters

recipe (Recipe) –

Return type

bool

operands = None
class recipemd.filter.BooleanXorOperation(operands: List[recipemd.filter._FilterElement])[source]

Bases: recipemd.filter._BooleanBinaryOperation

Parameters

operands (List[_FilterElement]) –

OPERATOR = 'xor'
evaluate(recipe)[source]

Evaluate filter against given recipe

Parameters

recipe (Recipe) –

Return type

bool

operands = None
class recipemd.filter.AnyFilterTerm(filter_string: recipemd.filter._FilterString)[source]

Bases: recipemd.filter.IngredientFilterTerm, recipemd.filter.TagFilterTerm, recipemd.filter.UnitFilterTerm

Parameters

filter_string (_FilterString) –

evaluate(recipe)[source]

Evaluate filter against given recipe

Parameters

recipe (Recipe) –

Return type

bool

filter_string = None
class recipemd.filter.TagFilterTerm(filter_string: recipemd.filter._FilterString)[source]

Bases: recipemd.filter.FilterTerm

Parameters

filter_string (_FilterString) –

evaluate(recipe)[source]

Evaluate filter against given recipe

Parameters

recipe (Recipe) –

Return type

bool

filter_string = None
class recipemd.filter.IngredientFilterTerm(filter_string: recipemd.filter._FilterString)[source]

Bases: recipemd.filter.FilterTerm

Parameters

filter_string (_FilterString) –

evaluate(recipe)[source]

Evaluate filter against given recipe

Parameters

recipe (Recipe) –

Return type

bool

filter_string = None
class recipemd.filter.UnitFilterTerm(filter_string: recipemd.filter._FilterString)[source]

Bases: recipemd.filter.FilterTerm

Parameters

filter_string (_FilterString) –

evaluate(recipe)[source]

Evaluate filter against given recipe

Parameters

recipe (Recipe) –

Return type

bool

filter_string = None
class recipemd.filter.FuzzyFilterString(string: str)[source]

Bases: recipemd.filter._FilterString

Parameters

string (str) –

contained_in(to_search)[source]

Checks if any of the elements in to_search match the filter string.

Parameters

to_search (Iterable[str]) –

Return type

bool

string: str = None
class recipemd.filter.ExactFilterString(string: str)[source]

Bases: recipemd.filter._FilterString

Parameters

string (str) –

contained_in(to_search)[source]

Checks if any of the elements in to_search match the filter string.

Parameters

to_search (Iterable[str]) –

Return type

bool

string: str = None
class recipemd.filter.RegexFilterString(regex: Pattern)[source]

Bases: recipemd.filter._FilterString

Parameters

regex (Pattern) –

contained_in(to_search)[source]

Checks if any of the elements in to_search match the filter string.

Parameters

to_search (Iterable[str]) –

Return type

bool

regex: Pattern = None