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&ADay 2
Week 2 — Day 2: Static Type Hints (`typing`) & Runtime Data Validation with Pydantic v2
1 code file(s) • 3 Q&ADay 3
Week 2 — Day 3: Asynchronous Python (`asyncio`), The Event Loop, `gather()` & Rate-Limiting Math
1 code file(s) • 3 Q&ADay 4
Week 2 — Day 4: Unit Testing AI Pipelines & Mocking LLM APIs
1 code file(s) • 3 Q&ADay 5
Week 2 — Day 5: Structured JSON Logging, Config Management & CLI Engineering
1 code file(s) • 2 Q&ADay 6
Week 2 — Day 6: Code Quality, Linting (`ruff`), Type Checking (`mypy`) & Python AST Auditing
1 code file(s) • 2 Q&ADay 7
Week 2 — Day 7: Mini-Project (Async Schema-Validated Runbook Batch Processor) + Week 2 Mock Interview
1 code file(s) • 0 Q&AWeek 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]) Crashes server withMemoryError(OOM)!- Pro AI Engineer (Day 1): Streams the 10 GB file line-by-line using a Generator (
yield) Uses only 120 bytes of RAM (O(1)memory)!- Beginner: Parses LLM JSON with
data["reply"]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) Enforces strict schemas and coerces/rejects malformed LLM outputs automatically!- Beginner: Calls an LLM 20 times in a
forloop (20 × 1.5s = 30 secondswait time!).- Pro AI Engineer (Day 3): Runs 20 coroutines concurrently using
asyncio.gather()+asyncio.Semaphore(5)Finishes in 6 seconds without hitting429 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
| Day | Topic | Deep Theory & Math Covered | Production Code Deliverable |
|---|---|---|---|
| Day 1 | uv, Comprehensions, Generators (yield) & Decorators | pyproject.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 2 | Static Type Hints & Pydantic v2 Internals | Structural subtyping (Protocol), Literal, Annotated, Pydantic v2 validation lifecycle, field/model validators, JSON Schema export | schema_validator.py — Strict RunbookQuery & RunbookAnalysisResponse validators catching LLM hallucinations |
| Day 3 | Asynchronous Python (asyncio) & Rate Limiting | GIL (Global Interpreter Lock), CPU vs I/O bound, Event Loop internals, Little's Law & Batch Latency math: | async_batch_engine.py — Concurrent asyncio.gather + asyncio.Semaphore rate-limited task runner |
| Day 4 | Unit Testing AI Pipelines & Mocking (AsyncMock) | Deterministic vs Flaky tests, Dependency Injection, unittest.mock.AsyncMock, patch, testing retries & edge cases at $0 cost | llm_client_mocking.py — LLM client wrapper + full offline mock test suite simulating 200 OK, 429 RateLimit, and malformed JSON |
| Day 5 | Structured JSON Logging, Config & CLI (argparse) | Why print() fails in Kubernetes/CloudWatch; JSON log lines (trace_id, latency_ms), immutable settings, subcommands | structured_logger_cli.py — Production JSON telemetry logger + multi-command CLI parser |
| Day 6 | Code Quality, AST Auditing, ruff & mypy | How Python compiles source code into an Abstract Syntax Tree (ast.parse); automated detection of bare except:, print(), and missing types | ast_code_auditor.py — Automated Python AST security & quality scanner |
| Day 7 | Week 2 Mini-Project & Senior Interview Grill | Combining Generators + Decorators + Validation + asyncio.Semaphore + Structured Logging into one cohesive async engine | async_runbook_pipeline.py — Full Async Validated Runbook Batch Processor + 15 Senior Interview Questions |