HomeSample Page

Sample Page Title


On this tutorial, we stroll by a sophisticated, end-to-end exploration of Polyfactory, specializing in how we are able to generate wealthy, practical mock information instantly from Python sort hints. We begin by organising the setting and progressively construct factories for information courses, Pydantic fashions, and attrs-based courses, whereas demonstrating customization, overrides, calculated fields, and the technology of nested objects. As we transfer by every snippet, we present how we are able to management randomness, implement constraints, and mannequin real-world constructions, making this tutorial instantly relevant to testing, prototyping, and data-driven growth workflows. Try the FULL CODES right here.

import subprocess
import sys


def install_package(package deal):
   subprocess.check_call([sys.executable, "-m", "pip", "install", "-q", package])


packages = [
   "polyfactory",
   "pydantic",
   "email-validator",
   "faker",
   "msgspec",
   "attrs"
]


for package deal in packages:
   strive:
       install_package(package deal)
       print(f"✓ Put in {package deal}")
   besides Exception as e:
       print(f"✗ Failed to put in {package deal}: {e}")


print("n")


print("=" * 80)
print("SECTION 2: Primary Dataclass Factories")
print("=" * 80)


from dataclasses import dataclass
from typing import Record, Elective
from datetime import datetime, date
from uuid import UUID
from polyfactory.factories import DataclassFactory


@dataclass
class Deal with:
   avenue: str
   metropolis: str
   nation: str
   zip_code: str


@dataclass
class Particular person:
   id: UUID
   title: str
   electronic mail: str
   age: int
   birth_date: date
   is_active: bool
   deal with: Deal with
   phone_numbers: Record[str]
   bio: Elective[str] = None


class PersonFactory(DataclassFactory[Person]):
   cross


individual = PersonFactory.construct()
print(f"Generated Particular person:")
print(f"  ID: {individual.id}")
print(f"  Identify: {individual.title}")
print(f"  Electronic mail: {individual.electronic mail}")
print(f"  Age: {individual.age}")
print(f"  Deal with: {individual.deal with.metropolis}, {individual.deal with.nation}")
print(f"  Cellphone Numbers: {individual.phone_numbers[:2]}")
print()


individuals = PersonFactory.batch(5)
print(f"Generated {len(individuals)} individuals:")
for i, p in enumerate(individuals, 1):
   print(f"  {i}. {p.title} - {p.electronic mail}")
print("n")

We arrange the setting and guarantee all required dependencies are put in. We additionally introduce the core thought of utilizing Polyfactory to generate mock information from sort hints. By initializing the essential dataclass factories, we set up the inspiration for all subsequent examples.

print("=" * 80)
print("SECTION 3: Customizing Manufacturing unit Conduct")
print("=" * 80)


from faker import Faker
from polyfactory.fields import Use, Ignore


@dataclass
class Worker:
   employee_id: str
   full_name: str
   division: str
   wage: float
   hire_date: date
   is_manager: bool
   electronic mail: str
   internal_notes: Elective[str] = None


class EmployeeFactory(DataclassFactory[Employee]):
   __faker__ = Faker(locale="en_US")
   __random_seed__ = 42


   @classmethod
   def employee_id(cls) -> str:
       return f"EMP-{cls.__random__.randint(10000, 99999)}"


   @classmethod
   def full_name(cls) -> str:
       return cls.__faker__.title()


   @classmethod
   def division(cls) -> str:
       departments = ["Engineering", "Marketing", "Sales", "HR", "Finance"]
       return cls.__random__.alternative(departments)


   @classmethod
   def wage(cls) -> float:
       return spherical(cls.__random__.uniform(50000, 150000), 2)


   @classmethod
   def electronic mail(cls) -> str:
       return cls.__faker__.company_email()


workers = EmployeeFactory.batch(3)
print("Generated Workers:")
for emp in workers:
   print(f"  {emp.employee_id}: {emp.full_name}")
   print(f"    Division: {emp.division}")
   print(f"    Wage: ${emp.wage:,.2f}")
   print(f"    Electronic mail: {emp.electronic mail}")
   print()
print()


print("=" * 80)
print("SECTION 4: Subject Constraints and Calculated Fields")
print("=" * 80)


@dataclass
class Product:
   product_id: str
   title: str
   description: str
   value: float
   discount_percentage: float
   stock_quantity: int
   final_price: Elective[float] = None
   sku: Elective[str] = None


