recipemd.data module

Defines the RecipeMD data structures, provides parser, serializer and recipe scaling functions.

class recipemd.data.RecipeParser[source]

Bases: object

Parses strings to a Recipe or Amount.

The markdown format is described in the RecipeMD Specification.

parse(src)[source]

Parses a markdown string into a Recipe.

>>> recipe_parser = RecipeParser()

>>> recipe = recipe_parser.parse('''
...   # Guacamole
...   ---
...   - *1* avocado
...   - *.5 teaspoon* salt
...   - *1 1/2 pinches* red pepper flakes
...   - lemon juice
...   ---
...   Remove flesh from avocado and roughly mash with fork. Season to taste with salt pepper and lemon juice.
... ''')

>>> recipe.title
'Guacamole'

>>> recipe.ingredients[0].name
'avocado'
Raises:

RuntimeException – If src is not a valid RecipeMD recipe.

Parameters:

src (str) –

Return type:

Recipe

static parse_amount(amount_str)[source]

Parses an amount string to an Amount.

>>> RecipeParser.parse_amount('3.5 l')
Amount(factor=Decimal('3.5'), unit='l')

Will recognize different number formats:

>>> RecipeParser.parse_amount('3 1/2 l')
Amount(factor=Decimal('3.5'), unit='l')

>>> RecipeParser.parse_amount('3 ½ l')
Amount(factor=Decimal('3.5'), unit='l')

>>> RecipeParser.parse_amount('3,5 l')
Amount(factor=Decimal('3.5'), unit='l')
Parameters:

amount_str (str) –

Return type:

Optional[Amount]

class recipemd.data.RecipeSerializer[source]

Bases: object

serialize(recipe, *, rounding=None)[source]
Parameters:
  • recipe (Recipe) –

  • rounding (Optional[int]) –

Return type:

str

recipemd.data.multiply_recipe(recipe, multiplier)[source]

Multiplies a recipe by the given multiplier.

Creates a new recipe where the factor of yield and ingredient is changed according to the multiplier.

>>> recipe = Recipe(
...   ingredients=[
...     Ingredient(name='Eggs', amount=Amount(factor=Decimal('5'), unit=None), link=None),
...     Ingredient(name='Butter', amount=Amount(factor=Decimal('200'), unit='g'), link=None),
...    ]    
... )
>>> multiplied_recipe = multiply_recipe(recipe, 3)

>>> multiplied_recipe.ingredients[0]
Ingredient(name='Eggs', amount=Amount(factor=Decimal('15'), unit=None), link=None)

>>> multiplied_recipe.ingredients[1]
Ingredient(name='Butter', amount=Amount(factor=Decimal('600'), unit='g'), link=None)
Parameters:
  • recipe (Recipe) –

  • multiplier (Decimal) –

Return type:

Recipe

recipemd.data.get_recipe_with_yield(recipe, required_yield)[source]

Scale the given recipe to a required yield.

Creates a new recipe, which has the yield given by required_yield. A recipe can only be scaled if a yield with a matching unit is present.

Raises:
  • StopIteration – If no yield with a matching unit can be found.

  • RuntimeError – If required_yield or the matching yield in the recipe do not have a factor.

Parameters:
Return type:

Recipe

class recipemd.data.Recipe(ingredients=<factory>, ingredient_groups=<factory>, title=None, description=None, yields=<factory>, tags=<factory>, instructions=None)[source]

Bases: IngredientList

Parameters:
  • ingredients (List[Ingredient]) –

  • ingredient_groups (List[IngredientGroup]) –

  • title (Optional[str]) –

  • description (Optional[str]) –

  • yields (List[Amount]) –

  • tags (List[str]) –

  • instructions (Optional[str]) –

title: Optional[str] = None
description: Optional[str] = None
yields: List[Amount]
tags: List[str]
instructions: Optional[str] = None
classmethod from_json(s, *, parse_float=None, parse_int=None, parse_constant=None, infer_missing=False, **kw)
Parameters:
  • cls (Type[TypeVar(A, bound= DataClassJsonMixin)]) –

  • s (Union[str, bytes, bytearray]) –

Return type:

TypeVar(A, bound= DataClassJsonMixin)

