Editor

The Complete ETLPipeline Class

Advanced · Write the code · from Build a Simple ETL Pipeline: Extract, Transform, Load

From the lesson: Step 7: Build the Complete ETLPipeline Class

Now let's assemble everything into a single ETLPipeline class. A class-based pipeline is easier to configure, test, and rerun. It tracks statistics from each step so you can monitor the pipeline's health.

Read this part of the lesson

Build an ETLPipeline class with these methods:

1. __init__(self) - initializes stats dict: {extracted: 0, validated: 0, errors: 0, transformed: 0, loaded: 0}.

2. extract(self, api_data, csv_data) - combines both sources, updates stats, returns records.

3. validate(self, records) - filters out records with empty name or non-positive amount, updates stats, returns valid records.

4. transform(self, records) - cleans (strip, title case names, lowercase emails) and adds amount_tier, updates stats, returns transformed records.

5. load(self, records) - creates SQLite :memory: db, loads records, stores connection as self.conn, updates stats.

6. run(self, api_data, csv_data) - runs all steps in order, prints a summary report.

Test by running the pipeline with sample data. The summary should print "=== PIPELINE SUMMARY ===" and each stat.

import sqlite3
class ETLPipeline:
def __init__(self):
self.stats = {'extracted': 0, 'validated': 0, 'errors': 0, 'transformed': 0, 'loaded': 0}
self.conn = None
def extract(self, api_data, csv_data):
pass
def validate(self, records):
pass
def transform(self, records):
pass
def load(self, records):
pass
def run(self, api_data, csv_data):
pass
# Test data
api_data = [
{'id': 1, 'name': 'Alice Johnson', 'email': 'ALICE@example.com', 'amount': 150.00},
{'id': 2, 'name': '', 'email': 'bad@test.com', 'amount': 100.00},
{'id': 3, 'name': 'Charlie Brown', 'email': '', 'amount': 75.00},
]
csv_data = """id,name,email,amount
4,Diana Prince,diana@example.com,320.00
5,Eve Wilson,eve@test.com,-50.00
6,Frank Miller,frank@example.com,95.50"""
pipeline = ETLPipeline()
pipeline.run(api_data, csv_data)

More ETL Pipeline exercises

All Python exercises · 50 practice problems · Practical Projects quiz