Skip to main content

Python Domain Events

Base classes for internal domain events with distributed tracing support

📦 What is this?

This library provides base classes for implementing internal domain events (in-process) that follow DDD best practices and support distributed tracing.

🎯 When to Use This Library?

Use BaseDomainEvent (this library) for:

  • 🔷 In-process events (same service side effects)
  • 🔷 Internal coordination (send email, update cache, log activity)
  • 🔷 Bounded context events (within one microservice)
  • 🔷 Immediate processing (synchronous event handling)

Do NOT use for:

  • 🌐 Cross-service communication (use IOutboxEvent from python-outbox-core)
  • 🌐 External integrations (analytics, audit services, third-party)
  • 🌐 Message broker events (Kafka, RabbitMQ)

Event Type Decision Tree

Need to publish an event?
│
├─ Same service only?
│  └─ Use BaseDomainEvent (python-domain-events) ⭐ THIS LIBRARY
│     Examples: Send email, log activity, update cache
│
└─ Other services/external?
   └─ Use IOutboxEvent (python-outbox-core)
      Examples: Notify microservices, analytics, audit

🚀 Quick Start

Installation

pip install -e python-web-toolkit/packages/python-domain-events

Basic Usage

from python_domain_events import BaseDomainEvent
from uuid import UUID
from typing import Optional

class InviteCreatedEvent(BaseDomainEvent):
    """Domain event: Invite was created."""
    event_type: str = "invite.created"
    
    invite_id: int
    token: str
    email: Optional[str]
    
    @classmethod
    def from_entity(cls, invite, correlation_id: Optional[UUID] = None):
        return cls(
            invite_id=invite.id,
            token=invite.token,
            email=invite.email,
            correlation_id=correlation_id
        )

# Create event
event = InviteCreatedEvent.from_entity(invite)

# Event has auto-generated fields
print(event.event_id)        # UUID
print(event.occurred_at)     # datetime
print(event.correlation_id)  # Optional UUID for tracing

📚 Features

✅ Auto-generated event_id - UUID for deduplication
✅ Auto-generated timestamps - No manual datetime.utcnow()
✅ Distributed tracing - correlation_id, causation_id support
✅ Immutability - Events are frozen after creation
✅ Type safety - Full Pydantic validation
✅ Extensible metadata - metadata: Dict[str, Any] for custom fields

🏗️ Components

  • BaseDomainEvent - Base class for domain events
  • IDomainEventHandler - Interface for event handlers
  • InProcessEventDispatcher - Dispatcher for routing events to handlers

📖 License

MIT

Metadata

Release files for python-domain-events 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-domain-events 0.1.0
File Size Uploaded Provenance Actions
python_domain_events-0.1.0.tar.gz 5.4 kB Without provenance Details for python_domain_events-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for python-domain-events 0.1.0
File Interpreter ABI Platform Actions
python_domain_events-0.1.0-py3-none-any.whl Python 3 none any Details for python_domain_events-0.1.0-py3-none-any.whl

Total release size: 10.4 kB

Release files / python_domain_events-0.1.0.tar.gz

Download URL python_domain_events-0.1.0.tar.gz
Size 5.4 kB
Tags Source
SHA-256 checksum
How to use checksums
d53a61215bb636be5002e6a16aec5bf5ca44412fcb789713342977de275ffff8
BLAKE2b-256 checksum
How to use checksums
8f5b63a125a252493acdf67f85be0e29e2df17266357fcad2a39a906bddf0987
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.11

Release files / python_domain_events-0.1.0-py3-none-any.whl

Download URL python_domain_events-0.1.0-py3-none-any.whl
Size 5.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a8c38f57c5d00ab89c411dafefa0e277705adb4a9ea3c5f37f76fc8b62c580a1
BLAKE2b-256 checksum
How to use checksums
84ec0a8694d5d04cffab970d4edcc3bbeba592aedc27b33ba974051fc4a8c350
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.11

Security for python-domain-events 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page