Skip to main content

No project description provided

Project description

Monadic Context

PyPI version Python versions License

A lightweight, type-safe dependency injection library for Python.

Features

  • Type-safe dependency injection with full Python type hints support
  • Zero runtime dependencies - just pure Python
  • Monadic interface for composition and transformation
  • Pythonic generator-based syntax for requesting dependencies
  • Flexible context creation with multiple builder patterns

Installation

Requires Python 3.12+

pip install context

Or with Poetry:

poetry add context

Quick Example

from monadic_context import requires, use
import monadic_context as context

# Define tags for your dependencies
port_tag = context.Tag[int]("port")
host_tag = context.Tag[str]("host")


# Function that requires dependencies from context
@requires
def build_url():
    port = yield from use(port_tag)
    host = yield from use(host_tag)
    return f"http://{host}:{port}"


# Create a context with required dependencies
ctx = context.from_dict({port_tag: 8080, host_tag: "localhost"})

# Run the function with the context
url = ctx.run(build_url())
print(url)  # Output: http://localhost:8080

Context Creation

The library offers multiple ways to create contexts:

# Single dependency
ctx1 = context.of(port_tag)(8080)

# Joining contexts
ctx2 = ctx1.join(context.of(host_tag)("localhost"))

# From pairs (more efficient for multiple dependencies)
ctx3 = context.from_pairs(
    (port_tag, 8080),
    (host_tag, "localhost"),
)

# From dictionary
ctx4 = context.from_dict({port_tag: 8080, host_tag: "localhost"})

Advanced Usage

Monadic Operations

The library supports standard monadic operations:

from context.context import map, bind, pure

# Map over a context-requiring function
home_url = pipe(build_url, map(lambda url: f"{url}/home"))

result = ctx.run(home_url)
print(result)  # Output: http://localhost:8080/home

With Service

For functions that take a service as first argument:

import socket

db_conn_tag = context.Tag[socket.SocketType]("db_conn")


@context.with_service(db_conn_tag)
def configure_server(db_conn: socket.SocketType, timeout=30):
    # Use db_conn to configure server
    return {"connection": db_conn, "timeout": timeout}

Why Use Monadic Context?

  • Testability: Easy to mock dependencies for testing
  • Composability: Combine and transform context-aware functions
  • Type Safety: Full type checking with mypy/pyright
  • Separation of Concerns: Clean separation between business logic and dependency resolution
  • No Runtime Reflection: Unlike some DI frameworks, no runtime reflection or complex containers
  • No Mypy Plugins: No need to reconfigure your programming environment

License

MIT

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

monadic_context-0.2.0.tar.gz (5.2 kB view details)

Uploaded Source

Built Distribution

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

monadic_context-0.2.0-py3-none-any.whl (7.0 kB view details)

Uploaded Python 3

File details

Details for the file monadic_context-0.2.0.tar.gz.

File metadata

  • Download URL: monadic_context-0.2.0.tar.gz
  • Upload date:
  • Size: 5.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.0 CPython/3.12.10 Linux/6.12.21-gentoo-dist

File hashes

Hashes for monadic_context-0.2.0.tar.gz
Algorithm Hash digest
SHA256 cb42c26a0a193c65cf826da9175b7615fd5f85325b7d695fdd557e720ab8ce6f
MD5 b6c94e45367895fa2c5d2b1e6b20dbdc
BLAKE2b-256 50b2c1512c2a1524e73a55a6f32cb9b0f59274869b852e8516e32a37820c9cdd

See more details on using hashes here.

File details

Details for the file monadic_context-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: monadic_context-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 7.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.0 CPython/3.12.10 Linux/6.12.21-gentoo-dist

File hashes

Hashes for monadic_context-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c7c6f9ebd357b3fc5f1a822caa2b3d60fd614af0cd0bbc746b70c1e07c395a07
MD5 6b10afaa95293fae8313175f5df34920
BLAKE2b-256 ad6837cf756b65da3e3f556d6c8b13b2b9e4650d77418fd4e7a84a3770af5b33

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