Skip to content

Commit

Permalink
add providers docs (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
lesnik512 authored May 12, 2024
1 parent 52c7f1f commit 7d38c89
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 102 deletions.
102 changes: 0 additions & 102 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -182,105 +182,3 @@ def test_litestar_di() -> None:
assert response.status_code == HTTP_200_OK, response.text
assert response.text == "async resource"
```

# Docs
## Providers
### 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)
```

### 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)
```
### 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)
```
### 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)
```
### 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)
```
### 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)
```
13 changes: 13 additions & 0 deletions docs/index.md
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
```
16 changes: 16 additions & 0 deletions docs/providers/async-factory.md
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)
```
17 changes: 17 additions & 0 deletions docs/providers/async-resource.md
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)
```
18 changes: 18 additions & 0 deletions docs/providers/factory.md
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)
```
13 changes: 13 additions & 0 deletions docs/providers/list.md
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)
```
17 changes: 17 additions & 0 deletions docs/providers/resource.md
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)
```
17 changes: 17 additions & 0 deletions docs/providers/singleton.md
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)
```

0 comments on commit 7d38c89

Please sign in to comment.