from faker import Faker import random SEED = 42 random.seed(SEED) fake = Faker() fake.seed_instance(SEED) class EcommerceFakeData: ''' Generates fake e-commerce data, including customers, products, orders, and payments. ''' def __init__(self): ''' Initializes the FakeCustomerData class. Parameters: n (int): The number of records to generate for each dataset. ''' self.n = 0 self.customers = None self.products = None self.sellers = None self.orders = None self.payments = None self.signup_date = fake.date_between(start_date='-2y', end_date='today') def generate_customers(self, n) -> list[dict]: ''' Generates fake customer data, including: - customer_id: Unique identifier for the customer - name: Full name of the customer - location: Geographical location (latitude, longitude, city, country) - signup_date: Date the customer signed up (formatted as YYYY-MM-DD) - email: Customer's email address, generated based on their name Returns: list[dict]: A list of dictionaries containing customer details. ''' customers = [] for _ in range(n): lat, lon , *_ = fake.local_latlng() customers.append({ "customer_id": fake.uuid4(), "name": fake.name(), "lat": lat, "lon": lon, "signup_date": self.signup_date.strftime('%Y-%m-%d') }) for customer in customers: customer["email"] = f"{customer['name'].replace(' ', '').lower()}@mail.com" return customers def generate_products(self, n) -> list[dict]: ''' Generates fake product data, including: - product_id: Unique identifier for the product - category: Randomly chosen category from a predefined list - price: Randomly generated price between $5 and $200 Returns: list[dict]: A list of dictionaries containing product details. ''' categories = ["Electronics", "Books", "Clothing", "Appliances", "Toys", "Beauty", "Furniture", "Tools", "Pets"] products = [] for _ in range(n): products.append({ "product_id": fake.uuid4(), "category": random.choice(categories), "price": round(random.uniform(5, 200), 2) }) return products def generate_sellers(self, n) -> list[dict]: ''' Generates fake seller data, including: - seller_id: Unique identifier for the seller - location: Geographical location (latitude, longitude, city, country) Returns: list[dict]: A list of dictionaries containing product details. ''' sellers = [] for _ in range(n): lat, lon, *_ = fake.local_latlng() sellers.append({ "seller_id": fake.uuid4(), "lat": lat, "lon": lon }) return sellers def generate_orders(self, n) -> list[dict]: ''' Generates fake order data, including: - order_id: Unique identifier for the order - customer_id: Associated customer ID - order_date: Date of the order, within the signup date range - product_id: ID of the purchased product - seller_id : ID of the seller - quantity: Random quantity (between 1 and 5) - total_amount: Calculated total amount based on quantity and product price Returns: list[dict]: A list of dictionaries containing order details. ''' orders = [] for _ in range(n): customer: dict = random.choice(self.customers) product: dict = random.choice(self.products) seller: dict = random.choice(self.sellers) quantity: int = random.randint(1, 5) orders.append({ "order_id": fake.uuid4(), "customer_id": customer["customer_id"], "order_date": fake.date_between_dates(self.signup_date).strftime('%Y-%m-%d'), "product_id": product["product_id"], "seller_id": seller["seller_id"], "quantity": quantity, "total_amount": round(product["price"] * quantity, 2) }) return orders def generate_payments(self) -> list[dict]: ''' Generates fake payment data, including: - payment_id: Unique identifier for the payment - order_id: Associated order ID - payment_method: Randomly chosen method (Card, PayPal, Wire) - payment_status: Randomly assigned status (Successful, Failed, Pending) with weighted probability - payment_date: Date of payment (same as order date) Returns: list[dict]: A list of dictionaries containing payment details. ''' payments = [] methods = ["Card", "PayPal", "Wire"] statuses = ["Successful", "Failed", "Pending"] for order in self.orders: payments.append({ "payment_id": fake.uuid4(), "order_id": order["order_id"], "payment_method": random.choice(methods), "payment_status": random.choices(statuses, weights=[80, 15, 5])[0], "payment_date": order["order_date"] }) return payments def generate(self, n_cust, n_prod, n_sell, n_ord): ''' Generates all datasets in sequence: customers, products, orders, and payments. ''' self.customers = self.generate_customers(n_cust) self.products = self.generate_products(n_prod) self.sellers = self.generate_sellers(n_sell) self.orders = self.generate_orders(n_ord) self.payments = self.generate_payments()