Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
.PHONY: check lint format fix test typecheck clean

lint:
uv run ruff check .

format:
uv run ruff format .

fix:
uv run ruff check . --fix
uv run ruff format .

typecheck:
uv run mypy .

test:
uv run pytest -v

check:
uv run ruff check .
uv run ruff format --check .
uv run mypy .
uv run pytest -v

clean:
find . -type d -name "__pycache__" -exec rm -rf {} +
find . -type d -name ".pytest_cache" -exec rm -rf {} +
find . -type d -name ".ruff_cache" -exec rm -rf {} +
find . -type d -name ".mypy_cache" -exec rm -rf {} +
find . -name "*.pyc" -delete
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ readme = "README.md"
requires-python = ">=3.12"

dependencies = [
"email-validator>=2.3.0",
"pydantic>=2.13.4",
"pytest>=9.1.1",
]
Expand Down
119 changes: 119 additions & 0 deletions python_fundamentals/exercises/day13_email_validation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env python3.12
# file_name: day13_email_validation.py
# branch: day13_pydantic_advance

from pydantic import (
BaseModel,
EmailStr,
Field,
ValidationError,
field_validator,
)


class Employee(BaseModel):
"""Represents an employee within the organization."""

name: str = Field(
min_length=3,
max_length=20,
)
email: EmailStr
salary: float = Field(gt=0)

@field_validator("name")
@classmethod
def normalize_and_validate_name(cls, value: str) -> str:
"""
Normalize and validate an employee name.

Rules:
- Remove leading/trailing whitespace.
- Convert to Title Case.
- Name must contain at least 3 characters.
- Name must contain alphabetic characters only.
"""

value = value.strip().title()

if len(value) < 3:
raise ValueError("Name must contain at least 3 characters.")

if not value.replace(" ", "").isalpha():
raise ValueError("Name must contain alphabetic characters only.")

return value


def print_separator() -> None:
print("-" * 60)


if __name__ == "__main__":
# ---------------------------------------------------------
# Valid Employee
# ---------------------------------------------------------

try:
valid_employee = Employee(
name=" tejas dixit ",
email="tejasdixit17@zohomail.in",
salary=25_000.99,
)

print("Valid Employee")
print(valid_employee.model_dump())

except ValidationError as error:
print(error)

print_separator()

# ---------------------------------------------------------
# Invalid Email
# ---------------------------------------------------------

try:
invalid_employee_1: Employee = Employee(
name="Vignesh Gawali",
email="invalid-email",
salary=30_000,
)

except ValidationError as error:
print("Invalid Email")
print(error)

print_separator()

# ---------------------------------------------------------
# Invalid Name (Contains Digits)
# ---------------------------------------------------------

try:
invalid_employee_2: Employee = Employee(
name="Tejas123",
email="tejas@example.com",
salary=30_000,
)

except ValidationError as error:
print("Invalid Name")
print(error)

print_separator()

# ---------------------------------------------------------
# Invalid Salary
# ---------------------------------------------------------

try:
invalid_employee_3: Employee = Employee(
name="Rahul Sharma",
email="rahul@example.com",
salary=-5_000,
)

except ValidationError as error:
print("Invalid Salary")
print(error)
2 changes: 1 addition & 1 deletion python_fundamentals/exercises/inventory.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def validate_product(product: Product) -> None:
if not product.name.strip():
raise ValueError("Product name is required.")

if not isinstance(product.price, (int, float)):
if not isinstance(product.price, (float)):
raise TypeError("Price must be numeric.")

if product.price <= 0:
Expand Down
Loading
Loading