Files
lnet_tutor/learning_plans/python/01_KNOWLEDGE_GRAPH.md
2025-10-22 20:14:31 +08:00

24 KiB

Python Knowledge Graph - Complete Dependency Map

🌳 Knowledge Tree Structure

This document maps all Python concepts with their dependencies, prerequisites, and learning order.


Level 1: Foundation Nodes (No Prerequisites)

1.1 Basic Syntax & Environment

┌─────────────────────────────┐
│ Python Installation         │
│ - CPython, PyPy, Anaconda  │
│ - PATH configuration       │
│ - pip & package management │
└─────────────────────────────┘
        │
        ├─> IDE Setup (VSCode, PyCharm, Jupyter)
        ├─> REPL Usage
        └─> Virtual Environments (venv)

1.2 Variables & Data Types

┌─────────────────────────────┐
│ Primitive Types             │
│ - int, float, complex       │
│ - str, bytes                │
│ - bool, None                │
└─────────────────────────────┘
        │
        ├─> Type Conversion
        ├─> Variable Assignment
        ├─> Naming Conventions (PEP 8)
        └─> Memory Model (Basics)

1.3 Operators

┌─────────────────────────────┐
│ Arithmetic Operators        │
│ Comparison Operators        │
│ Logical Operators           │
│ Assignment Operators        │
│ Identity & Membership       │
└─────────────────────────────┘
        │
        ├─> Operator Precedence
        ├─> Short-circuit Evaluation
        └─> Walrus Operator (:=)

1.4 Control Flow

┌─────────────────────────────┐
│ Conditional Statements      │
│ - if/elif/else             │
│ - Ternary Operator         │
│ - Match/Case (3.10+)       │
└─────────────────────────────┘
        │
        ├─> Boolean Logic
        ├─> Truthiness
        └─> None Checks

Level 2: Core Data Structures (Requires Level 1)

2.1 Sequences

┌─────────────────────────────┐
│ Lists                       │
│ - Mutable sequences         │
│ - Methods & operations      │
│ - Slicing                   │
└─────────────────────────────┘
        │
        ├─> List Comprehensions
        ├─> Nested Lists
        ├─> List Performance (O(n))
        └─> Copy vs Deep Copy
        
┌─────────────────────────────┐
│ Tuples                      │
│ - Immutable sequences       │
│ - Packing & unpacking       │
│ - Named tuples              │
└─────────────────────────────┘
        │
        ├─> Multiple Return Values
        ├─> Tuple Comprehensions (Generator)
        └─> Hash-ability

┌─────────────────────────────┐
│ Strings                     │
│ - Immutable sequences       │
│ - String methods            │
│ - Formatting (f-strings)    │
└─────────────────────────────┘
        │
        ├─> String Encoding (UTF-8, ASCII)
        ├─> Raw Strings (r"")
        ├─> Multiline Strings
        └─> String Interpolation

2.2 Collections

┌─────────────────────────────┐
│ Sets                        │
│ - Unordered, unique         │
│ - Set operations            │
│ - Frozen sets               │
└─────────────────────────────┘
        │
        ├─> Set Comprehensions
        ├─> Union, Intersection, Difference
        └─> Hash-based lookups O(1)

┌─────────────────────────────┐
│ Dictionaries                │
│ - Key-value pairs           │
│ - Hash tables               │
│ - Dict methods              │
└─────────────────────────────┘
        │
        ├─> Dict Comprehensions
        ├─> Default Values (get, setdefault)
        ├─> OrderedDict, defaultdict, Counter
        └─> ChainMap

2.3 Loops & Iteration

┌─────────────────────────────┐
│ for Loop                    │
│ - Iterating sequences       │
│ - range() function          │
│ - enumerate()               │
└─────────────────────────────┘
        │
        ├─> zip() for parallel iteration
        ├─> Loop else clause
        └─> Break, Continue, Pass

┌─────────────────────────────┐
│ while Loop                  │
│ - Condition-based loops     │
│ - Infinite loops            │
└─────────────────────────────┘
        │
        └─> While-else clause

Level 3: Functions & Modularity (Requires Level 1-2)

3.1 Function Basics

┌─────────────────────────────┐
│ Function Definition         │
│ - def keyword               │
│ - Parameters & Arguments    │
│ - Return values             │
└─────────────────────────────┘
        │
        ├─> Default Arguments
        ├─> Keyword Arguments
        ├─> *args (Variable Positional)
        ├─> **kwargs (Variable Keyword)
        ├─> Positional-only (/)
        └─> Keyword-only (*)

