Skip to content

Latest commit

 

History

History
474 lines (370 loc) · 15.3 KB

File metadata and controls

474 lines (370 loc) · 15.3 KB

Generating Realistic Fake Data with Faker Python

Slide 1: Introduction to Faker Package

Faker is a Python library that generates realistic fake data for testing and development purposes. It provides a comprehensive set of methods to create synthetic data while maintaining data consistency and relationships, making it invaluable for database seeding and API testing scenarios.

from faker import Faker

# Initialize Faker with English locale
fake = Faker('en_US')

# Generate basic personal information
print(f"Name: {fake.name()}")
print(f"Email: {fake.email()}")
print(f"Address: {fake.address()}")

# Example output:
# Name: John Smith
# Email: jsmith84@example.com
# Address: 123 Main St, Apt 4B, New York, NY 10001

Slide 2: Customizing Faker Providers

Faker's provider system allows developers to create custom data generators tailored to specific business requirements. These providers can generate domain-specific data while maintaining the realistic nature of the generated information through careful pattern matching and validation.

from faker.providers import BaseProvider
from faker import Faker

class CustomProvider(BaseProvider):
    def product_code(self):
        return f"PRD-{self.random_int(min=1000, max=9999)}"
    
    def status(self):
        return self.random_element(['active', 'pending', 'archived'])

fake = Faker()
fake.add_provider(CustomProvider)

print(f"Product: {fake.product_code()}")
print(f"Status: {fake.status()}")

Slide 3: Generating Consistent Data Sets

One of Faker's powerful features is the ability to generate consistent datasets using seed values. This ensures reproducibility in testing environments and allows developers to create reliable test scenarios with predetermined outcomes.

from faker import Faker

# Create two Faker instances with the same seed
fake1 = Faker()
fake2 = Faker()

Faker.seed(12345)

# Both instances will generate identical sequences
for _ in range(3):
    print(f"Faker 1: {fake1.name()}")
    print(f"Faker 2: {fake2.name()}")
    print("---")

Slide 4: Creating Realistic Company Data

When developing business applications, generating realistic company information is crucial. Faker provides specialized methods for creating corporate entities, including company names, catch phrases, and business-specific identifiers.

from faker import Faker
fake = Faker()

company_data = {
    'name': fake.company(),
    'industry': fake.company_suffix(),
    'catch_phrase': fake.catch_phrase(),
    'ein': fake.ein(),
    'duns': fake.duns_number()
}

for key, value in company_data.items():
    print(f"{key.title()}: {value}")

Slide 5: Generating Time Series Data

In data science applications, time series data generation is essential for testing analytical models. Faker can create datetime sequences with realistic patterns and intervals for temporal data analysis.

from faker import Faker
from datetime import datetime, timedelta

fake = Faker()

def generate_timeseries(start_date, num_points, interval_minutes=60):
    data = []
    current_date = start_date
    
    for _ in range(num_points):
        value = fake.pyfloat(min_value=10, max_value=100, right_digits=2)
        data.append({'timestamp': current_date, 'value': value})
        current_date += timedelta(minutes=interval_minutes)
    
    return data

start = datetime(2024, 1, 1)
series = generate_timeseries(start, 5)
for point in series:
    print(f"Time: {point['timestamp']}, Value: {point['value']}")

Slide 6: Creating Realistic User Profiles

Modern applications often require comprehensive user profiles for testing user management systems. This implementation demonstrates how to generate complete user profiles with consistent related data.

from faker import Faker
import json

fake = Faker()

def generate_user_profile():
    profile = {
        'user_id': fake.uuid4(),
        'username': fake.user_name(),
        'password_hash': fake.sha256(),
        'personal': {
            'name': fake.name(),
            'dob': fake.date_of_birth(minimum_age=18, maximum_age=90).isoformat(),
            'phone': fake.phone_number(),
            'email': fake.email()
        },
        'address': {
            'street': fake.street_address(),
            'city': fake.city(),
            'state': fake.state(),
            'zipcode': fake.zipcode(),
            'coordinates': {
                'lat': float(fake.latitude()),
                'lng': float(fake.longitude())
            }
        }
    }
    return profile

# Generate and display a user profile
print(json.dumps(generate_user_profile(), indent=2))

Slide 7: Financial Data Generation

Financial applications require precise and realistic monetary data for testing trading systems and financial reports. This implementation showcases how to generate structured financial transactions with appropriate formatting and validation.

from faker import Faker
from decimal import Decimal
import random
from datetime import datetime, timedelta

fake = Faker()

