Skip to main content

Simple yet powerful dependency injection framework!

Project description

Snake DI

ci coverage pypi python

Source Code: https://github.com/l1onsun/snake_di

Features:

  • Lightweight - no external dependencies (only typing_extensions)
  • Based on type annotations - no configuration required, less boilerplate
  • Overridable - simple and flexible override system
  • Pytest integration - writing unit tests was never so easy!
  • yeild powered - just yeild your component and then write closing steps
  • async support - able to work with async resources

Install

pip install snake-di

1.0.0 Roadmap

  • Documentation
  • Pytest fixtures support
  • Selective builds - allow build only necessary components
  • More helpful exception messages
  • Scopes - reuse factories for different app configurations

Quick example

file_manager.py

@dataclass
class FileManager:
    opened_file: typing.TextIO
    
    def do_some_file_work(self): 
      ...
    
@dataclass
class Settings:
    file_name: str

provider.py

from snake_di import Provider

provider = Provider()

@provider.include_factory
def provide_settings() -> Settings:
    return Settings(file_name="some.txt")

@provider.include_factory
def provide_file(settings: Settings) -> TextIO:
    with open(settings.file_name) as file:
        yield file

@provider.include_factory
def provide_file_manager(opened_file: TextIO) -> FileManager:
    return FileManager(opened_file)

main.py

def main():
    with provider.build() as container:
        assert container.keys() == {Settings, TextIO, FileManager}
        file_manager = container[FileManager]
        file_manager.do_some_file_work()

        assert container[TextIO].closed is False
    assert container[TextIO].closed is True

Override example - Multiple providers:

@Provider.from_factory
def change_settings() -> Settings:
    return Settings(file_name="other.txt")

@Provider.from_factory
def mock_file() -> TextIO:
    return unittest.mock.Mock()
    
def main():
    assert type(mock_file) is type(change_settings) is Provider
    assert type(provider | mock_file | change_settings) is Provider
    
    with (provider | mock_file | change_settings).build() as container:
        assert container[Settings].file_name = "other.txt"
        assert type(container[FileManager].opened_file) is unittest.mock.Mock

Integration with pytest example:

from snake_di.pytest import pytest_provide

@pytest.fixture
def any_usual_fixture():
    return ...

@pytest_provide(provider | mock_file)
def test_with_mocked_file(
    file_manager: FileManager, 
    mocked_file: typing.TextIO, 
    any_usual_fixture
):
    assert type(mocked_file) is unittest.mock.Mock
    assert file_manager.opened_file == mocked_file

Container.partial_solve() example

def handle_data(file_manager: FileManager, settings: Settings, data: str):
  ...

def main():
    with provider.build() as container:
        handle_data_solved = container.partial_solve(handle_data)
        handle_data_solved(data="data")

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

snake-di-0.0.3.tar.gz (8.0 kB view details)

Uploaded Source

Built Distribution

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

snake_di-0.0.3-py3-none-any.whl (10.0 kB view details)

Uploaded Python 3

File details

Details for the file snake-di-0.0.3.tar.gz.

File metadata

  • Download URL: snake-di-0.0.3.tar.gz
  • Upload date:
  • Size: 8.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.2

File hashes

Hashes for snake-di-0.0.3.tar.gz
Algorithm Hash digest
SHA256 2745d5af1e14f2323722c4e1e734ad20e9f26c41885be76ffefa370892eed924
MD5 ebad63b3025bd0d1ef4be76503d9e985
BLAKE2b-256 d423f05035694bd5156f948c8604218a4223df11f9c6d69b227dcbec7da78072

See more details on using hashes here.

File details

Details for the file snake_di-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: snake_di-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 10.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.28.2

File hashes

Hashes for snake_di-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0ddfed04ce735f8c8aaf846e7f52075b3da5a096161df315cbc32096f9930e54
MD5 35562eebd1ac0aa02c6837f13339e8d4
BLAKE2b-256 c4475abf2d6d4b377f604f447490f78e59affc94e0f908cf5915fabeea89a192

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