Day 21: Allergen Assessment¶
Classes:
|
Represent an ingredient or allergen identifier. |
|
Represent an identifier of an allergen. |
|
Represent an identifier of an ingredient. |
|
Specify a well-formed line representing an ingredient list with the allergen. |
|
Represent an entry in the list of foods. |
Data:
Express a line specifying an ingredient list along with the allergens. |
Functions:
|
Encapsulate the parsing of |
|
Serialize the |
|
Find the ingredients without allergens. |
|
Parse the input and return the set of ingredients without allergens. |
- class Identifier(value: str)[source]¶
Represent an ingredient or allergen identifier.
Methods:
__new__
(cls, value)Enforce the properties on the
identifier
.- static __new__(cls, value: str) Identifier [source]¶
Enforce the properties on the
identifier
.- Requires
re.fullmatch(r"^[a-zA-Z]+Z", value)
- INGREDIENT_LINE_RE = re.compile('\\s*(?P<ingredients>[a-zA-Z]+(\\s+[a-zA-Z]+)*)\\s+\\(contains\\s+(?P<allergens>[a-zA-Z]+(\\s*,\\s*[a-zA-Z]+)*)\\)')¶
Express a line specifying an ingredient list along with the allergens.
- class IngredientLine(line: str)[source]¶
Specify a well-formed line representing an ingredient list with the allergen.
Methods:
__new__
(cls, line)- requires
- static __new__(cls, line: str) IngredientLine [source]¶
- Requires
INGREDIENT_LINE_RE.fullmatch(line)
- class Entry(ingredients: List[Ingredient], allergens: List[Allergen])[source]¶
Represent an entry in the list of foods.
Methods:
__init__
(ingredients, allergens)Initialize with the given values.
Attributes:
Ingredients of the entry
Allergens of the entry
- __init__(ingredients: List[Ingredient], allergens: List[Allergen]) None [source]¶
Initialize with the given values.
- Requires
len(ingredients) > 0
len(allergens) > 0
- ingredients: List[Ingredient]¶
Ingredients of the entry
- parse_ingredient_line(line: IngredientLine) Entry [source]¶
Encapsulate the parsing of
line
into entries.
- serialize_entry(entry: Entry) IngredientLine [source]¶
Serialize the
entry
back into the string.- Ensures
result == serialize_entry(parse_ingredient_line(result))
- find_non_allergenic_ingredients(entries: List[Entry]) Set[Ingredient] [source]¶
Find the ingredients without allergens.
- solve(lines: List[IngredientLine]) Set[Ingredient] [source]¶
Parse the input and return the set of ingredients without allergens.