class ProductFactory(DataclassFactory[Product]):
   @classmethod
   def product_id(cls) -> str:
       return f"PROD-{cls.__random__.randint(1000, 9999)}"


   @classmethod
   def title(cls) -> str:
       adjectives = ["Premium", "Deluxe", "Classic", "Modern", "Eco"]
       nouns = ["Widget", "Gadget", "Device", "Tool", "Appliance"]
       return f"{cls.__random__.alternative(adjectives)} {cls.__random__.alternative(nouns)}"


   @classmethod
   def value(cls) -> float:
       return spherical(cls.__random__.uniform(10.0, 1000.0), 2)


   @classmethod
   def discount_percentage(cls) -> float:
       return spherical(cls.__random__.uniform(0, 30), 2)


   @classmethod
   def stock_quantity(cls) -> int:
       return cls.__random__.randint(0, 500)


   @classmethod
   def construct(cls, **kwargs):
       occasion = tremendous().construct(**kwargs)
       if occasion.final_price is None:
           occasion.final_price = spherical(
               occasion.value * (1 - occasion.discount_percentage / 100), 2
           )
       if occasion.sku is None:
           name_part = occasion.title.exchange(" ", "-").higher()[:10]
           occasion.sku = f"{occasion.product_id}-{name_part}"
       return occasion


merchandise = ProductFactory.batch(3)
print("Generated Merchandise:")
for prod in merchandise:
   print(f"  {prod.sku}")
   print(f"    Identify: {prod.title}")
   print(f"    Value: ${prod.value:.2f}")
   print(f"    Low cost: {prod.discount_percentage}%")
   print(f"    Remaining Value: ${prod.final_price:.2f}")
   print(f"    Inventory: {prod.stock_quantity} models")
   print()
print()

We deal with producing easy however practical mock information utilizing dataclasses and default Polyfactory habits. We present the right way to rapidly create single cases and batches with out writing any customized logic. It helps us validate how Polyfactory robotically interprets sort hints to populate nested constructions.

print("=" * 80)
print("SECTION 6: Complicated Nested Buildings")
print("=" * 80)


from enum import Enum


class OrderStatus(str, Enum):
   PENDING = "pending"
   PROCESSING = "processing"
   SHIPPED = "shipped"
   DELIVERED = "delivered"
   CANCELLED = "cancelled"


@dataclass
class OrderItem:
   product_name: str
   amount: int
   unit_price: float
   total_price: Elective[float] = None


@dataclass
class ShippingInfo:
   provider: str
   tracking_number: str
   estimated_delivery: date


@dataclass
class Order:
   order_id: str
   customer_name: str
   customer_email: str
   standing: OrderStatus
   gadgets: Record[OrderItem]
   order_date: datetime
   shipping_info: Elective[ShippingInfo] = None
   total_amount: Elective[float] = None
   notes: Elective[str] = None


class OrderItemFactory(DataclassFactory[OrderItem]):
   @classmethod
   def product_name(cls) -> str:
       merchandise = ["Laptop", "Mouse", "Keyboard", "Monitor", "Headphones",
                  "Webcam", "USB Cable", "Phone Case", "Charger", "Tablet"]
       return cls.__random__.alternative(merchandise)


   @classmethod
   def amount(cls) -> int:
       return cls.__random__.randint(1, 5)


   @classmethod
   def unit_price(cls) -> float:
       return spherical(cls.__random__.uniform(5.0, 500.0), 2)


   @classmethod
   def construct(cls, **kwargs):
       occasion = tremendous().construct(**kwargs)
       if occasion.total_price is None:
           occasion.total_price = spherical(occasion.amount * occasion.unit_price, 2)
       return occasion


class ShippingInfoFactory(DataclassFactory[ShippingInfo]):
   @classmethod
   def provider(cls) -> str:
       carriers = ["FedEx", "UPS", "DHL", "USPS"]
       return cls.__random__.alternative(carriers)


   @classmethod
   def tracking_number(cls) -> str:
       return ''.be part of(cls.__random__.selections('0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ', ok=12))


class OrderFactory(DataclassFactory[Order]):
   @classmethod
   def order_id(cls) -> str:
       return f"ORD-{datetime.now().12 months}-{cls.__random__.randint(100000, 999999)}"


   @classmethod
   def gadgets(cls) -> Record[OrderItem]:
       return OrderItemFactory.batch(cls.__random__.randint(1, 5))


   @classmethod
   def construct(cls, **kwargs):
       occasion = tremendous().construct(**kwargs)
       if occasion.total_amount is None:
           occasion.total_amount = spherical(sum(merchandise.total_price for merchandise in occasion.gadgets), 2)
       if occasion.shipping_info is None and occasion.standing in [OrderStatus.SHIPPED, OrderStatus.DELIVERED]:
           occasion.shipping_info = ShippingInfoFactory.construct()
       return occasion


