コード例 #1
0
    def test_repr(self):
        provider = providers.DelegatedCallable(_example)

        self.assertEqual(
            repr(provider), '<dependency_injector.providers.'
            'DelegatedCallable({0}) at {1}>'.format(repr(_example),
                                                    hex(id(provider))))
コード例 #2
0
    def test_call_overridden_by_delegated_callable(self):
        def _abstract_example():
            pass

        provider = providers.AbstractCallable(_abstract_example)
        provider.override(providers.DelegatedCallable(_example))

        self.assertTrue(provider(1, 2, 3, 4), (1, 2, 3, 4))
def test_call_overridden_by_delegated_callable():
    def _abstract_example():
        pass

    provider = providers.AbstractCallable(_abstract_example)
    provider.override(providers.DelegatedCallable(example))

    assert provider(1, 2, 3, 4) == (1, 2, 3, 4)
コード例 #4
0
def example(example_object, queue_object):
    """Put provided object in the provided queue."""
    queue_object.put(example_object)


# Create thread-local singleton provider for some object (main thread):
thread_local_object = providers.ThreadLocalSingleton(object)

# Create singleton provider for thread-safe queue:
queue_factory = providers.ThreadSafeSingleton(queue.Queue)

# Create callable provider for example(), inject dependencies:
example = providers.DelegatedCallable(
    example,
    example_object=thread_local_object,
    queue_object=queue_factory,
)

# Create factory for threads that are targeted to execute example():
thread_factory = providers.Factory(threading.Thread, target=example)

if __name__ == '__main__':
    # Create 10 threads for concurrent execution of example():
    threads = []
    for thread_number in range(10):
        threads.append(
            thread_factory(name='Thread{0}'.format(thread_number)), )

    # Start execution of all created threads:
    for thread in threads:
コード例 #5
0
 def test_inheritance(self):
     """Test inheritance."""
     self.assertIsInstance(providers.DelegatedCallable(len),
                           providers.Callable)
コード例 #6
0
 def test_is_delegated_provider(self):
     """Test is_delegated_provider."""
     provider = providers.DelegatedCallable(len)
     self.assertIs(provider.provide_injection(), provider)
コード例 #7
0
 def test_is_provider(self):
     """Test is_provider."""
     self.assertTrue(utils.is_provider(providers.DelegatedCallable(len)))
コード例 #8
0
 def test_is_delegated_provider(self):
     provider = providers.DelegatedCallable(_example)
     self.assertTrue(providers.is_delegated(provider))
コード例 #9
0
 def test_is_provider(self):
     self.assertTrue(
         providers.is_provider(providers.DelegatedCallable(_example)))
コード例 #10
0
 def test_inheritance(self):
     self.assertIsInstance(providers.DelegatedCallable(_example),
                           providers.Callable)