3.2 Function Scope

┌─────────────────────────────┐
│ Scope & Namespaces          │
│ - Local, Enclosing, Global │
│ - LEGB Rule                 │
│ - global & nonlocal         │
└─────────────────────────────┘
        │
        ├─> Namespace Lifecycle
        ├─> Variable Shadowing
        └─> globals(), locals()

3.3 Advanced Functions

┌─────────────────────────────┐
│ First-Class Functions       │
│ - Functions as objects      │
│ - Passing functions         │
│ - Returning functions       │
└─────────────────────────────┘
        │
        ├─> Lambda Functions
        ├─> Closures
        ├─> Higher-Order Functions
        └─> Nested Functions

┌─────────────────────────────┐
│ Built-in Functions          │
│ - map, filter, reduce       │
│ - sorted, reversed          │
│ - any, all                  │
└─────────────────────────────┘
        │
        └─> Functional Programming Patterns

Level 4: Object-Oriented Programming (Requires Level 1-3)

4.1 Classes & Objects

┌─────────────────────────────┐
│ Class Definition            │
│ - class keyword             │
│ - Attributes & Methods      │
│ - __init__() constructor    │
│ - self parameter            │
└─────────────────────────────┘
        │
        ├─> Instance vs Class Attributes
        ├─> Instance vs Class Methods
        ├─> Static Methods
        └─> __str__ and __repr__

4.2 Inheritance

┌─────────────────────────────┐
│ Single Inheritance          │
│ - Parent/Child classes      │
│ - Method Overriding         │
│ - super() function          │
└─────────────────────────────┘
        │
        ├─> Multiple Inheritance
        ├─> Method Resolution Order (MRO)
        ├─> Diamond Problem
        ├─> Mixins Pattern
        └─> Abstract Base Classes (ABC)

4.3 Encapsulation & Polymorphism

┌─────────────────────────────┐
│ Encapsulation               │
│ - Public attributes         │
│ - Protected (_)             │
│ - Private (__)              │
│ - Name Mangling             │
└─────────────────────────────┘
        │
        └─> Property Decorators (@property)

┌─────────────────────────────┐
│ Polymorphism                │
│ - Duck Typing               │
│ - Operator Overloading      │
│ - Magic Methods             │
└─────────────────────────────┘
        │
        ├─> __eq__, __lt__, __gt__
        ├─> __add__, __sub__, __mul__
        ├─> __len__, __getitem__
        └─> __call__

Level 5: Modules & Packages (Requires Level 3-4)

5.1 Module System

┌─────────────────────────────┐
│ Importing Modules           │
│ - import statement          │
│ - from...import             │
│ - import...as               │
└─────────────────────────────┘
        │
        ├─> Module Search Path
        ├─> __name__ == "__main__"
        ├─> Relative vs Absolute Imports
        └─> __init__.py

┌─────────────────────────────┐
│ Standard Library            │
│ - os, sys, pathlib          │
│ - datetime, time            │
│ - math, random              │
│ - json, csv                 │
└─────────────────────────────┘
        │
        ├─> collections Module
        ├─> itertools Module
        ├─> functools Module
        └─> operator Module

Level 6: File & Exception Handling (Requires Level 1-5)

6.1 File Operations

┌─────────────────────────────┐
│ File I/O                    │
│ - open() function           │
│ - Read modes (r, w, a)      │
│ - Text vs Binary            │
└─────────────────────────────┘
        │
        ├─> Context Managers (with)
        ├─> File Methods (read, write, seek)
        ├─> pathlib.Path
        └─> Working with CSV, JSON, XML

6.2 Exception Handling

┌─────────────────────────────┐
│ Exception Basics            │
│ - try/except                │
│ - Multiple except blocks    │
│ - else & finally            │
└─────────────────────────────┘
        │
        ├─> Exception Hierarchy
        ├─> Raising Exceptions (raise)
        ├─> Custom Exceptions
        ├─> Exception Chaining
        └─> Context with Exceptions

Level 7: Advanced Concepts (Requires Level 1-6)

7.1 Iterators & Generators

┌─────────────────────────────┐
│ Iterator Protocol           │
│ - __iter__()                │
│ - __next__()                │
│ - StopIteration             │
└─────────────────────────────┘
        │
        ├─> iter() and next()
        ├─> Custom Iterators
        └─> Iterator vs Iterable

