This commit is contained in:
146
src/utils/examples.py
Normal file
146
src/utils/examples.py
Normal file
@@ -0,0 +1,146 @@
|
||||
from typing import Any, Dict, List, Optional
|
||||
import random
|
||||
|
||||
|
||||
FAKE_DATA = {
|
||||
'names': ['John', 'Jane', 'Bob', 'Alice', 'Charlie', 'Diana', 'Eve', 'Frank'],
|
||||
'domains': ['example.com', 'test.org', 'sample.net', 'demo.io'],
|
||||
'cities': ['New York', 'Los Angeles', 'Chicago', 'Houston', 'Phoenix'],
|
||||
'streets': ['Main St', 'Oak Ave', 'Maple Dr', 'Cedar Ln', 'Pine Rd'],
|
||||
'countries': ['USA', 'Canada', 'UK', 'Germany', 'France'],
|
||||
'companies': ['Acme Corp', 'TechStart', 'Global Inc', 'Local LLC', 'Digital Co'],
|
||||
'job_titles': ['Engineer', 'Manager', 'Designer', 'Developer', 'Analyst'],
|
||||
'departments': ['Engineering', 'Marketing', 'Sales', 'HR', 'Finance'],
|
||||
'products': ['Widget', 'Gadget', 'Tool', 'Device', 'Component'],
|
||||
'adjectives': ['Premium', 'Essential', 'Professional', 'Standard', 'Deluxe'],
|
||||
'lorem_words': ['lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit'],
|
||||
'statuses': ['active', 'pending', 'completed', 'cancelled', 'archived'],
|
||||
'id_prefixes': ['usr_', 'ord_', 'prd_', 'inv_', 'txn_']
|
||||
}
|
||||
|
||||
|
||||
def generate_id(prefix: str = None) -> str:
|
||||
prefix = prefix or random.choice(FAKE_DATA['id_prefixes'])
|
||||
return f"{prefix}{random.randint(10000, 99999)}"
|
||||
|
||||
|
||||
def generate_name() -> str:
|
||||
first = random.choice(FAKE_DATA['names'])
|
||||
last = random.choice(FAKE_DATA['names'])
|
||||
return f"{first} {last}"
|
||||
|
||||
|
||||
def generate_email(name: str = None) -> str:
|
||||
name = (name or generate_name()).lower().replace(' ', '.')
|
||||
domain = random.choice(FAKE_DATA['domains'])
|
||||
return f"{name}@{domain}"
|
||||
|
||||
|
||||
def generate_phone() -> str:
|
||||
return f"+1-{random.randint(200, 999)}-{random.randint(100, 999)}-{random.randint(1000, 9999)}"
|
||||
|
||||
|
||||
def generate_address() -> Dict[str, Any]:
|
||||
return {
|
||||
'street': f"{random.randint(100, 9999)} {random.choice(FAKE_DATA['streets'])}",
|
||||
'city': random.choice(FAKE_DATA['cities']),
|
||||
'state': f"{random.choice(['CA', 'NY', 'TX', 'FL', 'IL'])}",
|
||||
'zip': f"{random.randint(10000, 99999)}",
|
||||
'country': random.choice(FAKE_DATA['countries'])
|
||||
}
|
||||
|
||||
|
||||
def generate_company() -> Dict[str, Any]:
|
||||
adj = random.choice(FAKE_DATA['adjectives'])
|
||||
product = random.choice(FAKE_DATA['products'])
|
||||
return {
|
||||
'name': f"{adj} {product} {random.choice(FAKE_DATA['companies'])}",
|
||||
'industry': random.choice(['Technology', 'Healthcare', 'Finance', 'Retail', 'Manufacturing']),
|
||||
'employees': random.randint(10, 10000),
|
||||
'founded': random.randint(1950, 2023)
|
||||
}
|
||||
|
||||
|
||||
def generate_user() -> Dict[str, Any]:
|
||||
return {
|
||||
'id': generate_id('usr_'),
|
||||
'name': generate_name(),
|
||||
'email': generate_email(),
|
||||
'phone': generate_phone(),
|
||||
'address': generate_address(),
|
||||
'created_at': '2024-01-15T10:30:00Z',
|
||||
'status': random.choice(FAKE_DATA['statuses'])
|
||||
}
|
||||
|
||||
|
||||
def generate_product() -> Dict[str, Any]:
|
||||
adj = random.choice(FAKE_DATA['adjectives'])
|
||||
product = random.choice(FAKE_DATA['products'])
|
||||
return {
|
||||
'id': generate_id('prd_'),
|
||||
'name': f"{adj} {product}",
|
||||
'description': ' '.join(random.choices(FAKE_DATA['lorem_words'], k=10)),
|
||||
'price': round(random.uniform(9.99, 999.99), 2),
|
||||
'sku': f"SKU-{random.randint(10000, 99999)}",
|
||||
'in_stock': random.choice([True, False]),
|
||||
'category': random.choice(['Electronics', 'Clothing', 'Home', 'Sports', 'Books'])
|
||||
}
|
||||
|
||||
|
||||
def generate_order() -> Dict[str, Any]:
|
||||
return {
|
||||
'id': generate_id('ord_'),
|
||||
'customer_id': generate_id('usr_'),
|
||||
'items': [generate_product() for _ in range(random.randint(1, 5))],
|
||||
'total': round(random.uniform(50, 2000), 2),
|
||||
'status': random.choice(FAKE_DATA['statuses']),
|
||||
'created_at': '2024-01-15T14:30:00Z'
|
||||
}
|
||||
|
||||
|
||||
def generate(schema: Dict[str, Any], depth: int = 0) -> Any:
|
||||
if depth > 3:
|
||||
return None
|
||||
|
||||
if not schema:
|
||||
return None
|
||||
|
||||
schema_type = schema.get('type', 'object')
|
||||
|
||||
if schema_type == 'object' and 'properties' in schema:
|
||||
result = {}
|
||||
for prop_name, prop_schema in schema['properties'].items():
|
||||
required = schema.get('required', [])
|
||||
if prop_name in required or random.choice([True, False]):
|
||||
result[prop_name] = generate(prop_schema, depth + 1)
|
||||
return result
|
||||
|
||||
elif schema_type == 'array':
|
||||
item_schema = schema.get('items', {})
|
||||
return [generate(item_schema, depth + 1) for _ in range(random.randint(1, 3))]
|
||||
|
||||
elif schema_type == 'string':
|
||||
string_format = schema.get('format')
|
||||
if string_format == 'date-time':
|
||||
return '2024-01-15T10:30:00Z'
|
||||
elif string_format == 'date':
|
||||
return '2024-01-15'
|
||||
elif string_format == 'email':
|
||||
return generate_email()
|
||||
elif string_format == 'uri':
|
||||
return 'https://example.com/api'
|
||||
elif string_format == 'uuid':
|
||||
return '550e8400-e29b-41d4-a716-446655440000'
|
||||
else:
|
||||
return random.choice(['sample', 'example', 'test', 'demo'])
|
||||
|
||||
elif schema_type == 'integer' or schema_type == 'number':
|
||||
return random.randint(1, 1000)
|
||||
|
||||
elif schema_type == 'boolean':
|
||||
return random.choice([True, False])
|
||||
|
||||
elif schema_type == 'null':
|
||||
return None
|
||||
|
||||
return None
|
||||
Reference in New Issue
Block a user