-
Notifications
You must be signed in to change notification settings - Fork 13
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
Showing
8 changed files
with
111 additions
and
102 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
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,2 +1,15 @@ | ||
```{include} ../README.md | ||
``` | ||
|
||
```{eval-rst} | ||
.. toctree:: | ||
:maxdepth: 1 | ||
:caption: Providers | ||
providers/resource | ||
providers/async-resource | ||
providers/singleton | ||
providers/factory | ||
providers/async-factory | ||
providers/list | ||
``` |
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,16 @@ | ||
### AsyncFactory | ||
- Initialized on every call, as `Factory`. | ||
- Async function is required. | ||
```python | ||
import datetime | ||
|
||
from that_depends import BaseContainer, providers | ||
|
||
|
||
async def async_factory() -> datetime.datetime: | ||
return datetime.datetime.now(tz=datetime.timezone.utc) | ||
|
||
|
||
class DIContainer(BaseContainer): | ||
async_factory = providers.Factory(async_factory) | ||
``` |
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,17 @@ | ||
### AsyncResource | ||
- Same as `Resource` but async generator function is required. | ||
```python | ||
import typing | ||
|
||
from that_depends import BaseContainer, providers | ||
|
||
|
||
async def create_async_resource() -> typing.AsyncIterator[str]: | ||
# resource initialization | ||
yield "async resource" | ||
# resource teardown | ||
|
||
|
||
class DIContainer(BaseContainer): | ||
async_resource = providers.AsyncResource(create_async_resource) | ||
``` |
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,18 @@ | ||
### Factory | ||
- Initialized on every call. | ||
- Class or simple function is allowed. | ||
```python | ||
import dataclasses | ||
|
||
from that_depends import BaseContainer, providers | ||
|
||
|
||
@dataclasses.dataclass(kw_only=True, slots=True) | ||
class IndependentFactory: | ||
dep1: str | ||
dep2: int | ||
|
||
|
||
class DIContainer(BaseContainer): | ||
independent_factory = providers.Factory(IndependentFactory, dep1="text", dep2=123) | ||
``` |
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,13 @@ | ||
### List | ||
- List provider contains other providers. | ||
- Resolves into list of dependencies. | ||
|
||
```python | ||
import random | ||
from that_depends import BaseContainer, providers | ||
|
||
|
||
class DIContainer(BaseContainer): | ||
random_number = providers.Factory(random.random) | ||
numbers_sequence = providers.List(random_number, random_number) | ||
``` |
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,17 @@ | ||
### Resource | ||
- Resource initialized only once and have teardown logic. | ||
- Generator function is required. | ||
```python | ||
import typing | ||
|
||
from that_depends import BaseContainer, providers | ||
|
||
|
||
def create_sync_resource() -> typing.Iterator[str]: | ||
# resource initialization | ||
yield "sync resource" | ||
# resource teardown | ||
|
||
class DIContainer(BaseContainer): | ||
sync_resource = providers.Resource(create_sync_resource) | ||
``` |
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,17 @@ | ||
### Singleton | ||
- Initialized only once, but without teardown logic. | ||
- Class or simple function is allowed. | ||
```python | ||
import dataclasses | ||
|
||
from that_depends import BaseContainer, providers | ||
|
||
|
||
@dataclasses.dataclass(kw_only=True, slots=True) | ||
class SingletonFactory: | ||
dep1: bool | ||
|
||
|
||
class DIContainer(BaseContainer): | ||
singleton = providers.Singleton(SingletonFactory, dep1=True) | ||
``` |