|
4 | 4 | import time |
5 | 5 | from typing import Generic, Protocol, TypeVar, overload |
6 | 6 |
|
7 | | -from pydantic import Field |
8 | | -from pydantic.dataclasses import dataclass |
9 | | - |
10 | 7 | LOG = logging.getLogger(__name__) |
11 | 8 |
|
12 | 9 | T = TypeVar("T") |
|
16 | 13 |
|
17 | 14 |
|
18 | 15 | # TODO: Add batching on bytes as well. |
19 | | -@dataclass |
20 | 16 | class Batcher(Generic[T]): |
21 | 17 | """ |
22 | 18 | A utility for collecting items into batches and flushing them when one or more batch policy conditions are met. |
@@ -51,14 +47,33 @@ class Batcher(Generic[T]): |
51 | 47 | assert batcher.flush() == ["item1", "item2", "item3", "item4"] |
52 | 48 | """ |
53 | 49 |
|
54 | | - max_count: int | None = Field(default=None, description="Maximum number of items", ge=0) |
55 | | - max_window: float | None = Field( |
56 | | - default=None, description="Maximum time window in seconds", ge=0 |
57 | | - ) |
| 50 | + max_count: int | None |
| 51 | + """ |
| 52 | + Maximum number of items, must be None or positive. |
| 53 | + """ |
| 54 | + |
| 55 | + max_window: float | None |
| 56 | + """ |
| 57 | + Maximum time window in seconds, must be None or positive. |
| 58 | + """ |
| 59 | + |
| 60 | + _triggered: bool |
| 61 | + _last_batch_time: float |
| 62 | + _batch: list[T] |
58 | 63 |
|
59 | | - _triggered: bool = Field(default=False, init=False) |
60 | | - _last_batch_time: float = Field(default_factory=time.monotonic, init=False) |
61 | | - _batch: list[T] = Field(default_factory=list, init=False) |
| 64 | + def __init__(self, max_count: int | None = None, max_window: float | None = None): |
| 65 | + """ |
| 66 | + Initialize a new Batcher instance. |
| 67 | +
|
| 68 | + :param max_count: Maximum number of items that be None or positive. |
| 69 | + :param max_window: Maximum time window in seconds that must be None or positive. |
| 70 | + """ |
| 71 | + self.max_count = max_count |
| 72 | + self.max_window = max_window |
| 73 | + |
| 74 | + self._triggered = False |
| 75 | + self._last_batch_time = time.monotonic() |
| 76 | + self._batch = [] |
62 | 77 |
|
63 | 78 | @property |
64 | 79 | def period(self) -> float: |
|
0 commit comments