┌─────────────────────────────┐
│ Generators                  │
│ - yield keyword             │
│ - Generator Functions       │
│ - Generator Expressions     │
└─────────────────────────────┘
        │
        ├─> yield from
        ├─> Generator Methods (send, throw, close)
        ├─> Coroutines (async/await)
        └─> Memory Efficiency

7.2 Decorators

┌─────────────────────────────┐
│ Function Decorators         │
│ - Wrapper functions         │
│ - @decorator syntax         │
│ - functools.wraps           │
└─────────────────────────────┘
        │
        ├─> Decorators with Arguments
        ├─> Chaining Decorators
        ├─> Class Decorators
        ├─> Built-in Decorators (@property, @staticmethod)
        └─> lru_cache, singledispatch

7.3 Context Managers

┌─────────────────────────────┐
│ Context Manager Protocol    │
│ - __enter__()               │
│ - __exit__()                │
│ - Exception handling        │
└─────────────────────────────┘
        │
        ├─> contextlib Module
        ├─> @contextmanager Decorator
        ├─> Nested Context Managers
        └─> Resource Management Patterns

Level 8: Type System (Requires Level 1-7)

8.1 Type Hints

┌─────────────────────────────┐
│ Type Annotations            │
│ - Variable annotations      │
│ - Function annotations      │
│ - Return type hints         │
└─────────────────────────────┘
        │
        ├─> typing Module
        ├─> List, Dict, Tuple, Set
        ├─> Optional, Union, Any
        ├─> Generic Types (TypeVar)
        ├─> Protocol & Structural Subtyping
        └─> TypedDict, NamedTuple

┌─────────────────────────────┐
│ Static Type Checking        │
│ - mypy                      │
│ - Type checking strategies  │
│ - Gradual typing            │
└─────────────────────────────┘
        │
        └─> Type Narrowing, Type Guards

Level 9: Metaclasses & Descriptors (Requires Level 4, 7)

9.1 Metaclasses

┌─────────────────────────────┐
│ Metaclass Basics            │
│ - type() as metaclass       │
│ - Class creation process    │
│ - __new__ vs __init__       │
└─────────────────────────────┘
        │
        ├─> Custom Metaclasses
        ├─> __prepare__
        ├─> Metaclass Use Cases
        └─> Class Decorators vs Metaclasses

9.2 Descriptors

┌─────────────────────────────┐
│ Descriptor Protocol         │
│ - __get__()                 │
│ - __set__()                 │
│ - __delete__()              │
└─────────────────────────────┘
        │
        ├─> Data vs Non-Data Descriptors
        ├─> Property Implementation
        ├─> Managed Attributes
        └─> Descriptor Use Cases (Validation, Lazy Loading)

Level 10: Concurrency (Requires Level 1-8)

10.1 Threading

┌─────────────────────────────┐
│ threading Module            │
│ - Thread class              │
│ - Thread lifecycle          │
│ - Daemon threads            │
└─────────────────────────────┘
        │
        ├─> Locks (Lock, RLock)
        ├─> Semaphores, Events, Conditions
        ├─> Thread-Safe Queue
        ├─> ThreadPoolExecutor
        └─> GIL (Global Interpreter Lock)

10.2 Multiprocessing

┌─────────────────────────────┐
│ multiprocessing Module      │
│ - Process class             │
│ - Process lifecycle         │
│ - Shared memory             │
└─────────────────────────────┘
        │
        ├─> Pipes & Queues
        ├─> Locks & Semaphores
        ├─> Manager objects
        ├─> ProcessPoolExecutor
        └─> Process vs Thread (When to use)

10.3 Async Programming

┌─────────────────────────────┐
│ asyncio Basics              │
│ - Event loop                │
│ - Coroutines                │
│ - async/await               │
└─────────────────────────────┘
        │
        ├─> Tasks & Futures
        ├─> asyncio.gather, wait
        ├─> Async Context Managers
        ├─> Async Iterators
        ├─> Async Generators
        └─> aiohttp, aiofiles

Level 11: Memory & Performance (Requires Level 1-10)

11.1 Memory Management

