class LightcurveContainer(containers.DeclarativeContainer): # wiring_config = containers.WiringConfiguration( # modules=["api.resources.light_curve"] # ) psql_db = providers.Dependency(instance_of=SQLConnection) mongo_db = providers.Dependency(instance_of=MongoConnection) detection_repository_factory = providers.FactoryAggregate( { "ztf": providers.Factory(PSQLDetectionRepository, db=psql_db), "atlas": providers.Factory(MongoDetectionRepository, db=mongo_db), } ) non_detection_repository_factory = providers.FactoryAggregate( { "ztf": providers.Factory(PSQLNonDetectionRepository, db=psql_db), "atlas": providers.Factory( MongoNonDetectionRepository, db=mongo_db ), } ) lightcurve_service = providers.Factory( LightcurveService, detection_repository_factory=detection_repository_factory, non_detection_repository_factory=non_detection_repository_factory, ) get_detections_command = providers.Factory( GetDetection, service=lightcurve_service ) get_non_detections_command = providers.Factory( GetNonDetection, service=lightcurve_service ) get_lightcurve_command = providers.Factory( GetLightcurve, service=lightcurve_service )
def factory_aggregate(factory_type, factory_a, factory_b): if factory_type == "empty": return providers.FactoryAggregate() elif factory_type == "non-string-keys": return providers.FactoryAggregate({ ExampleA: factory_a, ExampleB: factory_b, }) elif factory_type == "default": return providers.FactoryAggregate( example_a=factory_a, example_b=factory_b, ) else: raise ValueError("Unknown factory type \"{0}\"".format(factory_type))
class Container(containers.DeclarativeContainer): game_factory = providers.FactoryAggregate( chess=providers.Factory(Chess), checkers=providers.Factory(Checkers), ludo=providers.Factory(Ludo), )
def test_traverse(): factory1 = providers.Factory(dict) factory2 = providers.Factory(list) provider = providers.FactoryAggregate(factory1=factory1, factory2=factory2) all_providers = list(provider.traverse()) assert len(all_providers) == 2 assert factory1 in all_providers assert factory2 in all_providers
def test_traverse(self): factory1 = providers.Factory(dict) factory2 = providers.Factory(list) provider = providers.FactoryAggregate(factory1=factory1, factory2=factory2) all_providers = list(provider.traverse()) self.assertEqual(len(all_providers), 2) self.assertIn(factory1, all_providers) self.assertIn(factory2, all_providers)
class QueryBusContainer(containers.DeclarativeContainer): items_repository = providers.Singleton(AuctionItemsRepository) query_handler_factory = providers.FactoryAggregate( GetItemsQuery=providers.Factory(GetItemsQueryHandler, items_repository=items_repository)) query_bus_factory = providers.Factory( QueryBus, query_handler_factory=providers.DelegatedFactory( query_handler_factory))
class OverriddenQueryBusContainer(QueryBusContainer): items_repository = providers.Singleton(MockAuctionItemsRepository) query_handler_factory = providers.FactoryAggregate( GetItemsQuery=providers.Factory(GetItemsQueryHandler, items_repository=items_repository)) query_bus_factory = providers.Factory( QueryBus, query_handler_factory=providers.DelegatedFactory( query_handler_factory))
class CommandBusContainer(containers.DeclarativeContainer): items_repository = providers.Singleton(AuctionItemsRepository) command_handler_factory = providers.FactoryAggregate( AddItemCommand=providers.Factory(AddItemCommandHandler, items_repository=items_repository)) command_bus_factory = providers.Factory( CommandBus, command_handler_factory=providers.DelegatedFactory( command_handler_factory))
class AppContainer(containers.DeclarativeContainer): config = providers.Configuration() os_runner = providers.Singleton(os.CmdRunner) identity_key_factory = providers.FactoryAggregate( lastpass=providers.Factory(lpass.LastPassIdentityKey), default=providers.Factory(entities.IdentityKey), ) ssh_connection = providers.Factory( use_cases.EstablishSshConnection, os_runner=os_runner )
def test_init_optional_factories(self): provider = providers.FactoryAggregate() provider.set_factories( example_a=self.example_a_factory, example_b=self.example_b_factory, ) self.assertEqual( provider.factories, { 'example_a': self.example_a_factory, 'example_b': self.example_b_factory, }, ) self.assertIsInstance(provider('example_a'), self.ExampleA) self.assertIsInstance(provider('example_b'), self.ExampleB)
async def test_async_mode(): object1 = object() object2 = object() async def _get_object1(): return object1 def _get_object2(): return object2 provider = providers.FactoryAggregate( object1=providers.Factory(_get_object1), object2=providers.Factory(_get_object2), ) assert provider.is_async_mode_undefined() is True created_object1 = await provider("object1") assert created_object1 is object1 assert provider.is_async_mode_enabled() is True created_object2 = await provider("object2") assert created_object2 is object2
def test_async_mode(self): object1 = object() object2 = object() async def _get_object1(): return object1 def _get_object2(): return object2 provider = providers.FactoryAggregate( object1=providers.Factory(_get_object1), object2=providers.Factory(_get_object2), ) self.assertTrue(provider.is_async_mode_undefined()) created_object1 = self._run(provider('object1')) self.assertIs(created_object1, object1) self.assertTrue(provider.is_async_mode_enabled()) created_object2 = self._run(provider('object2')) self.assertIs(created_object2, object2)
def setUp(self): self.example_a_factory = providers.Factory(self.ExampleA) self.example_b_factory = providers.Factory(self.ExampleB) self.factory_aggregate = providers.FactoryAggregate( example_a=self.example_a_factory, example_b=self.example_b_factory)
import sys import dependency_injector.providers as providers from games import Chess, Checkers, Ludo game_factory = providers.FactoryAggregate(chess=providers.Factory(Chess), checkers=providers.Factory(Checkers), ludo=providers.Factory(Ludo)) if __name__ == '__main__': game_type = sys.argv[1].lower() player1 = sys.argv[2].capitalize() player2 = sys.argv[3].capitalize() selected_game = game_factory(game_type, player1, player2) selected_game.play() # $ python example.py chess John Jane # John and Jane are playing chess # # $ python example.py checkers John Jane # John and Jane are playing checkers # # $ python example.py ludo John Jane # John and Jane are playing ludo
def test_set_provides_returns_self(self): provider = providers.FactoryAggregate() self.assertIs(provider.set_factories(example_a=self.example_a_factory), provider)
def test_init_with_not_a_factory(): with raises(errors.Error): providers.FactoryAggregate( example_a=providers.Factory(ExampleA), example_b=object(), )
# Test 6: to check the DelegatedFactory provider6 = providers.DelegatedFactory(Cat) animal6: Animal = provider6(1, 2, 3, b='1', c=2, e=0.0) # Test 7: to check the AbstractFactory provider7 = providers.AbstractFactory(Animal) provider7.override(providers.Factory(Cat)) animal7: Animal = provider7(1, 2, 3, b='1', c=2, e=0.0) # Test 8: to check the FactoryDelegate __init__ provider8 = providers.FactoryDelegate(providers.Factory(object)) # Test 9: to check FactoryAggregate provider provider9 = providers.FactoryAggregate( a=providers.Factory(object), b=providers.Factory(object), ) factory_a_9: providers.Factory = provider9.a factory_b_9: providers.Factory = provider9.b val9: Any = provider9('a') # Test 10: to check the explicit typing factory10: providers.Provider[Animal] = providers.Factory(Cat) animal10: Animal = factory10() # Test 11: to check the return type with await provider11 = providers.Factory(Cat) async def _async11() -> None: animal1: Animal = await provider11(1, 2, 3, b='1', c=2,
class HCPLocalHandler(LocalHandler): def __init__(self, patient_directory, label='hcp'): super(HCPLocalHandler, self).__init__(patient_directory, label) def load(self, filters=[]): return self._make_patient(self.patient_directory, filters) class ADNILocalHandler(LocalHandler): def __init__(self, patient_directory, label='adni'): super(ADNILocalHandler, self).__init__(patient_directory, label) def load(self, filters=[]): return self._make_patient(self.patient_directory, filters) class RosenLocalHandler(LocalHandler): def __init__(self, patient_directory, label='rosen'): super(RosenLocalHandler, self).__init__(patient_directory, label) def load(self, filters=[]): return self._make_patient(self.patient_directory, filters) Handler = providers.FactoryAggregate(local=providers.Factory(LocalHandler))
class Container(containers.DeclarativeContainer): handler_factory = providers.FactoryAggregate({ CommandA: providers.Factory(HandlerA), CommandB: providers.Factory(HandlerB), })
class Container(containers.DeclarativeContainer): """ A class whose responsibility is instantiating, and resolving dependencies. """ config = providers.Configuration() arguments = providers.Object( CliParser.parse_args() ) inventory_data = providers.Object( get_inventory_data() ) config_file = arguments().config_file Configuration(config_file).validate_config() app_config = providers.Singleton( AppConfig, config_file=config_file ) database_connection_info = providers.Factory( DatabaseConnectionInfo, user=config.database.user, password=config.database.password, host=config.database.host, port=config.database.port, database=config.database.name ) database_connector = providers.Factory( MySqlConnector, connection_info=database_connection_info ) database_inventory_repository = providers.Singleton( DatabaseInventoryRepository, database_connector=database_connector ) setup_database_action = providers.Factory( SetupDatabaseAction, database_connector=database_connector, database_connection_info=database_connection_info, inventory_data=inventory_data ) file_inventory_repository = providers.Singleton( FileInventoryRepository ) inventory_repository = providers.Selector( inventory_repository_selector, file=file_inventory_repository, database=database_inventory_repository ) http_utils = providers.Singleton( HttpUtils, func_get_headers=lambda: request.headers or dict() ) behavior_factory = providers.FactoryAggregate( throw=providers.Factory(ThrowException), malloc=providers.Factory(Malloc), compute=providers.Factory(Compute), invalid_query=providers.Factory( InvalidQuery, database_inventory_repository=database_inventory_repository ) ) behavior_repository = providers.Singleton( Repository, app_id=app_config.provided.get_app_id.call(), behavior_factory_func=behavior_factory ) inventory_handler = providers.Singleton( InventoryHandler, app_config=app_config, datastore=inventory_repository, http_utils=http_utils, behavior_repository=behavior_repository ) message_handler = providers.Singleton( MessageHandler, app_config=app_config, http_utils=http_utils, behavior_repository=behavior_repository ) behaviors_handler = providers.Singleton( BehaviorsHandler, app_config=app_config, behavior_repository=behavior_repository ) index_handler = providers.Singleton( IndexHandler, app_config=app_config, behavior_repository=behavior_repository )
# Test 6: to check the DelegatedFactory provider6 = providers.DelegatedFactory(Cat) animal6: Animal = provider6(1, 2, 3, b="1", c=2, e=0.0) # Test 7: to check the AbstractFactory provider7 = providers.AbstractFactory(Animal) provider7.override(providers.Factory(Cat)) animal7: Animal = provider7(1, 2, 3, b="1", c=2, e=0.0) # Test 8: to check the FactoryDelegate __init__ provider8 = providers.FactoryDelegate(providers.Factory(object)) # Test 9: to check FactoryAggregate provider provider9: providers.FactoryAggregate[str] = providers.FactoryAggregate( a=providers.Factory(str, "str1"), b=providers.Factory(str, "str2"), ) factory_a_9: providers.Factory[str] = provider9.a factory_b_9: providers.Factory[str] = provider9.b val9: str = provider9("a") provider9_set_non_string_keys: providers.FactoryAggregate[ str] = providers.FactoryAggregate() provider9_set_non_string_keys.set_factories( {Cat: providers.Factory(str, "str")}) factory_set_non_string_9: providers.Factory[ str] = provider9_set_non_string_keys.factories[Cat] provider9_new_non_string_keys: providers.FactoryAggregate[ str] = providers.FactoryAggregate({Cat: providers.Factory(str, "str")}, ) factory_new_non_string_9: providers.Factory[
def test_init_with_not_a_factory(self): with self.assertRaises(errors.Error): providers.FactoryAggregate(example_a=providers.Factory( self.ExampleA), example_b=object())