Advanced Python: Working with Databases & APIs
1. Advanced SQLAlchemy Features
- ORM Inheritance (Single Table, Joined Table)
- Lazy Loading and Eager Loading
- Custom Data Types
- Schema Migrations using Alembic
Example (Joined Table Inheritance):
class Person(Base):
__tablename__ = 'person'
id = Column(Integer, primary_key=True)
name = Column(String)
class Student(Person):
__tablename__ = 'student'
id = Column(Integer, ForeignKey('person.id'), primary_key=True)
grade = Column(Integer)
2. Connection Pooling
Efficiently manage DB connections using SQLAlchemy's pooling feature.
from sqlalchemy import create_engine
engine = create_engine("sqlite:///data.db", pool_size=10, max_overflow=20)
3. Async Databases with SQLModel or Databases
Async support for high concurrency apps (e.g., FastAPI).
from databases import Database
database = Database("sqlite:///test.db")
await database.connect()
await database.fetch_all("SELECT * FROM students")
4. Object-Relational Impedance Mismatch
Challenges in syncing DB schema with object models.
Use hybrid properties, mixins, and raw SQL when needed.
5. Using PostgreSQL with psycopg2
Access advanced DBMS features like JSON, full-text search.
import psycopg2
conn = psycopg2.connect("dbname=test user=postgres")
cur = conn.cursor()
Advanced Python: Working with Databases & APIs
cur.execute("SELECT * FROM students;")
6. API Authentication Techniques
Token-based (Bearer Token), OAuth2, API Keys
headers = {"Authorization": "Bearer <token>"}
res = requests.get("https://api.example.com/data", headers=headers)
7. Caching API Results
Use `requests_cache` to cache API responses and improve performance.
import requests_cache
requests_cache.install_cache('api_cache')
requests.get("https://api.example.com/data")
8. Error Handling in APIs
Handle timeouts, status codes, malformed responses.
try:
r = requests.get(url, timeout=5)
r.raise_for_status()
except requests.exceptions.RequestException as e:
print("API Error:", e)
9. Integrating APIs with Databases
Combine external API data into your DB (ETL style).
data = requests.get(api_url).json()
for record in data:
db.insert(Student(name=record['name'], grade=record['grade']))
10. ORM Performance Optimization
Use bulk_insert_mappings for fast inserts.
Avoid N+1 query problem using `joinedload`.
from sqlalchemy.orm import joinedload
session.query(Student).options(joinedload(Student.courses)).all()