Add helper functions for Bird
This commit is contained in:
parent
9f764e7855
commit
5d8cf48b14
72
helpers/bird_helpers.py
Normal file
72
helpers/bird_helpers.py
Normal file
@ -0,0 +1,72 @@
|
||||
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]
|
Loading…
x
Reference in New Issue
Block a user