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

1"""Module for defining a presenter.""" 

2 

3from abc import ABC, abstractmethod 

4from dataclasses import dataclass 

5from typing import AsyncIterator, Self 

6 

7 

8@dataclass(frozen=True, kw_only=True, slots=True) 

9class IterableResult[T]: 

10 """A dataclass used to represent a result with multiple entities.""" 

11 

12 count: int 

13 offset: int = 0 

14 limit: int = 0 

15 iterator: AsyncIterator[T] 

16 

17 

18class Presenter[T](ABC): 

19 """An interface for a presenter. 

20 

21 A presenter is used to transform an entity into another object that can be used 

22 in a view. 

23 

24 An example: convert to a JSON:API resource for returning the entity in a restful 

25 API. 

26 """ 

27 

28 @abstractmethod 

29 def present(self, use_case_result: T) -> Self: 

30 """Present the entity. 

31 

32 This method is responsible for converting the entity. 

33 """ 

34 

35 

36class EntityPresenter[T](Presenter[T]): 

37 """A simple presenter that stores the entity it receives.""" 

38 

39 def __init__(self): 

40 super().__init__() 

41 self._entity: T | None = None 

42 

43 @property 

44 def entity(self) -> T | None: 

45 """Return the entity.""" 

46 return self._entity 

47 

48 def present(self, entity: T) -> Self: 

49 """It just stores the received entity.""" 

50 self._entity = entity 

51 return self 

52 

53 

54class AsyncPresenter[T](ABC): 

55 """An interface for an async presenter. 

56 

57 A presenter is used to transform an entity into another object that can be used 

58 in a view. 

59 

60 An example: convert to a JSON:API resource for returning the entity in a restful 

61 API. 

62 """ 

63 

64 @abstractmethod 

65 async def present(self, use_case_result: T) -> Self: 

66 """Present the entity. 

67 

68 This method is responsible for converting the entity. 

69 """ 

70 

71 

72class CountIterableAsyncPresenter[T](AsyncPresenter[IterableResult[T]]): 

73 """A presenter that counts the number of entries in the use case result. 

74 

75 This presenter can be used in tests. 

76 """ 

77 

78 def __init__(self): 

79 super().__init__() 

80 self._count = 0 

81 

82 @property 

83 def count(self) -> int: 

84 """Return count.""" 

85 return self._count 

86 

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