to_json(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, indent=None, separators=None, default=None, sort_keys=False, **kw)
Parameters:
  • skipkeys (bool) –

  • ensure_ascii (bool) –

  • check_circular (bool) –

  • allow_nan (bool) –

  • indent (Union[int, str, None]) –

  • separators (Tuple[str, str]) –

  • default (Callable) –

  • sort_keys (bool) –

Return type:

str

class recipemd.data.Ingredient(name, amount=None, link=None)[source]

Bases: object

Parameters:
  • name (str) –

  • amount (Optional[Amount]) –

  • link (Optional[str]) –

name: str
amount: Optional[Amount] = None
classmethod from_json(s, *, parse_float=None, parse_int=None, parse_constant=None, infer_missing=False, **kw)
Parameters:
  • cls (Type[TypeVar(A, bound= DataClassJsonMixin)]) –

  • s (Union[str, bytes, bytearray]) –

Return type:

TypeVar(A, bound= DataClassJsonMixin)

to_json(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, indent=None, separators=None, default=None, sort_keys=False, **kw)
Parameters:
  • skipkeys (bool) –

  • ensure_ascii (bool) –

  • check_circular (bool) –

  • allow_nan (bool) –

  • indent (Union[int, str, None]) –

  • separators (Tuple[str, str]) –

  • default (Callable) –

  • sort_keys (bool) –

Return type:

str

class recipemd.data.IngredientGroup(ingredients=<factory>, ingredient_groups=<factory>, title=None)[source]

Bases: IngredientList

Parameters:
title: Optional[str] = None
classmethod from_json(s, *, parse_float=None, parse_int=None, parse_constant=None, infer_missing=False, **kw)
Parameters:
  • cls (Type[TypeVar(A, bound= DataClassJsonMixin)]) –

  • s (Union[str, bytes, bytearray]) –

Return type:

TypeVar(A, bound= DataClassJsonMixin)

to_json(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, indent=None, separators=None, default=None, sort_keys=False, **kw)
Parameters:
  • skipkeys (bool) –

  • ensure_ascii (bool) –

  • check_circular (bool) –

  • allow_nan (bool) –

  • indent (Union[int, str, None]) –

  • separators (Tuple[str, str]) –

  • default (Callable) –

  • sort_keys (bool) –

Return type:

str

class recipemd.data.Amount(factor=None, unit=None)[source]

Bases: object

Parameters:
  • factor (Optional[Decimal]) –

  • unit (Optional[str]) –

factor: Optional[Decimal] = None
unit: Optional[str] = None
classmethod from_json(s, *, parse_float=None, parse_int=None, parse_constant=None, infer_missing=False, **kw)
Parameters:
  • cls (Type[TypeVar(A, bound= DataClassJsonMixin)]) –

  • s (Union[str, bytes, bytearray]) –

Return type:

TypeVar(A, bound= DataClassJsonMixin)

to_json(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, indent=None, separators=None, default=None, sort_keys=False, **kw)
Parameters:
  • skipkeys (bool) –

  • ensure_ascii (bool) –

  • check_circular (bool) –

  • allow_nan (bool) –

  • indent (Union[int, str, None]) –

  • separators (Tuple[str, str]) –

  • default (Callable) –

  • sort_keys (bool) –

Return type:

str

class recipemd.data.IngredientList(ingredients=<factory>, ingredient_groups=<factory>)[source]

Bases: object

Parameters:
ingredients: List[Ingredient]
ingredient_groups: List[IngredientGroup]
property leaf_ingredients: Generator[Ingredient, None, None]
Return type:

Generator[Ingredient, None, None]

property all_ingredients: Generator[Union[Ingredient, IngredientGroup], None, None]
Return type:

Generator[Union[Ingredient, IngredientGroup], None, None]

classmethod from_json(s, *, parse_float=None, parse_int=None, parse_constant=None, infer_missing=False, **kw)
Parameters:
  • cls (Type[TypeVar(A, bound= DataClassJsonMixin)]) –

  • s (Union[str, bytes, bytearray]) –

Return type:

TypeVar(A, bound= DataClassJsonMixin)

to_json(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, indent=None, separators=None, default=None, sort_keys=False, **kw)
Parameters:
  • skipkeys (bool) –

  • ensure_ascii (bool) –

  • check_circular (bool) –

  • allow_nan (bool) –

  • indent (Union[int, str, None]) –

  • separators (Tuple[str, str]) –

  • default (Callable) –

  • sort_keys (bool) –

Return type:

str