File size: 5,930 Bytes
8a586f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
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()