72 lines
1.9 KiB
Python
72 lines
1.9 KiB
Python
from typing import List, Optional
|
|
import re
|
|
|
|
def validate_bird_name(name: str) -> bool:
|
|
"""
|
|
Validate the bird name.
|
|
|
|
Args:
|
|
name (str): The name of the bird.
|
|
|
|
Returns:
|
|
bool: True if the name is valid, False otherwise.
|
|
"""
|
|
pattern = r'^[a-zA-Z\s]+$'
|
|
return bool(re.match(pattern, name))
|
|
|
|
def validate_bird_species(species: str) -> bool:
|
|
"""
|
|
Validate the bird species.
|
|
|
|
Args:
|
|
species (str): The species of the bird.
|
|
|
|
Returns:
|
|
bool: True if the species is valid, False otherwise.
|
|
"""
|
|
pattern = r'^[a-zA-Z\s]+$'
|
|
return bool(re.match(pattern, species))
|
|
|
|
def validate_bird_age(age: Optional[int]) -> bool:
|
|
"""
|
|
Validate the bird age.
|
|
|
|
Args:
|
|
age (Optional[int]): The age of the bird.
|
|
|
|
Returns:
|
|
bool: True if the age is valid (non-negative integer or None), False otherwise.
|
|
"""
|
|
if age is None:
|
|
return True
|
|
return isinstance(age, int) and age >= 0
|
|
|
|
def get_bird_description(name: str, species: str, age: Optional[int]) -> str:
|
|
"""
|
|
Generate a description for a bird based on its name, species, and age.
|
|
|
|
Args:
|
|
name (str): The name of the bird.
|
|
species (str): The species of the bird.
|
|
age (Optional[int]): The age of the bird.
|
|
|
|
Returns:
|
|
str: The generated description for the bird.
|
|
"""
|
|
description = f"{name} is a {species}"
|
|
if age is not None:
|
|
description += f" that is {age} years old"
|
|
return description
|
|
|
|
def filter_birds_by_species(birds: List[dict], species: str) -> List[dict]:
|
|
"""
|
|
Filter a list of birds by species.
|
|
|
|
Args:
|
|
birds (List[dict]): A list of bird dictionaries.
|
|
species (str): The species to filter by.
|
|
|
|
Returns:
|
|
List[dict]: A list of bird dictionaries matching the specified species.
|
|
"""
|
|
return [bird for bird in birds if bird['species'] == species] |