-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ivan
committed
Jul 26, 2024
1 parent
c74f3a2
commit e8041c6
Showing
7 changed files
with
93 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Callable | ||
|
||
soon... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Partial callable | ||
|
||
soon... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Provided instance | ||
|
||
soon... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
``` |