Week 2 Master Guide

Week 2: Modern Pro Python — `uv`, Generators, Decorators, Pydantic v2, `asyncio` & Testing

Week 2 Daily Breakdown (7 Days)

Day 1

Week 2 — Day 1: `uv` Packaging, Comprehensions, Lazy Generators (`yield`) & Decorators

1 code file(s) • 3 Q&A
Day 2

Week 2 — Day 2: Static Type Hints (`typing`) & Runtime Data Validation with Pydantic v2

1 code file(s) • 3 Q&A
Day 3

Week 2 — Day 3: Asynchronous Python (`asyncio`), The Event Loop, `gather()` & Rate-Limiting Math

1 code file(s) • 3 Q&A
Day 4

Week 2 — Day 4: Unit Testing AI Pipelines & Mocking LLM APIs

1 code file(s) • 3 Q&A
Day 5

Week 2 — Day 5: Structured JSON Logging, Config Management & CLI Engineering

1 code file(s) • 2 Q&A
Day 6

Week 2 — Day 6: Code Quality, Linting (`ruff`), Type Checking (`mypy`) & Python AST Auditing

1 code file(s) • 2 Q&A
Day 7

Week 2 — Day 7: Mini-Project (Async Schema-Validated Runbook Batch Processor) + Week 2 Mock Interview

1 code file(s) • 0 Q&A

Week 2: Modern Pro Python — uv, Generators, Decorators, Pydantic v2, asyncio & Testing (week2.md)

Mentor's Note (10+ YOE Staff AI Engineer POV): In Week 1, you learned how to write working Python code. In Week 2, you learn how to write Principal/Staff-level Production Python for AI Systems.

Look at the difference between a beginner script and a production AI system:

  • Beginner: Loads a 10 GB JSONL training file into a list ([json.loads(line) for line in f]) →\rightarrow Crashes server with MemoryError (OOM)!
  • Pro AI Engineer (Day 1): Streams the 10 GB file line-by-line using a Generator (yield) →\rightarrow Uses only 120 bytes of RAM (O(1) memory)!
  • Beginner: Parses LLM JSON with data["reply"] →\rightarrow Crashes when LLM returns "covered_by_doc": "yes" instead of a boolean!
  • Pro AI Engineer (Day 2): Validates boundary payloads with Pydantic v2 (BaseModel, @field_validator) →\rightarrow Enforces strict schemas and coerces/rejects malformed LLM outputs automatically!
  • Beginner: Calls an LLM 20 times in a for loop (20 × 1.5s = 30 seconds wait time!).
  • Pro AI Engineer (Day 3): Runs 20 coroutines concurrently using asyncio.gather() + asyncio.Semaphore(5) →\rightarrow Finishes in 6 seconds without hitting 429 RateLimitError!

Week 2 Architecture & Daily Folder Map

text
week02_advanced_python_pydantic_asyncio_tests/ ├── week2.md # Master Week 2 Syllabus & Architecture ├── day01_uv_comprehensions_generators_decorators/ # Day 1: uv, Lazy Generators (yield) & Decorators │ ├── README.md │ ├── src/stream_and_decorate.py │ ├── tests/test_stream_and_decorate.py │ └── interview_questions.md ├── day02_type_hints_and_pydantic_v2/ # Day 2: Typing, Protocol & Pydantic v2 Validation │ ├── README.md │ ├── src/schema_validator.py │ ├── tests/test_schema_validator.py │ └── interview_questions.md ├── day03_asyncio_concurrency_event_loop/ # Day 3: Asyncio Event Loop, gather() & Semaphores │ ├── README.md │ ├── src/async_batch_engine.py │ ├── tests/test_async_batch_engine.py │ └── interview_questions.md ├── day04_pytest_and_mocking_llm_apis/ # Day 4: Mocking LLM APIs (AsyncMock, patch) │ ├── README.md │ ├── src/llm_client_mocking.py │ ├── tests/test_llm_client_mocking.py │ └── interview_questions.md ├── day05_structured_logging_config_and_cli/ # Day 5: JSON Structured Logging & CLI Engineering │ ├── README.md │ ├── src/structured_logger_cli.py │ ├── tests/test_structured_logger_cli.py │ └── interview_questions.md ├── day06_code_quality_linting_type_checking/ # Day 6: AST Code Auditing, Linting & Type Safety │ ├── README.md │ ├── src/ast_code_auditor.py │ ├── tests/test_ast_code_auditor.py │ └── interview_questions.md └── day07_week2_mini_project_async_pipeline/ # Day 7: Async Validated Runbook Pipeline + Interview ├── README.md ├── src/async_runbook_pipeline.py ├── tests/test_async_runbook_pipeline.py └── interview_questions.md

Daily Schedule & Core Deliverables

DayTopicDeep Theory & Math CoveredProduction Code Deliverable
Day 1uv, Comprehensions, Generators (yield) & Decoratorspyproject.toml dependency resolution; O(n) List RAM vs O(1) Generator RAM (sys.getsizeof); Closures & Higher-Order Decorators (@wraps)stream_and_decorate.py — Lazy JSONL dataset streamer (yield) + @measure_latency & @retry_on_error decorators
Day 2Static Type Hints & Pydantic v2 InternalsStructural subtyping (Protocol), Literal, Annotated, Pydantic v2 validation lifecycle, field/model validators, JSON Schema exportschema_validator.py — Strict RunbookQuery & RunbookAnalysisResponse validators catching LLM hallucinations
Day 3Asynchronous Python (asyncio) & Rate LimitingGIL (Global Interpreter Lock), CPU vs I/O bound, Event Loop internals, Little's Law & Batch Latency math: T=⌈N/C⌉⋅LT = \lceil N / C \rceil \cdot Lasync_batch_engine.py — Concurrent asyncio.gather + asyncio.Semaphore rate-limited task runner
Day 4Unit Testing AI Pipelines & Mocking (AsyncMock)Deterministic vs Flaky tests, Dependency Injection, unittest.mock.AsyncMock, patch, testing retries & edge cases at $0 costllm_client_mocking.py — LLM client wrapper + full offline mock test suite simulating 200 OK, 429 RateLimit, and malformed JSON
Day 5Structured JSON Logging, Config & CLI (argparse)Why print() fails in Kubernetes/CloudWatch; JSON log lines (trace_id, latency_ms), immutable settings, subcommandsstructured_logger_cli.py — Production JSON telemetry logger + multi-command CLI parser
Day 6Code Quality, AST Auditing, ruff & mypyHow Python compiles source code into an Abstract Syntax Tree (ast.parse); automated detection of bare except:, print(), and missing typesast_code_auditor.py — Automated Python AST security & quality scanner
Day 7Week 2 Mini-Project & Senior Interview GrillCombining Generators + Decorators + Validation + asyncio.Semaphore + Structured Logging into one cohesive async engineasync_runbook_pipeline.py — Full Async Validated Runbook Batch Processor + 15 Senior Interview Questions
Week 2: Week 2: Modern Pro Python — `uv`, Generators, Decorators, Pydantic v2, `asyncio` & Testing | 24-Week AI Engineer Academy · DataArena