orders = OrderFactory.batch(2)
print("Generated Orders:")
for order in orders:
   print(f"n  Order {order.order_id}")
   print(f"    Buyer: {order.customer_name} ({order.customer_email})")
   print(f"    Standing: {order.standing.worth}")
   print(f"    Objects ({len(order.gadgets)}):")
   for merchandise so as.gadgets:
       print(f"      - {merchandise.amount}x {merchandise.product_name} @ ${merchandise.unit_price:.2f} = ${merchandise.total_price:.2f}")
   print(f"    Complete: ${order.total_amount:.2f}")
   if order.shipping_info:
       print(f"    Delivery: {order.shipping_info.provider} - {order.shipping_info.tracking_number}")
print("n")

We construct extra complicated area logic by introducing calculated and dependent fields inside factories. We present how we are able to derive values comparable to closing costs, totals, and transport particulars after object creation. This enables us to mannequin practical enterprise guidelines instantly inside our take a look at information turbines.

print("=" * 80)
print("SECTION 7: Attrs Integration")
print("=" * 80)


import attrs
from polyfactory.factories.attrs_factory import AttrsFactory


@attrs.outline
class BlogPost:
   title: str
   creator: str
   content material: str
   views: int = 0
   likes: int = 0
   printed: bool = False
   published_at: Elective[datetime] = None
   tags: Record[str] = attrs.discipline(manufacturing facility=listing)


class BlogPostFactory(AttrsFactory[BlogPost]):
   @classmethod
   def title(cls) -> str:
       templates = [
           "10 Tips for {}",
           "Understanding {}",
           "The Complete Guide to {}",
           "Why {} Matters",
           "Getting Started with {}"
       ]
       subjects = ["Python", "Data Science", "Machine Learning", "Web Development", "DevOps"]
       template = cls.__random__.alternative(templates)
       subject = cls.__random__.alternative(subjects)
       return template.format(subject)


   @classmethod
   def content material(cls) -> str:
       return " ".be part of(Faker().sentences(nb=cls.__random__.randint(3, 8)))


   @classmethod
   def views(cls) -> int:
       return cls.__random__.randint(0, 10000)


   @classmethod
   def likes(cls) -> int:
       return cls.__random__.randint(0, 1000)


   @classmethod
   def tags(cls) -> Record[str]:
       all_tags = ["python", "tutorial", "beginner", "advanced", "guide",
                  "tips", "best-practices", "2024"]
       return cls.__random__.pattern(all_tags, ok=cls.__random__.randint(2, 5))


posts = BlogPostFactory.batch(3)
print("Generated Weblog Posts:")
for submit in posts:
   print(f"n  '{submit.title}'")
   print(f"    Writer: {submit.creator}")
   print(f"    Views: {submit.views:,} | Likes: {submit.likes:,}")
   print(f"    Revealed: {submit.printed}")
   print(f"    Tags: {', '.be part of(submit.tags)}")
   print(f"    Preview: {submit.content material[:100]}...")
print("n")


print("=" * 80)
print("SECTION 8: Constructing with Particular Overrides")
print("=" * 80)


custom_person = PersonFactory.construct(
   title="Alice Johnson",
   age=30,
   electronic mail="[email protected]"
)
print(f"Customized Particular person:")
print(f"  Identify: {custom_person.title}")
print(f"  Age: {custom_person.age}")
print(f"  Electronic mail: {custom_person.electronic mail}")
print(f"  ID (auto-generated): {custom_person.id}")
print()


vip_customers = PersonFactory.batch(
   3,
   bio="VIP Buyer"
)
print("VIP Prospects:")
for buyer in vip_customers:
   print(f"  {buyer.title}: {buyer.bio}")
print("n")

We lengthen Polyfactory utilization to validated Pydantic fashions and attrs-based courses. We display how we are able to respect discipline constraints, validators, and default behaviors whereas nonetheless producing legitimate information at scale. It ensures our mock information stays appropriate with actual utility schemas.

print("=" * 80)
print("SECTION 9: Subject-Stage Management with Use and Ignore")
print("=" * 80)


