From e8041c6f43daf5676a04f8acd1b42b1ef53c123b Mon Sep 17 00:00:00 2001 From: ivan Date: Sat, 27 Jul 2024 00:15:15 +0300 Subject: [PATCH] add docs for some providers --- docs/providers/callable.md | 3 +++ docs/providers/coroutine.md | 26 ++++++++++++++++++++++++++ docs/providers/object.md | 21 +++++++++++++++++++++ docs/providers/partial_callable.md | 3 +++ docs/providers/provided_instance.md | 3 +++ docs/providers/singleton.md | 14 +++++++++----- docs/providers/transient.md | 28 ++++++++++++++++++++++++++++ 7 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 docs/providers/callable.md create mode 100644 docs/providers/coroutine.md create mode 100644 docs/providers/object.md create mode 100644 docs/providers/partial_callable.md create mode 100644 docs/providers/provided_instance.md create mode 100644 docs/providers/transient.md diff --git a/docs/providers/callable.md b/docs/providers/callable.md new file mode 100644 index 0000000..dac63cf --- /dev/null +++ b/docs/providers/callable.md @@ -0,0 +1,3 @@ +# Callable + +soon... diff --git a/docs/providers/coroutine.md b/docs/providers/coroutine.md new file mode 100644 index 0000000..2be42c7 --- /dev/null +++ b/docs/providers/coroutine.md @@ -0,0 +1,26 @@ +# Coroutine + +**Coroutine** provider creates a coroutine. + +## Example + +```python3 +import asyncio + +from injection import DeclarativeContainer, providers + + +async def coroutine(arg1, arg2): + await asyncio.sleep(0.1) + return arg1, arg2 + + +class DIContainer(DeclarativeContainer): + provider = providers.Coroutine(coroutine, arg1=1, arg2=2) + + +if __name__ == "__main__": + arg1, arg2 = asyncio.run(DIContainer.provider()) + assert (arg1, arg2) == (1, 2) + +``` diff --git a/docs/providers/object.md b/docs/providers/object.md new file mode 100644 index 0000000..13d546a --- /dev/null +++ b/docs/providers/object.md @@ -0,0 +1,21 @@ +# Object + +**Object** provider returns an object “as is”. + +## Example + +```python3 +from injection import DeclarativeContainer, providers + + +class Container(DeclarativeContainer): + provider1 = providers.Object("string") + provider2 = providers.Object(13425) + + +if __name__ == "__main__": + assert Container.provider1() == "string" + assert Container.provider2() == 13425 + + +``` diff --git a/docs/providers/partial_callable.md b/docs/providers/partial_callable.md new file mode 100644 index 0000000..ca2bc07 --- /dev/null +++ b/docs/providers/partial_callable.md @@ -0,0 +1,3 @@ +# Partial callable + +soon... diff --git a/docs/providers/provided_instance.md b/docs/providers/provided_instance.md new file mode 100644 index 0000000..9965220 --- /dev/null +++ b/docs/providers/provided_instance.md @@ -0,0 +1,3 @@ +# Provided instance + +soon... diff --git a/docs/providers/singleton.md b/docs/providers/singleton.md index e80ad0f..118c8cf 100644 --- a/docs/providers/singleton.md +++ b/docs/providers/singleton.md @@ -1,26 +1,30 @@ # Singleton -Singleton provider make caching for your object. -sfsfsfdf +**Singleton** provider creates and returns a new object on the first call +and caches it, and on subsequent calls it returns the cached object. ## Example ```python3 +from dataclasses import dataclass + from injection import DeclarativeContainer, providers +@dataclass class SingletonClass: field: int class DIContainer(DeclarativeContainer): - singleton_provider = providers.Singleton(SingletonClass, field=15) + provider = providers.Singleton(SingletonClass, field=15) if __name__ == "__main__": - instance1 = DIContainer.singleton_provider() - instance2 = DIContainer.singleton_provider() + instance1 = DIContainer.provider() + instance2 = DIContainer.provider() assert instance1 is instance2 assert instance1.field == 15 + ``` diff --git a/docs/providers/transient.md b/docs/providers/transient.md new file mode 100644 index 0000000..ed463be --- /dev/null +++ b/docs/providers/transient.md @@ -0,0 +1,28 @@ +# Transient + +**Transient** provider creates and returns a new object for each call. + +## Example + +```python3 +from dataclasses import dataclass + +from injection import DeclarativeContainer, providers + + +@dataclass +class SomeClass: + field: str + + +class DIContainer(DeclarativeContainer): + provider = providers.Transient(SomeClass, field="str_value") + + +if __name__ == "__main__": + instance1 = DIContainer.provider() + instance2 = DIContainer.provider() + + assert instance1 is not instance2 + +```