Coverage for packages/kwai-core/src/kwai_core/domain/presenter.py: 100%
30 statements
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
« prev ^ index » next coverage.py v7.11.0, created at 2024-01-01 00:00 +0000
1"""Module for defining a presenter."""
3from abc import ABC, abstractmethod
4from dataclasses import dataclass
5from typing import AsyncIterator, Self
8@dataclass(frozen=True, kw_only=True, slots=True)
9class IterableResult[T]:
10 """A dataclass used to represent a result with multiple entities."""
12 count: int
13 offset: int = 0
14 limit: int = 0
15 iterator: AsyncIterator[T]
18class Presenter[T](ABC):
19 """An interface for a presenter.
21 A presenter is used to transform an entity into another object that can be used
22 in a view.
24 An example: convert to a JSON:API resource for returning the entity in a restful
25 API.
26 """
28 @abstractmethod
29 def present(self, use_case_result: T) -> Self:
30 """Present the entity.
32 This method is responsible for converting the entity.
33 """
36class EntityPresenter[T](Presenter[T]):
37 """A simple presenter that stores the entity it receives."""
39 def __init__(self):
40 super().__init__()
41 self._entity: T | None = None
43 @property
44 def entity(self) -> T | None:
45 """Return the entity."""
46 return self._entity
48 def present(self, entity: T) -> Self:
49 """It just stores the received entity."""
50 self._entity = entity
51 return self
54class AsyncPresenter[T](ABC):
55 """An interface for an async presenter.
57 A presenter is used to transform an entity into another object that can be used
58 in a view.
60 An example: convert to a JSON:API resource for returning the entity in a restful
61 API.
62 """
64 @abstractmethod
65 async def present(self, use_case_result: T) -> Self:
66 """Present the entity.
68 This method is responsible for converting the entity.
69 """
72class CountIterableAsyncPresenter[T](AsyncPresenter[IterableResult[T]]):
73 """A presenter that counts the number of entries in the use case result.
75 This presenter can be used in tests.
76 """
78 def __init__(self):
79 super().__init__()
80 self._count = 0
82 @property
83 def count(self) -> int:
84 """Return count."""
85 return self._count
87 async def present(self, use_case_result: IterableResult[T]) -> Self:
88 """Process the result of the use case."""
89 async for _ in use_case_result.iterator:
90 self._count += 1
91 return self