Add helper functions for Flower
This commit is contained in:
parent
47c67e1a4f
commit
ca83c9e70e
77
helpers/flower_helpers.py
Normal file
77
helpers/flower_helpers.py
Normal file
@ -0,0 +1,77 @@
|
|||||||
|
from typing import List, Dict, Optional, Union, Any
|
||||||
|
import re
|
||||||
|
|
||||||
|
def validate_flower_name(name: str) -> bool:
|
||||||
|
"""
|
||||||
|
Validate a flower name.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name: The flower name to validate.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
bool: True if the flower name is valid, False otherwise.
|
||||||
|
"""
|
||||||
|
pattern = r'^[a-zA-Z\s]+$'
|
||||||
|
return bool(re.match(pattern, name))
|
||||||
|
|
||||||
|
def get_flower_info(flower_name: str) -> Optional[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Get information about a flower from an external API or database.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
flower_name: The name of the flower.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Optional[Dict[str, Any]]: A dictionary containing information about the flower, or None if not found.
|
||||||
|
"""
|
||||||
|
# Implement logic to fetch flower information from an external source
|
||||||
|
pass
|
||||||
|
|
||||||
|
def format_flower_list(flowers: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Format a list of flower information dictionaries.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
flowers: A list of dictionaries containing flower information.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Dict[str, Any]]: A formatted list of dictionaries with flower information.
|
||||||
|
"""
|
||||||
|
formatted_flowers = []
|
||||||
|
for flower in flowers:
|
||||||
|
formatted_flower = {
|
||||||
|
'name': flower['name'],
|
||||||
|
'description': flower.get('description', ''),
|
||||||
|
'category': flower.get('category', ''),
|
||||||
|
'image_url': flower.get('image_url', '')
|
||||||
|
}
|
||||||
|
formatted_flowers.append(formatted_flower)
|
||||||
|
return formatted_flowers
|
||||||
|
|
||||||
|
def filter_flowers_by_category(flowers: List[Dict[str, Any]], category: str) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Filter a list of flower information dictionaries by category.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
flowers: A list of dictionaries containing flower information.
|
||||||
|
category: The category to filter by.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Dict[str, Any]]: A list of dictionaries containing flower information for the specified category.
|
||||||
|
"""
|
||||||
|
filtered_flowers = [flower for flower in flowers if flower.get('category', '').lower() == category.lower()]
|
||||||
|
return filtered_flowers
|
||||||
|
|
||||||
|
def search_flowers_by_name(flowers: List[Dict[str, Any]], search_term: str) -> List[Dict[str, Any]]:
|
||||||
|
"""
|
||||||
|
Search for flowers by name in a list of flower information dictionaries.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
flowers: A list of dictionaries containing flower information.
|
||||||
|
search_term: The search term to use for filtering flower names.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[Dict[str, Any]]: A list of dictionaries containing flower information for matching flower names.
|
||||||
|
"""
|
||||||
|
matching_flowers = [flower for flower in flowers if search_term.lower() in flower['name'].lower()]
|
||||||
|
return matching_flowers
|
Loading…
x
Reference in New Issue
Block a user