Skip to main content

A lightweight pub-sub framework.

Project description

splurge-pub-sub

PyPI version Python versions License: MIT

CI Coverage Ruff mypy

A lightweight, thread-safe publish-subscribe framework for Python applications. Splurge provides simple, Pythonic in-process event communication with full type safety and comprehensive error handling.

Features

  • Lightweight: Zero external dependencies, minimal footprint
  • Thread-Safe: Full concurrency support with reentrant locks
  • Type-Safe: Complete type annotations with mypy strict mode compliance
  • Simple API: Subscribe, publish, unsubscribe with intuitive methods
  • Decorator Syntax: @bus.on("topic") for simplified subscriptions
  • Topic Filtering: Wildcard pattern matching for selective message delivery
  • Error Handling: Custom error handlers for failed callbacks
  • Context Manager: Automatic resource cleanup with with statement
  • 95% Coverage: Comprehensive test coverage across all features

Quick Start

Installation

pip install splurge-pub-sub

Basic Usage

from splurge_pub_sub import PubSub, Message

# Create a pub-sub bus
bus = PubSub()

# Subscribe to a topic
def handle_event(msg: Message) -> None:
    print(f"Received: {msg.data}")

sub_id = bus.subscribe("user.created", handle_event)

# Publish a message
bus.publish("user.created", {"id": 123, "name": "Alice"})
# Output: Received: {'id': 123, 'name': 'Alice'}

# Unsubscribe when done
bus.unsubscribe("user.created", sub_id)

Decorator API

@bus.on("user.updated")
def handle_user_updated(msg: Message) -> None:
    print(f"User updated: {msg.data}")

bus.publish("user.updated", {"id": 123, "status": "active"})

Topic Filtering

from splurge_pub_sub import TopicPattern

# Create patterns with wildcards
pattern = TopicPattern("user.*")
pattern.matches("user.created")  # True
pattern.matches("user.updated")  # True
pattern.matches("order.created")  # False

# Patterns with ? for single character
pattern = TopicPattern("user.?.created")
pattern.matches("user.a.created")  # True
pattern.matches("user.ab.created")  # False

Error Handling

def my_error_handler(exc: Exception, topic: str) -> None:
    print(f"Error on topic '{topic}': {exc}")

bus = PubSub(error_handler=my_error_handler)

@bus.on("risky.operation")
def handle_event(msg: Message) -> None:
    raise ValueError("Something went wrong!")

bus.publish("risky.operation", {})  # Error handler called
# Output: Error on topic 'risky.operation': Something went wrong!

Context Manager

with PubSub() as bus:
    bus.subscribe("topic", callback)
    bus.publish("topic", data)
    # Cleanup happens automatically

Documentation

Requirements

  • Python 3.10 or later
  • No external dependencies

License

MIT License - see LICENSE file for details

Author

Jim Schilling

Support

For issues, questions, or contributions, visit the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

splurge_pub_sub-2025.0.0.tar.gz (23.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

splurge_pub_sub-2025.0.0-py3-none-any.whl (28.1 kB view details)

Uploaded Python 3

File details

Details for the file splurge_pub_sub-2025.0.0.tar.gz.

File metadata

  • Download URL: splurge_pub_sub-2025.0.0.tar.gz
  • Upload date:
  • Size: 23.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for splurge_pub_sub-2025.0.0.tar.gz
Algorithm Hash digest
SHA256 bbe8be36422545d429d08ce518b38c57764194d82b4e1f85df1b6bf278f1fcd1
MD5 f96ccc284d3c555dddd43b0a9ff7a544
BLAKE2b-256 95ee3fea69c973116d4cb7974a6696dcead8cfb927df573339160d88dc5918f7

See more details on using hashes here.

File details

Details for the file splurge_pub_sub-2025.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for splurge_pub_sub-2025.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 461cc1aeb709925dce53c7f40e393bc8357ccb6774c130c713b73ad0537eb302
MD5 08f260c50b1714136d4761ffb8b40b21
BLAKE2b-256 1d6a3642349b9c2ccb40152c34182e2fdeac9fe649c4aebfa3d4cc3913e64cba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page