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.1.0.tar.gz (5.0 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.1.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: monadic_context-0.1.0.tar.gz
  • Upload date:
  • Size: 5.0 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.1.0.tar.gz
Algorithm Hash digest
SHA256 251d1ac7cd3e73c02fb93cacbd0f70bfe599ae07b5be4e47be11793a9d916555
MD5 7c1ad2a04c9cdf6bd9552d5f4178837a
BLAKE2b-256 abdafb4b1eb9f91cb8dad48241e616dbde12c92f9c4821cbe4cf3893244a5963

See more details on using hashes here.

File details

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

File metadata

  • Download URL: monadic_context-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 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.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 49a6c5accc38833357b64f97f9a5c89eb29a101134d3a649e6dcec2c87f819e0
MD5 ff0897f2c9f91d1b50c81418b78a023d
BLAKE2b-256 cdfae0dd7c4647e1279f90c2b63d457a14a116aecd400fb6a86cc1278d0fe594

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