0% found this document useful (0 votes)
17 views2 pages

CBS U4-Ap

The document covers advanced Python techniques for working with databases and APIs, including features of SQLAlchemy, connection pooling, async database handling, and API authentication methods. It also discusses error handling, caching API results, and integrating API data into databases. Performance optimization strategies for ORM are highlighted, such as using bulk inserts and avoiding N+1 query issues.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views2 pages

CBS U4-Ap

The document covers advanced Python techniques for working with databases and APIs, including features of SQLAlchemy, connection pooling, async database handling, and API authentication methods. It also discusses error handling, caching API results, and integrating API data into databases. Performance optimization strategies for ORM are highlighted, such as using bulk inserts and avoiding N+1 query issues.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

You might also like