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

Represents a recipe.

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
class recipemd.data.Ingredient(name, amount=None, link=None)[source]

Bases: object

Represents an ingredient with name and optional amount and link.

Parameters:
  • name (str)

  • amount (Optional[Amount])

  • link (Optional[str])

name: str
amount: Optional[Amount] = None
class recipemd.data.IngredientGroup(ingredients=<factory>, ingredient_groups=<factory>, title='')[source]

Bases: IngredientList

An ingredient group is a list of ingredients and ingredient groups with a title.

Parameters:
title: str = ''
class recipemd.data.Amount(factor, unit=None)[source]

Bases: object

Represents an amount, which is a factor with an associated unit.

Parameters:
  • factor (Decimal)

  • unit (Optional[str])

factor: Decimal
unit: Optional[str] = None
class recipemd.data.IngredientList(ingredients=<factory>, ingredient_groups=<factory>)[source]

Bases: object

Represents a list of ingredients.

This is used as a base class for Recipe and IngredientGroup, allowing common algorithm implementations for both.

Parameters:
ingredients: List[Ingredient]
ingredient_groups: List[IngredientGroup]
property leaf_ingredients: Generator[Ingredient, None, None]
property all_ingredients: Generator[Ingredient | IngredientGroup, None, None]