def generate_transaction():
    transaction_types = ['DEPOSIT', 'WITHDRAWAL', 'TRANSFER', 'PAYMENT']
    amount = round(Decimal(random.uniform(10.00, 10000.00)), 2)
    
    return {
        'transaction_id': fake.uuid4(),
        'timestamp': fake.date_time_this_month().isoformat(),
        'type': random.choice(transaction_types),
        'amount': float(amount),
        'currency': 'USD',
        'account_number': fake.bban(),
        'description': fake.text(max_nb_chars=50),
        'status': random.choice(['COMPLETED', 'PENDING', 'FAILED'])
    }

# Generate sample transactions
transactions = [generate_transaction() for _ in range(3)]
for tx in transactions:
    print(f"Transaction ID: {tx['transaction_id']}")
    print(f"Amount: ${tx['amount']:.2f}")
    print(f"Type: {tx['type']}")
    print("---")

Slide 8: Generating Healthcare Records

Healthcare applications require specialized data that adheres to medical terminology and formatting standards. This implementation creates realistic patient records while maintaining HIPAA-compliant data structures.

from faker import Faker
import random

fake = Faker()

class HealthcareProvider(fake.providers.BaseProvider):
    medical_conditions = ['Hypertension', 'Diabetes Type 2', 'Asthma', 'Arthritis']
    blood_types = ['A+', 'A-', 'B+', 'B-', 'AB+', 'AB-', 'O+', 'O-']
    
    def medical_record(self):
        return {
            'patient_id': f"P{fake.random_number(digits=6)}",
            'medical_record_number': f"MRN{fake.random_number(digits=8)}",
            'blood_type': random.choice(self.blood_types),
            'conditions': random.sample(self.medical_conditions, 
                                     k=random.randint(0, 3)),
            'height_cm': random.randint(150, 200),
            'weight_kg': random.randint(45, 120),
            'allergies': [fake.word() for _ in range(random.randint(0, 3))],
            'last_visit': fake.date_this_year().isoformat()
        }

fake.add_provider(HealthcareProvider)
patient_record = fake.medical_record()
print(f"Patient Record:\n{patient_record}")

Slide 9: Generating IoT Sensor Data

IoT applications require realistic sensor data streams for testing and development. This implementation creates time-series data with realistic patterns and anomalies typical of IoT sensor networks.

from faker import Faker
import numpy as np
from datetime import datetime, timedelta

fake = Faker()

def generate_sensor_data(num_readings=100, sensor_type='temperature'):
    base_value = {
        'temperature': 22.0,
        'humidity': 45.0,
        'pressure': 1013.25
    }.get(sensor_type, 0)
    
    noise_factor = {
        'temperature': 0.5,
        'humidity': 2.0,
        'pressure': 5.0
    }.get(sensor_type, 1.0)
    
    start_time = datetime.now() - timedelta(hours=num_readings)
    
    readings = []
    for i in range(num_readings):
        timestamp = start_time + timedelta(hours=i)
        value = base_value + np.random.normal(0, noise_factor)
        readings.append({
            'sensor_id': fake.uuid4(),
            'timestamp': timestamp.isoformat(),
            'type': sensor_type,
            'value': round(value, 2),
            'unit': {
                'temperature': '°C',
                'humidity': '%',
                'pressure': 'hPa'
            }.get(sensor_type, 'units')
        })
    
    return readings

# Generate sample sensor readings
temp_data = generate_sensor_data(5, 'temperature')
for reading in temp_data:
    print(f"Time: {reading['timestamp']}")
    print(f"Value: {reading['value']}{reading['unit']}")
    print("---")

Slide 10: E-commerce Data Generation

E-commerce testing requires complex, interrelated data structures for products, orders, and customer interactions. This implementation creates realistic product catalogs with consistent pricing and inventory data.

from faker import Faker
import random
from decimal import Decimal

fake = Faker()

def generate_product_catalog(num_products=10):
    categories = ['Electronics', 'Clothing', 'Books', 'Home & Garden']
    
    def generate_product():
        price = round(Decimal(random.uniform(9.99, 999.99)), 2)
        discount = round(random.uniform(0, 0.3), 2)
        
        return {
            'product_id': fake.uuid4(),
            'sku': f"SKU-{fake.random_number(digits=6)}",
            'name': fake.catch_phrase(),
            'category': random.choice(categories),
            'description': fake.text(max_nb_chars=200),
            'price': float(price),
            'discount': discount,
            'final_price': float(price * (1 - discount)),
            'stock': random.randint(0, 1000),
            'rating': round(random.uniform(1, 5), 1),
            'reviews_count': random.randint(0, 1000)
        }
    
    return [generate_product() for _ in range(num_products)]

