dependency-injection

Inject different implementation based on yaml configuration

Inject different implementation based on yaml configuration Question: Using python package dependency injector, I need to instantiate/inject a different implementation of an interface based on a yaml configuration file. class SomeInterface(abc.ABC): @abc.abstractmethod def some_method(self): pass class Impl1(SomeInterface): def some_method(self): # e.g. file-based implementation class Impl2(SomeInterface): def some_method(self): # e.g. service-based implementation I have different yaml …

Total answers: 1

Unable to inject dependencies to FastAPI endpoints

Unable to inject dependencies to FastAPI endpoints Question: I have configured a dependencies.py where I’m injecting a set of dependencies to different services by using python’s binder.bind(my_config). The goal is being able to easily inject those services to each endpoint of my API. The problem arises when I pass that service as an argument to …

Total answers: 2

FastAPI: can I use Depends() for parameters in a POST, too?

FastAPI: can I use Depends() for parameters in a POST, too? Question: Overview I have created a class-based dependency, similar to what is in the amazing FastAPI tutorial. Problem It works, except that the parameters in the dependency (the Depends() portion) are passed as query parameters, meaning that they are part of the URI/URL. I …

Total answers: 2

Pytest use same fixture twice in one function

Pytest use same fixture twice in one function Question: For my web server, I have a login fixture that create a user and returns the headers needed to send requests. For a certain test, I need two users. How can I use the same fixture twice in one function? from test.fixtures import login class TestGroups(object): …

Total answers: 6

What is a Pythonic way for Dependency Injection?

What is a Pythonic way for Dependency Injection? Question: Introduction For Java, Dependency Injection works as pure OOP, i.e. you provide an interface to be implemented and in your framework code accept an instance of a class that implements the defined interface. Now for Python, you are able to do the same way, but I …

Total answers: 9

Python mock Patch os.environ and return value

Python mock Patch os.environ and return value Question: Unit testing conn() using mock: app.py import mysql.connector import os, urlparse def conn(): if "DATABASE_URL" in os.environ: url = urlparse(os.environ["DATABASE_URL"]) g.db = mysql.connector.connect( user=url.username, password=url.password, host=url.hostname, database=url.path[1:], ) else: return "Error" test.py def test_conn(self): with patch(app.mysql.connector) as mock_mysql: with patch(app.os.environ) as mock_environ: con() mock_mysql.connect.assert_callled_with("credentials") Error: Assertion mock_mysql.connect.assert_called_with …

Total answers: 7

Why is IoC / DI not common in Python?

Why is IoC / DI not common in Python? Question: In Java IoC / DI is a very common practice which is extensively used in web applications, nearly all available frameworks and Java EE. On the other hand, there are also lots of big Python web applications, but beside of Zope (which I’ve heard should …

Total answers: 18

Python Dependency Injection Framework

Python Dependency Injection Framework Question: Is there a framework equivalent to Guice (http://code.google.com/p/google-guice) for Python? Asked By: Mark Roddy || Source Answers: I haven’t used it, but the Spring Python framework is based on Spring and implements Inversion of Control. There also appears to be a Guice in Python project: snake-guice Answered By: Matthew Trevor …

Total answers: 19