Introduction & Getting Started
Welcome to the Advanced Python & FastAPI Workshop! This course is designed to take you from writing basic Python scripts to developing robust, production-ready REST APIs using FastAPI, Pydantic, and modern database patterns.Workshop Aim & Objectives
The primary aim of this workshop is to bridge the gap between the Python concepts taught in academics and the modern, practical skills required to build scalable backend applications. It also lays a strong foundation for students towards their Generative AI learning journey. By the end of this workshop, you will be able to:- Understand CPython memory management, mutability side-effects, and scope resolutions.
- Master advanced function concepts including positional/keyword arguments, packing/unpacking (
*args/**kwargs), scope resolution (LEGB), closures, decorators, iterators, generator functions, and functional programming patterns. - Write clean, concise, and memory-efficient comprehensions and generator streams.
- Use advanced object-oriented design patterns, abstract classes, and magic methods.
- Build resilient data validation schemas using Pydantic v2.
- Structure scalable web applications with FastAPI using modular routers and advanced dependency injection.
- Design database schemas and manage asynchronous connections using SQLAlchemy 2.0 ORM and Alembic migrations.
- Secure APIs with JWT Authentication and role-based access control.
- Build interactive frontend user interfaces and data-driven dashboards using Streamlit.
- Deploy and containerize applications to production using Docker and cloud environments.
Workshop Curriculum
Here is a roadmap of the topics we will cover during this workshop:- Introduction & Setup (This Module): Setting up Python 3.12+, VS Code, and Astral’s Rust-based
uvmanager. - Advanced Python Fundamentals: Mutability, floating-point precision, integer caching, string operations, and ternary operators.
- Data Structures & Comprehensions: Lists, tuples, dictionaries, sets, queues with
deque, and comprehension mechanics. - Python Functions & Internals: positional/keyword args, print parameters (
sep/end), LEGB scope resolution, and packing/unpacking (*args/**kwargs). - Python Modules, Packages & StdLibs (03b): Namespace imports, directory packaging (
__init__.py), and core modules (math,random,os,pathlib,python-dotenv). - Advanced Python Concepts (04): Closures, decorators, and context managers (
withstatements). - Functional Programming (05): Lambda expressions, generator functions (
yield), and built-ins (map,filter,zip). - Advanced OOP (06): Magic methods (
__repr__,__call__), inheritance, properties (@property), and abstract base classes. - Exception & Data Handling (06b): Asynchronous and synchronous error handling (
try-except-else-finally), custom exceptions, and text/CSV/JSON processing. - Data Validation (07): Modeling schemas, nested validation, serialization, and custom constraints using Pydantic.
- Project Essentials (08): Configurations, project structuring, and package lockfiles.
- FastAPI Essentials (09): Path & query parameters, request bodies, dependency injection, and automatic OpenAPI documentation.
- SQL Databases & ORM (10): Asynchronous database connections, SQLAlchemy models, relationships, and database migrations.
- Authentication & Security (11): Hashing passwords with
passlib, generating JWT tokens, and securing API endpoints. - Prototyping with Streamlit (12): Building user interfaces, widgets, state management, and connecting Streamlit frontends to FastAPI backends.
- Deployment (13): Containerizing applications with Docker, managing multi-stage builds, handling environment variables, and deploying to production.
Getting Started: Setting Up Your Environment
To write and execute code in this workshop, we need to set up a clean, modern development environment. Step 1: Install Python 3.12+ We will use features from recent Python versions (like modern type hinting and syntax).- Go to the Official Python Download Page.
- Download and run the installer for your OS (macOS, Windows, or Linux).
- Important (Windows users): Ensure you check the box that says “Add python.exe to PATH” before clicking install.
Python 3.12.x (or higher).
Step 2: Install and Configure VS Code
We recommend Visual Studio Code (VS Code) as your editor because of its speed, lightweight nature, and excellent Python ecosystem support.
- Download and install VS Code.
- Open VS Code, go to the Extensions view (shortcut:
Ctrl+Shift+XorCmd+Shift+X), and install the following recommended plugins:- Python (by Microsoft): Syntax highlighting, debugging, and code formatting.
- Pylance (by Microsoft): Fast, feature-rich static type checking.
- Ruff (by Astral Software): An extremely fast Python linter and formatter.
- Jupyter (by Microsoft): For running interactive Jupyter notebooks (
.ipynb) directly inside VS Code.
Step 3: Package Managers —
pip vs uv
Historically, Python developers used pip (Python’s default package installer) combined with virtualenv or venv to manage dependencies.
While pip is standard, we will be using uv for this workshop.
What is uv?
Developed by Astral (the creators of Ruff), uv is an extremely fast Python package installer and resolver written in Rust. It serves as a drop-in replacement for pip, pip-tools, virtualenv, and poetry. It is typically 10x to 100x faster than pip and manages virtual environments automatically.
Installing uv
The easiest way to install uv globally is using pip:
- macOS/Linux:
curl -LsSf https://astral.sh/uv/install.sh | sh - Windows:
powershell -c "irm https://astral.sh/uv/install.ps1 | iex"
uv
Let’s initialize a sandbox directory to practice code and manage our virtual environments cleanly.
-
Create a new directory and navigate into it:
-
Initialize a new project with
uv:This command automatically creates a basic project structure including apyproject.tomlfile, a.python-versionlock, and a simplemain.pyfile. -
Create and activate a virtual environment:
-
Install required packages (for example,
fastapi):Notice how incredibly fast the installation completes compared to standard pip!