-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearn.py
More file actions
391 lines (354 loc) · 16.5 KB
/
learn.py
File metadata and controls
391 lines (354 loc) · 16.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import sys
import os
import subprocess
import json
import time
from rich.console import Console
from rich.markdown import Markdown
from rich.panel import Panel
from rich.syntax import Syntax
from rich.table import Table
import questionary
# ==========================================
# 🎓 COURSE CONFIGURATION
# ==========================================
# ==========================================
# 🎓 COURSE CONFIGURATION
# ==========================================
COURSE = [
{
"id": "01",
"title": "🐍 Python Essentials",
"path": "01_python_essentials",
"lessons": [
{
"title": "⚡ Concurrency Deep Dive (Sub-Module)",
"type": "submodule",
"path": "concurrency", # Relative to parent path
"lessons": [
{"file": "00_intro_threading.py", "title": "0A. Threading Basics (Lifecycle)"},
{"file": "00_intro_multiprocessing.py", "title": "0B. Multiprocessing Basics (Isolation)"},
{"file": "00_intro_async.py", "title": "0C. Async Basics (Coroutines)"},
{"file": "01_threads_vs_processes.py", "title": "1. Threads vs Processes (The GIL)"},
{"file": "02_async_io.py", "title": "2. AsyncIO (Event Loop)"},
{"file": "03_race_conditions.py", "title": "3. Race Conditions (Async Locks)"},
{"file": "04_producer_consumer.py", "title": "4. Producer-Consumer (Queues)"},
{"file": "05_build_event_loop.py", "title": "5. Build an Event Loop (Internals)"},
{"file": "06_cpu_offloading.py", "title": "6. CPU Offloading (ProcessPool)"},
{"file": "07_structured_concurrency.py", "title": "7. Structured Concurrency (TaskGroup)"},
]
},
{
"title": "🚦 Thread Safety (OS Threads)",
"type": "submodule",
"path": "thread_safety",
"lessons": [
{"file": "01_race_condition_threading.py", "title": "Race Condition (Bytecode Proof)"},
{"file": "02_primitives.py", "title": "Primitives (Lock, RLock, Semaphore)"},
{"file": "03_deadlocks.py", "title": "Deadocks (Dining Philosophers)"},
]
},
{"file": "02_typing.py", "title": "Typing (Pydantic vs Dicts)"},
{"file": "03_generators.py", "title": "Generators (Stream vs List)"},
{"file": "04_context_managers.py", "title": "Context Managers (Safety)"},
{"file": "05_decorators.py", "title": "Decorators (Clean Code)"},
{"file": "challenge_solution.py", "title": "🏆 Capstone Challenge Solution"}
]
},
{
"id": "02",
"title": "🧩 Low Level Design (LLD)",
"path": "02_lld_principles",
"lessons": [
{"file": "00_oop_primer.py", "title": "0. OOP Primer (Interfaces & Polymorphism)"},
{"file": "01_solid_agents.py", "title": "1. SOLID Principles"},
{"file": "02_composition_over_inheritance.py", "title": "2. Composition over Inheritance"},
{
"title": "📐 Design Patterns (Categorized)",
"type": "submodule",
"path": "design_patterns",
"lessons": [
# CREATIONAL
{"file": "creational_factory.py", "title": "Creational: Factory (Object Creation)"},
{"file": "creational_builder.py", "title": "Creational: Builder (Complex Configs)"},
{"file": "creational_singleton.py", "title": "Creational: Singleton (Global State)"},
# STRUCTURAL
{"file": "structural_adapter_facade.py", "title": "Structural: Adapter & Facade"},
{"file": "structural_composite.py", "title": "Structural: Composite (Trees)"},
# BEHAVIORAL
{"file": "behavioral_strategy.py", "title": "Behavioral: Strategy (Algorithms)"},
{"file": "behavioral_observer.py", "title": "Behavioral: Observer (Events)"},
{"file": "behavioral_state.py", "title": "Behavioral: State (Workflow)"},
{"file": "behavioral_chain.py", "title": "Behavioral: Chain of Responsibility (Middleware)"},
]
},
{"file": "challenge_solution.py", "title": "🏆 Capstone Challenge Solution"}
]
},
{
"id": "03",
"title": "☁️ High Level Design (HLD)",
"path": "03_hld_concepts",
"lessons": [
{"file": "00_scaling_101.py", "title": "0. Scaling 101 (Vertical vs Horizontal)"},
{
"title": "🛡️ Resiliency Patterns",
"type": "submodule",
"path": "resiliency",
"lessons": [
{"file": "01_circuit_breaker.py", "title": "Circuit Breaker"},
{"file": "02_retry_backoff.py", "title": "Retry with Backoff & Jitter"},
{"file": "03_chaos_engineering.py", "title": "Chaos Engineering (Netflix Monkey)"},
]
},
{
"title": "🔮 Advanced Data Structures",
"type": "submodule",
"path": "advanced_structures",
"lessons": [
{"file": "01_bloom_filter.py", "title": "Bloom Filter (Probabilistic Set)"},
]
},
{
"title": "💾 Distributed Data (Interviews)",
"type": "submodule",
"path": "distributed_data",
"lessons": [
{"file": "01_replication_lag.py", "title": "Replication Lag (Consistency)"},
{"file": "02_sharding_strategies.py", "title": "Sharding (Range vs Hash)"},
{"file": "03_cap_theorem.py", "title": "CAP Theorem Simulation"},
{"file": "04_distributed_locking.py", "title": "Distributed Locks (Redis)"},
{"file": "05_saga_pattern.py", "title": "Saga Pattern (Distributed Tx)"},
]
},
{
"title": "🔩 Database Internals",
"type": "submodule",
"path": "database_internals",
"lessons": [
{"file": "01_acid_transactions.py", "title": "ACID (Atomicity & Isolation)"},
]
},
{"file": "01_llm_load_balancing.py", "title": "1. Load Balancing"},
{"file": "02_semantic_caching.py", "title": "2. Semantic Caching"},
{"file": "03_consistent_hashing.py", "title": "3. Consistent Hashing"},
{"file": "04_distributed_id.py", "title": "4. Distributed IDs"},
{"file": "05_grpc_vs_rest.py", "title": "5. gRPC vs REST (Binary vs JSON)"},
{"file": "challenge_solution.py", "title": "🏆 Capstone Challenge Solution"}
]
},
{
"id": "04",
"title": "🧠 Advanced AI Architecture",
"path": "04_advanced_ai_arch",
"lessons": [
{"file": "00_what_is_a_vector.py", "title": "0A. What is a Vector? (Embeddings)"},
{"file": "00_llm_mechanics.py", "title": "0B. LLM Mechanics (Probabilities)"},
{
"title": "🏭 Production Inference (ChatGPT Stack)",
"type": "submodule",
"path": "production_inference",
"lessons": [
{"file": "01_kv_cache.py", "title": "KV Cache (PagedAttention)"},
{"file": "02_continuous_batching.py", "title": "Continuous Batching (vLLM)"},
{"file": "03_lora_adapters.py", "title": "LoRA Adapters (Multi-Tenant)"},
]
},
{
"title": "📺 Recommender Systems (Netflix Stack)",
"type": "submodule",
"path": "recsys",
"lessons": [
{"file": "01_two_tower_arch.py", "title": "Two-Tower Architecture"},
]
},
{
"title": "✂️ RAG Deep Dive (Chunking/Vectors)",
"type": "submodule",
"path": "rag_deep_dive",
"lessons": [
{"file": "01_chunking_strategies.py", "title": "Chunking Strategies"},
{"file": "02_vector_indexing.py", "title": "Vector Indexing (HNSW)"},
]
},
{
"title": "📉 AI Optimization (Internals)",
"type": "submodule",
"path": "optimization",
"lessons": [
{"file": "01_quantization.py", "title": "Quantization (FP32->INT8)"},
{"file": "03_inference_optimization.py", "title": "Speculative Decoding (Moved)"},
]
},
{"file": "01_rag_pipeline_optimization.py", "title": "RAG Optimization (Pipeline)"},
{"file": "02_agent_orchestrator.py", "title": "Agent Orchestration"},
{"file": "04_mixture_of_experts.py", "title": "Mixture of Experts"},
{"file": "05_reasoning_search.py", "title": "Reasoning (Tree of Thoughts)"},
{"file": "challenge_solution.py", "title": "🏆 Capstone Challenge Solution"}
]
},
{
"id": "05",
"title": "💼 Interview Prep",
"path": "05_interview_prep",
"lessons": [
{
"title": "🧱 Common Components (Coding Round)",
"type": "submodule",
"path": "common_components",
"lessons": [
{"file": "01_notification_service.py", "title": "Notification System (Pub/Sub)"},
{"file": "02_distributed_scheduler.py", "title": "Distributed Scheduler (Cron)"},
{"file": "03_geospatial_index.py", "title": "Geospatial Index (QuadTree/Uber)"},
{"file": "04_lru_cache.py", "title": "LRU Cache (Dict + DoublyLL)"},
{"file": "05_trie_autocomplete.py", "title": "Trie (Prefix Tree/Typeahead)"},
{"file": "06_token_bucket.py", "title": "Token Bucket (Rate Limiter Algo)"},
]
},
{"file": "Q1_RateLimiter/optimal.py", "title": "Q1: Rate Limiter (Redis)"},
{"file": "Q2_URLShortener/optimal.py", "title": "Q2: URL Shortener (Base62)"},
{"file": "Q3_WebCrawler/optimal.py", "title": "Q3: Web Crawler (BFS)"},
{"file": "Q4_ChatSystem/optimal.py", "title": "Q4: Chat System (WhatsApp)"},
{"file": "Q5_MetricsMonitoring/optimal.py", "title": "Q5: Metrics (Datadog)"},
# Batch A: Consumer Apps
{"file": "Q6_NewsFeed/optimal.py", "title": "Q6: News Feed (Push vs Pull)"},
{"file": "Q7_GoogleDrive/optimal.py", "title": "Q7: Google Drive (Block Sync)"},
{"file": "Q8_YouTube/optimal.py", "title": "Q8: YouTube (Adaptive Streaming)"},
{"file": "Q9_GamingLeaderboard/optimal.py", "title": "Q9: Leaderboards (Redis ZSet)"},
# Batch B: Concurrency
{"file": "Q10_Ticketmaster/optimal.py", "title": "Q10: Ticketmaster (Locking)"},
{"file": "Q11_AdClickAggregator/optimal.py", "title": "Q11: Stream Processing (Windows)"},
{"file": "Q12_TrendingTopics/optimal.py", "title": "Q12: Trending Topics (Count-Min)"},
# Batch C: Infra
{"file": "Q13_MessageQueue/optimal.py", "title": "Q13: Kafka (Distributed Log)"},
{"file": "Q14_DistributedSearch/optimal.py", "title": "Q14: Search (Inverted Index)"},
{"file": "Q15_PaymentSystem/optimal.py", "title": "Q15: Payments (Idempotency)"},
]
}
]
# ==========================================
# 🛠️ ENGINE
# ==========================================
console = Console()
def clear():
os.system('cls' if os.name == 'nt' else 'clear')
def render_header():
clear()
console.print(Panel.fit(
"[bold cyan]🤖 AI System Design Course[/bold cyan]\n"
"[dim]Use Arrow Keys to Navigate • Ctrl+C to Exit[/dim]",
border_style="cyan"
))
def show_readme(path):
readme_path = os.path.join(path, "README.md")
if not os.path.exists(readme_path):
console.print(f"[red]No README found at {readme_path}[/red]")
input("Press Enter...")
return
with open(readme_path, "r") as f:
md = Markdown(f.read())
console.print(md)
console.print("\n[dim]Press Enter to return...[/dim]")
input()
def run_script(path, script_name):
full_path = os.path.join(path, script_name)
if not os.path.exists(full_path):
console.print(f"[red]File not found: {full_path}[/red]")
input("Press Enter...")
return
console.print(f"[green]🚀 Running {script_name}...[/green]\n")
try:
# Run and stream output
# Use python from env
env = os.environ.copy()
subprocess.run([sys.executable, script_name], cwd=path, env=env)
except Exception as e:
console.print(f"[red]Error: {e}[/red]")
console.print("\n[dim]Execution Finished. Press Enter...[/dim]")
input()
def view_code(path, script_name):
full_path = os.path.join(path, script_name)
if not os.path.exists(full_path):
console.print(f"[red]File not found: {full_path}[/red]")
return
with open(full_path, "r") as f:
code = f.read()
syntax = Syntax(code, "python", theme="monokai", line_numbers=True)
console.print(syntax)
console.print("\n[dim]Press Enter to return...[/dim]")
input()
def lesson_menu(base_path, lesson):
while True:
render_header()
console.print(f"[bold green]Lesson:[/bold green] {lesson['title']}\n")
action = questionary.select(
"What do you want to do?",
choices=[
"🚀 Run Script (See Bad vs Good)",
"📄 View Code",
"🔙 Back"
]
).ask()
if action == "🔙 Back":
break
elif action == "🚀 Run Script (See Bad vs Good)":
run_script(base_path, lesson['file'])
elif action == "📄 View Code":
view_code(base_path, lesson['file'])
def submodule_menu(base_path, submodule):
sub_path = os.path.join(base_path, submodule['path'])
while True:
render_header()
console.print(f"[bold magenta]Sub-Module:[/bold magenta] {submodule['title']}\n")
choices = [l['title'] for l in submodule['lessons']] + ["🔙 Back"]
selection = questionary.select(
"Select a Lesson:",
choices=choices
).ask()
if selection == "🔙 Back":
break
# Find lesson
lesson = next(l for l in submodule['lessons'] if l['title'] == selection)
lesson_menu(sub_path, lesson)
def module_menu(module):
while True:
render_header()
console.print(f"[bold yellow]Module:[/bold yellow] {module['title']}\n")
choices = ["📖 Read Teacher Guide (README)"] + \
[l['title'] for l in module['lessons']] + \
["🔙 Back to Main Menu"]
selection = questionary.select(
"Select a Topic:",
choices=choices
).ask()
if selection == "🔙 Back to Main Menu":
break
elif selection == "📖 Read Teacher Guide (README)":
show_readme(module['path'])
else:
# Find lesson object
item = next(l for l in module['lessons'] if l['title'] == selection)
if item.get("type") == "submodule":
submodule_menu(module['path'], item)
else:
lesson_menu(module['path'], item)
def main_menu():
while True:
render_header()
choices = [m['title'] for m in COURSE] + ["❌ Exit"]
selection = questionary.select(
"Select a Module to Start:",
choices=choices
).ask()
if selection == "❌ Exit":
console.print("[cyan]Happy Coding! Goodbye! 👋[/cyan]")
sys.exit(0)
# Find module object
module = next(m for m in COURSE if m['title'] == selection)
module_menu(module)
if __name__ == "__main__":
try:
main_menu()
except KeyboardInterrupt:
console.print("\n[red]Exiting...[/red]")
sys.exit(0)