def create_app(): provider = Provider() provider.bind(Repo, SQLRepo) configure(provider=provider) app = Sanic(__name__) app.blueprint(bp) return app
def create_app(): provider = Provider() provider.bind(Repo, SQLRepo) configure(provider=provider) app = Flask(__name__) app.register_blueprint(bp) return app
def test_configure(): provider = Provider() provider.bind(int, str) configure(provider=provider) assert isinstance(Container.get(), Provider) assert Container.get().bindings[int] == str with raises(InjectException): configure(provider=provider)
def test_unbind(): provider = Provider() with raises(InjectException): provider.unbind(cls=int) provider.bind(int, str) provider.unbind(cls=int) assert provider.bindings == {}
def test_configure_after_clear(): provider = Provider() provider.bind(int, str) configure_after_clear(provider=provider) provider = Provider() provider.bind(str, int) configure_after_clear(provider=provider) assert isinstance(Container.get(), Provider) assert Container.get().bindings[str] == int
from pythondi import Provider, configure, configure_after_clear, inject from .repo import Repo, SQLRepo class Usecase: @inject() def __init__(self, repo: Repo): self.repo = repo if __name__ == '__main__': # There is three ways to binding classes # 1. Init provider without arguments provider = Provider() # Bind one by one provider.bind(Repo, SQLRepo) # Bind all at once provider.bind(classes={Repo: SQLRepo}) # 2. Init provider with arguments provider = Provider(cls=Repo, new_cls=SQLRepo) # 3. Init provider with dictionary provider = Provider(classes={Repo: SQLRepo}) # Inject with configure configure(provider=provider) # Or if you want to fresh inject, use `configure_after_clear` configure_after_clear(provider=provider)
def test_clear_bindings(): provider = Provider() provider.bind(int, str) provider.bind(list, dict) provider.clear_bindings() assert provider.bindings == {}
def test_bind_with_classes(): provider = Provider() provider.bind(classes={int: str}) assert provider.bindings[int] == str provider.bind(int, dict) assert provider.bindings[int] == dict
def test_bind(): provider = Provider() provider.bind(int, str) assert provider.bindings[int] == str
def init_di() -> None: provider = Provider() provider.bind(UserRepo, UserMySQLRepo) configure(provider=provider)