The ingredient manipulation module provides tools for creating, modifying, and analyzing virtual ingredients in TomatoPy.
Creating Ingredients
Basic Ingredient Creation
Copy from tomatopy import Ingredient
# Create a basic ingredient
tomato = Ingredient (
name = "tomato" ,
amount = 1 ,
unit = "whole" ,
properties = {
"ripeness" : 0.8 ,
"water_content" : 0.95 ,
"sugar_content" : 0.03
}
)
Specialized Ingredient Classes
Copy from tomatopy import Tomato, Garlic, Basil
# Create specialized ingredients
san_marzano = Tomato(
ripeness=0.8,
variety="San Marzano",
weight=150
)
fresh_garlic = Garlic(
cloves=3,
freshness=0.9,
size="medium"
)
sweet_basil = Basil(
leaves=10,
freshness=0.95,
variety="Genovese"
)
Modifying Ingredients
Property Updates
Copy # Update ingredient properties
tomato.properties["ripeness"] = 0.9
tomato.properties["water_content"] = 0.92
# Batch update properties
tomato.update_properties({
"ripeness": 0.9,
"water_content": 0.92,
"sugar_content": 0.04
})
Unit Conversion
Copy # Convert between units
tomato.convert_to_imperial() # Converts metric to imperial
tomato.convert_volume("ml", "cups") # Converts between volume units
# Get measurements in different units
print(tomato.get_weight("g")) # 150
print(tomato.get_weight("oz")) # 5.29
Ingredient Analysis
Physical Properties
Copy # Get physical properties
print(tomato.get_density()) # 1.02 g/ml
print(tomato.get_volume()) # 147 ml
print(tomato.get_surface_area()) # 150 cm²
Chemical Analysis
Copy # Analyze chemical properties
print(tomato.get_ph()) # 4.5
print(tomato.get_water_content()) # 0.95
print(tomato.get_sugar_content()) # 0.03
Ingredient Operations
Cutting and Chopping
Copy # Cut ingredients
diced_tomato = tomato.cut(
method="dice",
size="medium",
consistency="uniform"
)
# Chop ingredients
chopped_garlic = fresh_garlic.chop(
fineness="medium",
method="mince"
)
Combining Ingredients
Copy # Combine ingredients
mixture = tomato.combine(
fresh_garlic,
sweet_basil,
method="mix",
consistency="chunky"
)
Quality Control
Freshness Check
Copy # Check ingredient freshness
if tomato.is_fresh():
print("Ingredient is fresh")
else:
print("Ingredient may be past its prime")
# Get freshness score
freshness_score = tomato.get_freshness_score() # 0.85
Quality Assessment
Copy # Assess ingredient quality
quality_report = tomato.assess_quality()
print(quality_report.overall_score) # 0.92
print(quality_report.recommendations) # ["Consider using within 2 days"]
Error Handling
Copy try:
# Attempt invalid operation
tomato.cut(method="invalid_method")
except IngredientError as e:
print(f"Error: {e}") # "Invalid cutting method"
try:
# Attempt invalid property update
tomato.update_properties({"invalid_property": 1.0})
except PropertyError as e:
print(f"Error: {e}") # "Invalid property name"
Best Practices
Always Check Ingredient State
Copy if tomato.state == "fresh":
# Proceed with operation
Use Type Hints
Copy from typing import Dict, Any
def create_ingredient(
name: str,
properties: Dict[str, Any]
) -> Ingredient:
return Ingredient(name=name, properties=properties)
Handle Unit Conversions Carefully
Copy # Always specify units explicitly
weight_grams = tomato.get_weight("g")
weight_ounces = tomato.get_weight("oz")
API Reference
Classes
Ingredient
: Base class for all ingredients
Tomato
: Specialized tomato ingredient
Garlic
: Specialized garlic ingredient
Basil
: Specialized basil ingredient
Methods
Ingredient Class
__init__(name, amount, unit, properties)
update_properties(properties)
convert_volume(from_unit, to_unit)
cut(method, size, consistency)
combine(*ingredients, method, consistency)
Next Steps
API Reference - Explore the full API