┌─────────────────────────────┐
│ Python Memory Model         │
│ - Object references         │
│ - Reference counting        │
│ - Garbage collection        │
└─────────────────────────────┘
        │
        ├─> gc Module
        ├─> __slots__ Optimization
        ├─> Weak References
        ├─> Memory Profiling
        └─> Memory Leaks Detection

11.2 Performance Optimization

┌─────────────────────────────┐
│ Profiling                   │
│ - cProfile                  │
│ - line_profiler             │
│ - memory_profiler           │
└─────────────────────────────┘
        │
        ├─> Benchmarking (timeit)
        ├─> Performance Best Practices
        ├─> Algorithm Optimization
        └─> Data Structure Selection

┌─────────────────────────────┐
│ Just-In-Time Compilation    │
│ - PyPy                      │
│ - Numba                     │
│ - Cython                    │
└─────────────────────────────┘
        │
        └─> When to use JIT

Level 12: Testing & Quality (Requires Level 1-11)

12.1 Testing Frameworks

┌─────────────────────────────┐
│ unittest Framework          │
│ - TestCase class            │
│ - Assertions                │
│ - Test fixtures             │
└─────────────────────────────┘
        │
        ├─> setUp & tearDown
        ├─> Test discovery
        └─> Test suites

┌─────────────────────────────┐
│ pytest Framework            │
│ - Test functions            │
│ - Fixtures                  │
│ - Parametrization           │
└─────────────────────────────┘
        │
        ├─> Mocking (unittest.mock)
        ├─> Patching
        ├─> Test Coverage (coverage.py)
        └─> Property-Based Testing (Hypothesis)

12.2 Code Quality

┌─────────────────────────────┐
│ Code Style & Linting        │
│ - PEP 8                     │
│ - pylint, flake8            │
│ - black formatter           │
└─────────────────────────────┘
        │
        ├─> Type Checking (mypy)
        ├─> Documentation (docstrings, Sphinx)
        └─> Code Reviews

Level 13: Advanced Topics (Requires Level 1-12)

13.1 Design Patterns

┌─────────────────────────────┐
│ Creational Patterns         │
│ - Singleton, Factory        │
│ - Builder, Prototype        │
└─────────────────────────────┘
        │
        ├─> Structural Patterns (Adapter, Decorator, Facade)
        ├─> Behavioral Patterns (Observer, Strategy, Command)
        └─> Python-Specific Patterns

13.2 Package Development

┌─────────────────────────────┐
│ Package Structure           │
│ - setup.py, pyproject.toml  │
│ - setuptools, pip           │
│ - Versioning (semver)       │
└─────────────────────────────┘
        │
        ├─> Virtual Environments
        ├─> Dependency Management (Poetry, pipenv)
        ├─> Building Distributions (wheel, sdist)
        └─> Publishing to PyPI

13.3 C Extensions

┌─────────────────────────────┐
│ Interfacing with C          │
│ - ctypes                    │
│ - cffi                      │
│ - Python C API              │
└─────────────────────────────┘
        │
        ├─> Cython
        ├─> Performance with C
        └─> When to use C Extensions

🔗 Dependency Map Summary

Critical Path (Must Follow Order)

Level 1 (Basics) 
    → Level 2 (Data Structures)
    → Level 3 (Functions)
    → Level 4 (OOP)
    → Level 5 (Modules)
    → Level 6 (Files & Exceptions)
    → Level 7 (Advanced Concepts)
    → Level 8 (Type System) [parallel with Level 9-10]
    → Level 9 (Metaclasses) [parallel with Level 10]
    → Level 10 (Concurrency) [parallel with Level 9]
    → Level 11 (Performance)
    → Level 12 (Testing)
    → Level 13 (Advanced Topics)

Parallel Learning Opportunities

  • Levels 8, 9, 10 can be learned in parallel after Level 7
  • Level 11 can start after Level 7 (basics) but full mastery requires Level 9-10
  • Level 12 should be practiced throughout the entire journey

📊 Prerequisite Matrix

Topic Prerequisites Can Learn In Parallel With
Iterators Functions, OOP Decorators
Generators Iterators Context Managers
Decorators Functions, Closures Generators
Metaclasses OOP, Decorators Descriptors
Descriptors OOP, Properties Metaclasses
Threading Basic Python Multiprocessing
Multiprocessing Basic Python Threading
Async Generators, Coroutines -
Type Hints All basics Any Level 7+ topic

This knowledge graph should guide your learning journey, ensuring you build strong foundations before tackling advanced topics!