AI & Python

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/26

encourage image

There's no tags or description

Looks like no tags are added yet.

27 Terms

1
New cards

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


2
New cards

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

3
New cards

Pydantic

A data validation and settings management library, which handles the data parts (parsing, validation, serialization)

the "letter" (the data inside)

4
New cards

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 int is an int, an email is a valid email, etc.

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.


5
New cards

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"}


6
New cards

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.

/items/42

def read_item(item_id: int):

Query Parameter

Filters or modifies the result.

/items?limit=10

def read_items(limit: int = 10):

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).


7
New cards

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 password_hash).

Implementation

Parsed from the HTTP request body.

Serialized from the return value of the endpoint function.


8
New cards

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 params


9
New cards

APIRouter

  • 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().


10
New cards

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.


11
New cards

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:

  1. 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.

  2. ReDoc (/redoc): A highly organized, professional-looking documentation layout focused on readability and easy navigation.


12
New cards

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


13
New cards

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.


14
New cards

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.

15
New cards

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')


16
New cards

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).


17
New cards

Label-based vs. Integer-based Selection

Pandas distinguishes between selecting data by its name (label) and its position (integer).

Method

Type

Description

Example

.loc

Label-based

Accesses data using the actual names of rows and columns.

df.loc[0, 'Age']

.iloc

Integer-based

Accesses data using the numerical position (0-indexed).

df.iloc[0, 1]


18
New cards

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')]


19
New cards

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).


20
New cards

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'])


21
New cards

Split-Apply-Combine Pattern

The most powerful way to analyze data is through the .groupby() method. This follows a three-step logic:

  1. Split: Break the data into groups based on a key (e.g., grouping by "City").

  2. Apply: Perform a calculation on each group (e.g., calculating the mean age).

  3. Combine: Merge the results back into a new structure.

# Calculate average salary per department
avg_salary = df.groupby('Department')['Salary'].mean()


22
New cards

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 NaN.

Right

Keeps all rows from the right DataFrame; fills missing left-side data with NaN.

Outer

Keeps all rows from both DataFrames, filling gaps with NaN.


23
New cards

pytest

the industry-standard framework for writing and running Python tests

pip install pytest


24
New cards

Test 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_.

25
New cards

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.

26
New cards

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.


27
New cards