from polyfactory.fields import Use, Ignore


@dataclass
class Configuration:
   app_name: str
   model: str
   debug: bool
   created_at: datetime
   api_key: str
   secret_key: str


class ConfigFactory(DataclassFactory[Configuration]):
   app_name = Use(lambda: "MyAwesomeApp")
   model = Use(lambda: "1.0.0")
   debug = Use(lambda: False)


   @classmethod
   def api_key(cls) -> str:
       return f"api_key_{''.be part of(cls.__random__.selections('0123456789abcdef', ok=32))}"


   @classmethod
   def secret_key(cls) -> str:
       return f"secret_{''.be part of(cls.__random__.selections('0123456789abcdef', ok=64))}"


configs = ConfigFactory.batch(2)
print("Generated Configurations:")
for config in configs:
   print(f"  App: {config.app_name} v{config.model}")
   print(f"    Debug: {config.debug}")
   print(f"    API Key: {config.api_key[:20]}...")
   print(f"    Created: {config.created_at}")
   print()
print()


print("=" * 80)
print("SECTION 10: Mannequin Protection Testing")
print("=" * 80)


from pydantic import BaseModel, ConfigDict
from typing import Union


class PaymentMethod(BaseModel):
   model_config = ConfigDict(use_enum_values=True)
   sort: str
   card_number: Elective[str] = None
   bank_name: Elective[str] = None
   verified: bool = False


class PaymentMethodFactory(ModelFactory[PaymentMethod]):
   __model__ = PaymentMethod


payment_methods = [
   PaymentMethodFactory.build(type="card", card_number="4111111111111111"),
   PaymentMethodFactory.build(type="bank", bank_name="Chase Bank"),
   PaymentMethodFactory.build(verified=True),
]


print("Fee Methodology Protection:")
for i, pm in enumerate(payment_methods, 1):
   print(f"  {i}. Kind: {pm.sort}")
   if pm.card_number:
       print(f"     Card: {pm.card_number}")
   if pm.bank_name:
       print(f"     Financial institution: {pm.bank_name}")
   print(f"     Verified: {pm.verified}")
print("n")


print("=" * 80)
print("TUTORIAL SUMMARY")
print("=" * 80)
print("""
This tutorial coated:


1. ✓ Primary Dataclass Factories - Easy mock information technology
2. ✓ Customized Subject Turbines - Controlling particular person discipline values
3. ✓ Subject Constraints - Utilizing PostGenerated for calculated fields
4. ✓ Pydantic Integration - Working with validated fashions
5. ✓ Complicated Nested Buildings - Constructing associated objects
6. ✓ Attrs Help - Various to dataclasses
7. ✓ Construct Overrides - Customizing particular cases
8. ✓ Use and Ignore - Express discipline management
9. ✓ Protection Testing - Making certain complete take a look at information


Key Takeaways:
- Polyfactory robotically generates mock information from sort hints
- Customise technology with classmethods and interior designers
- Helps a number of libraries: dataclasses, Pydantic, attrs, msgspec
- Use PostGenerated for calculated/dependent fields
- Override particular values whereas preserving others random
- Excellent for testing, growth, and prototyping


For extra info:
- Documentation: https://polyfactory.litestar.dev/
- GitHub: https://github.com/litestar-org/polyfactory
""")
print("=" * 80)

We cowl superior utilization patterns comparable to express overrides, fixed discipline values, and protection testing eventualities. We present how we are able to deliberately assemble edge instances and variant cases for sturdy testing. This closing step ties every little thing collectively by demonstrating how Polyfactory helps complete and production-grade take a look at information methods.

In conclusion, we demonstrated how Polyfactory allows us to create complete, versatile take a look at information with minimal boilerplate whereas nonetheless retaining fine-grained management over each discipline. We confirmed the right way to deal with easy entities, complicated nested constructions, and Pydantic mannequin validation, in addition to express discipline overrides, inside a single, constant factory-based method. General, we discovered that Polyfactory allows us to maneuver sooner and take a look at extra confidently, because it reliably generates practical datasets that carefully mirror production-like eventualities with out sacrificing readability or maintainability.


Try the FULL CODES right here. Additionally, be at liberty to observe us on Twitter and don’t neglect to hitch our 100k+ ML SubReddit and Subscribe to our E-newsletter. Wait! are you on telegram? now you’ll be able to be part of us on telegram as properly.


Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles