1/26
Looks like no tags are added yet.
FastAPI
modern, high-performance web framework for building APIs
relies on Pydantic for data validation and Starlette for web capabilities
asynchronous programming and strict type safety
Starlette
A lightweight ASGI framework/toolkit, which handles the web parts (routing, requests, responses, WebSockets).
the "envelope" (the HTTP request)
ASGI: Asynchronous Server Gateway Interface: standard interface between async-capable Python web servers, frameworks, and applications
Pydantic
A data validation and settings management library, which handles the data parts (parsing, validation, serialization)
the "letter" (the data inside)
Pydantic Models
Python classes that inherit from pydantic.BaseModel. They define the exact shape, type, and constraints of the data your application expects.
Feature | Benefit |
|---|---|
Type Validation | Ensures an |
Data Parsing | Automatically converts incoming strings (e.g., from a URL) into the correct Python types. |
Error Reporting | Generates clear, structured error messages when validation fails. |
Serialization | Easily converts Python objects back into JSON for responses. |
REST Endpoints (FastAPI)
Endpoint: function decorated with an HTTP method (like @app.get(), @app.post(), etc.). These functions represent the different "actions" your API can perform.
from fastapi import FastAPI
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Hello World"}Path and Query Parameters
Data can be passed to an endpoint in several ways. The two most common for simple lookups are Path Parameters and Query Parameters.
Parameter Type | Purpose | Example URL | FastAPI Implementation |
|---|---|---|---|
Path Parameter | Identifies a specific resource. |
|
|
Query Parameter | Filters or modifies the result. |
|
|
Path Parameter vs. Query Parameter
Use Path Parameters when the value is an integral part of the resource's identity (e.g., a specific User ID).
Use Query Parameters for optional modifiers, such as sorting, pagination, or filtering (e.g., ?sort=desc).
Request & Response Models
Request Models (the body of an incoming POST/PUT request) and Response Models (the shape of the data returned to the client).
^^ Pydantic models used for both
Feature | Request Model | Response Model |
|---|---|---|
Primary Role | Input Validation: Ensures the client sent all required fields in the correct format. | Output Filtering: Ensures the API only returns the specific fields you want to expose. |
Security | Prevents malformed or malicious data from entering the application. | Prevents accidental data leakage (e.g., returning a |
Implementation | Parsed from the HTTP request body. | Serialized from the return value of the endpoint function. |
Dependency Injection (FastAPI)
a function (the "dependency") is "injected" into your endpoint
built-in system for this using the Depends function.
Use Cases for DI:
Database Connections: Sharing a single DB session across multiple endpoints.
Security/Authentication: Verifying a JWT token before allowing access to a route.
Common Logic: Sharing pagination or filtering logic.
from fastapi import Depends
async def common_params(q: str | None = None, skip: int = 0, limit: int = 100):
return {"q": q, "skip": skip, "limit": limit}
@app.get("/items/")
async def read_items(params: dict = Depends(common_params)):
return paramsAPIRouter
allows you to split your API into multiple files (modules), organized by feature or resource.
As your API grows, putting every endpoint in a single main.py file becomes unmanageable. APIRouter
users.py → contains all /users/... routes.
items.py → contains all /items/... routes.
main.py → includes all routers via app.include_router().
Middleware
is a function that runs for every single request that enters your application and every single response that leaves it.
Tip
The Onion Model: Think of middleware as layers of an onion. When a request comes in, it passes through each layer from the outside in. When the response is sent back, it passes through the same layers in reverse order (from the inside out).
Common Middleware tasks:
CORS (Cross-Origin Resource Sharing): Allowing your API to be called from different domains.
Logging: Recording every request for auditing.
GZip Compression: Automatically compressing response bodies to save bandwidth.
OpenAPI Integration
FastAPI is built to be OpenAPI-compliant. This means that as you write your code and define your Pydantic models, FastAPI automatically generates a massive, standardized JSON schema describing your entire API.
Because this schema is standardized, FastAPI provides two incredible interactive tools out of the box:
Swagger UI (/docs): An interactive web interface that allows you to browse all your endpoints and test them directly in your browser by sending real requests.
ReDoc (/redoc): A highly organized, professional-looking documentation layout focused on readability and easy navigation.
Pandas
Python library for data manipulation and analysis
provides data structures designed to work w "relational" or "labeled" data
vectorized operations: operations are applied to entire columns at once, bypassing the overhead of slow Python for loops. (large-scale data science)
relies on: Series and DataFrame
Series
is a one-dimensional labeled array capable of holding any data type. You can think of it as a single column in a spreadsheet.
Key Feature: Every element in a Series is associated with an Index. This index allows for rapid lookups and automatic data alignment.
DataFrame
a two-dimensional, size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). Essentially, a DataFrame is a collection of Series objects that share the same index.
Loading Data
Pandas provides highly optimized functions to read data from various formats
import pandas as pd
# Loading from CSV
df = pd.read_csv('data.csv')
# Loading from Excel
df = pd.read_excel('data.xlsx')
# Loading from JSON
df = pd.read_json('data.json')Structural Discovery
Once loaded, use these "quick-look" methods to understand the dataset's health:
df.head(n): View the first n rows.
df.tail(n): View the last n rows.
df.info(): Provides a summary of the DataFrame, including non-null counts and data types (dtypes).
df.describe(): Generates descriptive statistics (mean, std, min, max, etc.) for numerical columns.
df.shape: Returns a tuple representing the dimensionality (rows, columns).
Label-based vs. Integer-based Selection
Pandas distinguishes between selecting data by its name (label) and its position (integer).
Method | Type | Description | Example |
|---|---|---|---|
| Label-based | Accesses data using the actual names of rows and columns. |
|
| Integer-based | Accesses data using the numerical position (0-indexed). |
|
Boolean Indexing
You can filter data by passing a condition inside the selection brackets. This is known as Boolean Indexing.
# Select rows where Age is greater than 25
filtered_df = df[df['Age'] > 25]
# Multiple conditions (use & for AND, \| for OR)
# Note: Each condition MUST be wrapped in parentheses
filtered_df = df[(df['Age'] > 25) & (df['City'] == 'New York')]Handling Missing Values
Missing data is typically represented as NaN (Not a Number).
Detection: df.isna() returns a boolean mask of missing values.
Removal: df.dropna() removes any rows or columns containing missing values.
Imputation: df.fillna(value) replaces missing values with a specific constant or statistical measure (like the mean).
Type Conversion
Incorrect data types (e.g., numbers stored as strings) prevent mathematical operations.
# Convert a column to a specific type
df['Price'] = df['Price'].astype(float)
# Robust datetime conversion
df['Date'] = pd.to_datetime(df['Date'])Split-Apply-Combine Pattern
The most powerful way to analyze data is through the .groupby() method. This follows a three-step logic:
Split: Break the data into groups based on a key (e.g., grouping by "City").
Apply: Perform a calculation on each group (e.g., calculating the mean age).
Combine: Merge the results back into a new structure.
# Calculate average salary per department
avg_salary = df.groupby('Department')['Salary'].mean()Merging & Joining
To combine different DataFrames, we use relational algebra concepts.
pd.merge(): The primary tool for SQL-style joins. You specify a "key" column that exists in both DataFrames.
pd.concat(): Used to "stack" DataFrames on top of each other (axis=0) or side-by-side (axis=1).
Merge Types Comparison
Join Type | Result |
|---|---|
Inner | Only keeps rows where the key exists in both DataFrames. |
Left | Keeps all rows from the left DataFrame; fills missing right-side data with |
Right | Keeps all rows from the right DataFrame; fills missing left-side data with |
Outer | Keeps all rows from both DataFrames, filling gaps with |
pytest
the industry-standard framework for writing and running Python tests
pip install pytestTest Discovery
Pytest automatically finds tests by looking for files that match the patterns test_*.py or *_test.py. Inside these files, it looks for functions prefixed with test_.
Python collections Module
The collections module in Python provides specialized container datatypes that extend the functionality of the built‑in containers like list, dict, set, and tuple.
OrderedDict
a dictionary subclass that remembers the order in which items were inserted.
Useful:
move_to_end(key, last=True): Move an existing key to the end or beginning.
popitem(last=True): Removes and returns an item.