# Generate sample product catalog
catalog = generate_product_catalog(3)
for product in catalog:
    print(f"Product: {product['name']}")
    print(f"Price: ${product['price']:.2f}")
    print(f"Stock: {product['stock']}")
    print("---")

Slide 11: Generating Geographic Data

Geographic information systems require precise location data with proper coordinate systems and boundary validations. This implementation creates realistic geographic datasets with corresponding metadata and regional specifications.

from faker import Faker
from dataclasses import dataclass
from typing import List, Dict
import random

fake = Faker()

@dataclass
class GeoLocation:
    location_id: str
    name: str
    latitude: float
    longitude: float
    elevation: float
    country: str
    postal_code: str
    timezone: str

def generate_geo_cluster(num_points: int = 5, center_lat: float = 40.7, center_lng: float = -74.0, radius_km: float = 10):
    locations = []
    
    for _ in range(num_points):
        # Generate points within radius of center
        lat_offset = random.uniform(-radius_km/111, radius_km/111)
        lng_offset = random.uniform(-radius_km/111, radius_km/111)
        
        location = GeoLocation(
            location_id=fake.uuid4(),
            name=fake.city(),
            latitude=round(center_lat + lat_offset, 6),
            longitude=round(center_lng + lng_offset, 6),
            elevation=random.uniform(0, 500),
            country=fake.country(),
            postal_code=fake.postcode(),
            timezone=fake.timezone()
        )
        locations.append(location)
    
    return locations

# Generate and display sample locations
locations = generate_geo_cluster(3)
for loc in locations:
    print(f"Location: {loc.name}")
    print(f"Coordinates: ({loc.latitude}, {loc.longitude})")
    print(f"Timezone: {loc.timezone}")
    print("---")

Slide 12: Creating Testing Datasets for Machine Learning

Generate synthetic datasets for machine learning model testing with controlled distributions and patterns. This implementation creates datasets with specified features and target variables suitable for various ML algorithms.

from faker import Faker
import numpy as np
import pandas as pd
from sklearn.preprocessing import StandardScaler

fake = Faker()

def generate_ml_dataset(n_samples=1000, n_features=5, target_type='classification'):
    # Generate feature names
    feature_names = [f'feature_{i}' for i in range(n_features)]
    
    # Create random feature data with correlations
    X = np.random.randn(n_samples, n_features)
    
    # Add some non-linear relationships
    X[:, 0] = np.sin(X[:, 1]) + np.random.randn(n_samples) * 0.1
    
    # Generate target variable
    if target_type == 'classification':
        y = (X[:, 0] + X[:, 1] > 0).astype(int)
    else:  # regression
        y = 2 * X[:, 0] + 3 * X[:, 1] + np.random.randn(n_samples) * 0.1
    
    # Create DataFrame
    df = pd.DataFrame(X, columns=feature_names)
    df['target'] = y
    
    # Add some categorical features
    df['category'] = [fake.random_element(['A', 'B', 'C']) for _ in range(n_samples)]
    
    return df

# Generate and display sample dataset
dataset = generate_ml_dataset(n_samples=5)
print("Sample ML Dataset:")
print(dataset.head())
print("\nDataset Info:")
print(dataset.describe().round(2))

Slide 13: Document Generation with Templates

Create realistic document templates filled with faker data for testing document processing systems. This implementation demonstrates how to generate structured documents with consistent formatting and relationships.

from faker import Faker
from jinja2 import Template
import json
from datetime import datetime

fake = Faker()

class DocumentGenerator:
    def __init__(self):
        self.template = """
{
    "document_id": "{{document_id}}",
    "created_at": "{{created_at}}",
    "type": "{{doc_type}}",
    "metadata": {
        "author": "{{author}}",
        "department": "{{department}}",
        "version": "{{version}}"
    },
    "content": {
        "title": "{{title}}",
        "body": "{{body}}",
        "tags": {{tags}},
        "references": {{references}}
    }
}"""
    
    def generate_document(self):
        template = Template(self.template)
        
        document = template.render(
            document_id=fake.uuid4(),
            created_at=datetime.now().isoformat(),
            doc_type=fake.random_element(['report', 'memo', 'proposal']),
            author=fake.name(),
            department=fake.company_suffix(),
            version=f"{fake.random_int(1,5)}.{fake.random_int(0,9)}",
            title=fake.catch_phrase(),
            body=fake.text(max_nb_chars=200),
            tags=json.dumps([fake.word() for _ in range(3)]),
            references=json.dumps([fake.uri() for _ in range(2)])
        )
        
        return json.loads(document)

# Generate sample document
doc_gen = DocumentGenerator()
document = doc_gen.generate_document()
print(json.dumps(document, indent=2))

Slide 14: Additional Resources