A lightweight dependency injection framework inspired by Symfony
Project description
Dimwiddle
A lightweight dependency injection framework for Python inspired by the Symfony Framework.
Features
- YAML Configuration: Define your services in a clean, readable YAML format
- Autowiring: Automatically inject dependencies into your services
- Factories: Use factory services to create complex service instances
- Tagged Services: Group related services together with tags
- Environment Variables: Inject configuration from environment variables
- Clean Architecture: Organized following clean architecture principles
Installation
pip install dimwiddle
Or install from the repository:
git clone https://github.com/whittlelabs/dimwiddle.git
cd dimwiddle
pip install -e .
Quick Start
1. Define your services in YAML
Create a services.yaml file:
services:
# Basic service
greeting:
class: myapp.services.greeting_service.GreetingService
arguments: ["Hello, %USER_NAME%!"]
# Service with dependency
greeter:
class: myapp.services.greeter.Greeter
arguments: ["@greeting"]
2. Create your service classes
# myapp/services/greeting_service.py
class GreetingService:
def __init__(self, greeting_format):
self.greeting_format = greeting_format
def greet(self, name):
return self.greeting_format.format(name=name)
# myapp/services/greeter.py
class Greeter:
def __init__(self, greeting_service):
self.greeting_service = greeting_service
def greet_user(self, user):
return self.greeting_service.greet(user)
3. Load the container and use your services
from dimwiddle.application.load_definitions import create_container_from_yaml
from dimwiddle.infrastructure.config import EnvConfig
# Create a container with environment variables
config = EnvConfig(dotenv_path=".env")
container = create_container_from_yaml("services.yaml", config=config)
# Get a service
greeter = container.get("greeter")
print(greeter.greet_user("World")) # Hello, World!
Service Definition
Services are defined in YAML with the following properties:
class: The fully qualified class namearguments: List (positional) or dict (named) of constructor argumentsfactory: A two-element list: service reference and method nametags: List of tags to categorize this serviceinject_class: If true, inject the class itself instead of an instance
Argument References
Dimwiddle supports special syntax in service arguments:
@service_name: Reference to another service%ENV_VAR%: Reference to an environment variable!tagged_iterator { tag: tag_name, index_by: property }: Collection of services with a tag~path/to/file.yaml:subpath.to.value: Reference to a value in another YAML file
Configuration
The library uses an abstract configuration provider that can be implemented in different ways:
from dimwiddle.infrastructure.config import EnvConfig, DictConfig
# Load configuration from environment variables and .env file
config = EnvConfig(dotenv_path=".env")
# Or use a dictionary for configuration
config = DictConfig({
"API_KEY": "my-api-key",
"DEBUG": "true"
})
Testing
Dimwiddle is designed to be easily testable. You can create test containers with mock services:
from dimwiddle.domain.container import Container
from dimwiddle.domain.service_definition import ServiceDefinition
# Create mock services
definitions = {
"mock_service": ServiceDefinition(
cls=MockService,
pos_args=["test value"]
)
}
# Create container with mocks
container = Container(definitions)
Examples
See the examples directory for complete working examples, including:
- Basic service definitions
- Service dependencies
- Factory-based services
- Tagged services
- Configuration injection
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dimwiddle-0.1.2.tar.gz.
File metadata
- Download URL: dimwiddle-0.1.2.tar.gz
- Upload date:
- Size: 21.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b5d8ee7f4ee8048ab785bbe88695bdd598cbe742e25764ab98b5ec8734e0983
|
|
| MD5 |
2ce0b76da3bee7b81b70de180e97b4a1
|
|
| BLAKE2b-256 |
987eecedf6c2eff838ab42e944fef01a844ac896e817565ba3ddab57a1686313
|
File details
Details for the file dimwiddle-0.1.2-py3-none-any.whl.
File metadata
- Download URL: dimwiddle-0.1.2-py3-none-any.whl
- Upload date:
- Size: 13.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4d1469ef8c946cb61d6b37aea3f0393ee5f0311e4592718f90026c35bedfb45d
|
|
| MD5 |
78ac063e8a05127d87795a07dc5d08b4
|
|
| BLAKE2b-256 |
1ab63e6f617c321afab5db4cde7621571d20e9fb6d6880cf0c1c6ceb3d327dc7
|