コード例 #11
0
        def __init__(self, config, main, common_ioc_factory, message_bus_ioc_factory, combinator_ioc_factory, lexicomb_ioc_factory, state_machine_ioc_factory, lexical_state_machine_ioc_factory, interpreter_state_machine_ioc_factory):
            self.__instance = containers.DynamicContainer()
            logging_provider = IocUtil.create_basic_log_adapter(providers, "bbpyp.lexicomb_engine")
            common_ioc = common_ioc_factory(config=config, source_format_rules={
                ":=": {"format": [(r"\s*{}\s*", r"{} ")]},
                "+": {"format": [(r"\s*{}\s*", r" {} ")]},
                "-": {"format": [(r"\s*{}\s*", r" {} ")]},
                "*": {"format": [(r"\s*{}\s*", r" {} ")]},
                "/": {"format": [(r"\s*{}\s*", r" {} ")]},
                ";": {"format": [(r"\s*{}\s*", r"{}" + linesep)]},
                "{": {"format": [(r"\s*{}\s*", r"{}" + linesep)], "indent_delta": IndentDelta.INCREASE},
                "}": {"format": [(rf"\s*{linesep}{{{{1}}}}(\s*){{}}\s*", linesep + r"\1{}" + linesep)], "indent_delta": IndentDelta.DECREASE},
                "return": {"format": [(rf"\s*{linesep}{{{{1}}}}(\s*){{}}\s*", linesep + r"\1{} ")]}
            }).build()

            message_bus_ioc = message_bus_ioc_factory(
                config=config,
                common_ioc=common_ioc).build()
            combinator_ioc = combinator_ioc_factory(
                common_ioc=common_ioc).build()
            lexicomb_ioc = lexicomb_ioc_factory(
                config=config,
                common_ioc=common_ioc,
                combinator_ioc=combinator_ioc).build()
            state_machine_ioc = state_machine_ioc_factory()
            lexical_state_machine_ioc = lexical_state_machine_ioc_factory(
                config=config,
                common_ioc=common_ioc,
                state_machine_ioc=state_machine_ioc).build()
            interpreter_state_machine_ioc = interpreter_state_machine_ioc_factory(
                config=config,
                common_ioc=common_ioc,
                state_machine_ioc=state_machine_ioc).build()

            lexical_actions_provider = providers.Singleton(
                LexicalActions,
                logger=logging_provider,
                lexer=lexicomb_ioc.lexer_provider,
                queue_service=common_ioc.queue_service_provider,
                notification_service=common_ioc.notification_service_provider)

            interpreter_actions_provider = providers.Singleton(
                InterpreterActions,
                logger=logging_provider,
                parser_combinator=lexicomb_ioc.parser_provider,
                queue_service=common_ioc.queue_service_provider,
                notification_service=common_ioc.notification_service_provider)

            lexical_state_machine_ioc.lexical_actions_provider.provided_by(
                lexical_actions_provider)

            interpreter_state_machine_ioc.interpreter_actions_provider.provided_by(
                interpreter_actions_provider)

            lexicomb_pub_sub_client_provider = providers.DelegatedFactory(
                LexicombPubSubClient,
                logger=logging_provider,
                queue_service=common_ioc.queue_service_provider,
                notification_service=common_ioc.notification_service_provider,
                message_factory=message_bus_ioc.message_factory_provider,
                file_stream_service=common_ioc.file_stream_service_provider,
                context_service=common_ioc.context_service_provider,
                async_service=common_ioc.async_service_provider)

            create_client = providers.DelegatedCallable(
                self.__create_lexicomb_client,
                logger=logging_provider,
                pub_sub=message_bus_ioc.pub_sub_provider,
                message_pipe_line_builder=message_bus_ioc.message_pipe_line_builder_provider,
                pub_sub_client_factory=lexicomb_pub_sub_client_provider,
                lexical_state_machine=lexical_state_machine_ioc.lexical_state_machine_provider,
                interpreter_state_machine=interpreter_state_machine_ioc.interpreter_state_machine_provider)

            self.__instance.lexicomb_ioc = lexicomb_ioc

            self.__instance.main = providers.Callable(
                main, create_client=create_client, pub_sub=message_bus_ioc.pub_sub_provider, async_service=common_ioc.async_service_provider, metric_service=common_ioc.metric_service_provider)
コード例 #12
0
# Test 4: to check the .args & .kwargs attributes
provider4 = providers.Callable(Animal)
args4: Tuple[Any] = provider4.args
kwargs4: Dict[str, Any] = provider4.kwargs

# Test 5: to check the provided instance interface
provider5 = providers.Callable(Animal)
provided5: providers.ProvidedInstance = provider5.provided
attr_getter5: providers.AttributeGetter = provider5.provided.attr
item_getter5: providers.ItemGetter = provider5.provided['item']
method_caller: providers.MethodCaller = provider5.provided.method.call(123,
                                                                       arg=324)

# Test 6: to check the DelegatedCallable
provider6 = providers.DelegatedCallable(Cat)
animal6: Animal = provider6(1, 2, 3, b='1', c=2, e=0.0)

# Test 7: to check the AbstractCallable
provider7 = providers.AbstractCallable(Animal)
provider7.override(providers.Callable(Cat))
animal7: Animal = provider7(1, 2, 3, b='1', c=2, e=0.0)

# Test 8: to check the CallableDelegate __init__
provider8 = providers.CallableDelegate(providers.Callable(lambda: None))

# Test 9: to check the return type with await
provider9 = providers.Callable(Cat)


async def _async9() -> None:
def test_inheritance():
    assert isinstance(providers.DelegatedCallable(example), providers.Callable)
def test_repr():
    provider = providers.DelegatedCallable(example)
    assert repr(provider) == ("<dependency_injector.providers."
                              "DelegatedCallable({0}) at {1}>".format(
                                  repr(example), hex(id(provider))))
def test_is_delegated_provider():
    provider = providers.DelegatedCallable(example)
    assert providers.is_delegated(provider) is True
def test_is_provider():
    assert providers.is_provider(providers.DelegatedCallable(example)) is True