10. Python Comprehensions, Pathlib & Datetime for QA

Modern Python for QA Engineers

from pathlib import Path
from datetime import datetime, timedelta

# Pathlib for cross-platform file paths (no Windows backslash bugs):
report_path = Path("reports") / "test_summary.html"

# Generating unique test email:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
user_email = f"test_{timestamp}@targetqa.com"

# List comprehension to clean extracted table values:
raw_cells = [" Row 1 ", " Row 2 ", " "]
clean_cells = [c.strip() for c in raw_cells if c.strip()]

Task

  • Implement generate_unique_email(prefix: str = "test") -> str:
  • 1. Use datetime.now().strftime("%Y%m%d_%H%M%S").
  • 2. Return f"{prefix}_{timestamp}@targetqa.com".
πŸ’‘Common doubts
Did you like the problem?
Lesson 10 of 43
Python
Loading code editor…
Next