class PipelineContainer(containers.DeclarativeContainer): CONFIG_PATH = pkg_resources.resource_filename('hcpipeline', 'conf/') config = providers.Configuration() config.from_ini(CONFIG_PATH + 'pipeline_confg.ini') @classmethod def getSparkEnv(cls,config): sparkenv = providers.Singleton( SparkEnv, spark_config = config.SPARK_CONFIG().items(), spark_udf = config.SPARK_UDF().items(), app_name = config.SPARK.app_name() ) return sparkenv log_config = providers.Callable( fileConfig, CONFIG_PATH + 'logging_config.ini') log_config() logger = providers.Singleton( logging.Logger, 'hcapLogger') extractor_factory = providers.AbstractFactory(AbstractExtractor) operator_factory = providers.AbstractFactory(AbstractOperator) writer_factory = providers.AbstractFactory(AbstractWriter) metadata_factory = providers.AbstractFactory(AbstractMetadataPrep)
class MatrixContainer(containers.DeclarativeContainer): config = providers.Configuration() matrix_factory = providers.AbstractFactory(ABCMatrixFactory) matrix_sequence_factory = providers.AbstractFactory( ABCMatrixSequenceFactory) shape_sequence_factory = providers.AbstractFactory(ABCShapeSequenceFactory) generate_matrix_sequence = providers.Factory( GenerateMatrixSequence, matrix_sequence_factory=matrix_sequence_factory, shape_sequence_factory=shape_sequence_factory)
def test_repr(self): provider = providers.AbstractFactory(Example) self.assertEqual( repr(provider), '<dependency_injector.providers.' 'AbstractFactory({0}) at {1}>'.format(repr(Example), hex(id(provider))))
class Container(containers.DeclarativeContainer): cache_client_factory = providers.AbstractFactory(AbstractCacheClient) service_factory = providers.Factory( Service, cache=cache_client_factory, )
class MoviesModule(containers.DeclarativeContainer): """IoC container of movies module component providers.""" movie = providers.Factory(movies.models.Movie) finder = providers.AbstractFactory(movies.finders.MovieFinder, movie_model=movie.provider) lister = providers.Factory(movies.listers.MovieLister, movie_finder=finder)
def test_abstract_factory_3_factory_kw_injections(self, providers): """Test factory with 3 keyword argument injections via factories.""" class A(object): pass class B(object): pass class C(object): pass class Test(object): def __init__(self, a, b, c): pass a_factory = providers.Factory(A) b_factory = providers.Factory(B) c_factory = providers.Factory(C) test_factory = providers.AbstractFactory(object) test_factory.override( providers.Factory(Test, a=a_factory, b=b_factory, c=c_factory)) for x in xrange(int(5000000 * self.duration_factor)): test_factory()
def test_override_by_not_factory(): provider = providers.AbstractFactory(object) with raises(errors.Error): provider.override(providers.Callable(object))
def test_call_not_overridden(): provider = providers.AbstractFactory(object) with raises(errors.Error): provider()
def test_call_overridden_by_delegated_factory(): provider = providers.AbstractFactory(object) provider.override(providers.DelegatedFactory(Example)) assert isinstance(provider(), Example)
def test_call_not_overridden(self): provider = providers.AbstractFactory(object) with self.assertRaises(errors.Error): provider()
def test_inheritance(): assert isinstance(providers.AbstractFactory(Example), providers.Factory)
class Container(containers.DeclarativeContainer): payment_service_factory = providers.AbstractFactory( AbstractPayServiceProvider) service_factory = providers.Factory( PaymentService, payment_service_provider=payment_service_factory)
class MovieModuleContainer(containers.DeclarativeContainer): # movie_model = providers.Factory(MovieModel) movie_finder_abs = providers.AbstractFactory(MovieFinder, model_name=MoviesModel) lister = providers.Factory(MovieLister, movie_finder=movie_finder_abs)
"""`AbstractFactory` providers example.""" import cache import dependency_injector.providers as providers # Define abstract cache client factory: cache_client_factory = providers.AbstractFactory(cache.AbstractCacheClient) if __name__ == '__main__': # Override abstract factory with redis client factory: cache_client_factory.override( providers.Factory( cache.RedisCacheClient, host='localhost', port=6379, db=0, ), ) redis_cache = cache_client_factory() print(redis_cache) # <cache.RedisCacheClient object at 0x10975bc50> # Override abstract factory with memcache client factory: cache_client_factory.override( providers.Factory( cache.MemcacheCacheClient, hosts=['10.0.1.1', '10.0.1.2', '10.0.1.3'], port=11211, prefix='my_app', ), ) memcache_cache = cache_client_factory()
def test_provide_not_implemented(): provider = providers.AbstractFactory(Example) with raises(NotImplementedError): provider._provide(tuple(), dict())
def test_inheritance(self): self.assertIsInstance(providers.AbstractFactory(Example), providers.Factory)
def test_repr(): provider = providers.AbstractFactory(Example) assert repr(provider) == ("<dependency_injector.providers." "AbstractFactory({0}) at {1}>".format( repr(Example), hex(id(provider))))
def test_call_overridden_by_factory(self): provider = providers.AbstractFactory(object) provider.override(providers.Factory(Example)) self.assertIsInstance(provider(), Example)
attributes4: Dict[str, Any] = provider4.attributes # Test 5: to check the provided instance interface provider5 = providers.Factory(Animal) provided5: providers.ProvidedInstance = provider5.provided attr_getter5: providers.AttributeGetter = provider5.provided.attr item_getter5: providers.ItemGetter = provider5.provided["item"] method_caller5: providers.MethodCaller = provider5.provided.method.call( 123, arg=324) # 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")
import translators import converters import sys import dependency_injector.providers as providers if len(sys.argv) < 2: print('usage: %s <number>' % sys.argv[0]) sys.exit(1) number = sys.argv[1] # Define abstract translator factory: translator_factory = providers.AbstractFactory(translators.ITranslator) # Define abstract converter factory: converter_factory = providers.AbstractFactory(converters.INumeralConverter) if __name__ == '__main__': # Override abstract factory with CardinalTranslator factory: translator_factory.override( providers.Factory(translators.CardinalTranslator)) translator = translator_factory() converter_factory.override( providers.Factory(converters.ArabicToCardinalConverter, translator=translator)) arabic_to_cardinal = converter_factory() print(arabic_to_cardinal.convert(str(number)))