|
1 | 1 | from __future__ import annotations
|
2 | 2 |
|
3 | 3 | from enum import Enum
|
4 |
| -from typing import Dict, List, Optional, Union |
| 4 | +from typing import Any, Dict, Iterator, List, Optional, Union |
5 | 5 |
|
6 | 6 | from camel_converter.pydantic_base import CamelBase
|
| 7 | +from pydantic import ConfigDict, field_validator |
| 8 | + |
| 9 | + |
| 10 | +class FieldDistribution: |
| 11 | + __dict: Dict |
| 12 | + |
| 13 | + def __init__(self, dist: Dict[str, int]) -> None: |
| 14 | + self.__dict = dist |
| 15 | + for key in dist: |
| 16 | + setattr(self, key, dist[key]) |
| 17 | + |
| 18 | + def __getattr__(self, attr: str) -> str: |
| 19 | + if attr in self.__dict.keys(): |
| 20 | + return attr |
| 21 | + raise AttributeError(f"{self.__class__.__name__} object has no attribute {attr}") |
| 22 | + |
| 23 | + def __iter__(self) -> Iterator: |
| 24 | + return iter(self.__dict__.items()) |
7 | 25 |
|
8 | 26 |
|
9 | 27 | class IndexStats(CamelBase):
|
| 28 | + model_config = ConfigDict(arbitrary_types_allowed=True) |
| 29 | + |
10 | 30 | number_of_documents: int
|
11 | 31 | is_indexing: bool
|
12 |
| - field_distribution: Dict[str, int] |
| 32 | + field_distribution: FieldDistribution |
| 33 | + |
| 34 | + @field_validator("field_distribution", mode="before") |
| 35 | + @classmethod |
| 36 | + def build_field_distribution(cls, v: Any) -> FieldDistribution: |
| 37 | + if not isinstance(v, dict): |
| 38 | + raise TypeError('"field_distribution" in IndexStats must be a dict') |
| 39 | + |
| 40 | + return FieldDistribution(v) |
13 | 41 |
|
14 | 42 |
|
15 | 43 | class Faceting(CamelBase):
|
|
0 commit comments