-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Convert TranslatedObject to dataclass with improved type sa…
…fety
- Loading branch information
1 parent
910cdca
commit c1f2a54
Showing
2 changed files
with
58 additions
and
44 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,41 @@ | ||
"""Translation result object implementation.""" | ||
import json | ||
from typing import Any, Dict, List, Union | ||
from dataclasses import dataclass | ||
from typing import Any, Dict, List, Optional | ||
|
||
|
||
class TranslatedObject(dict): | ||
"""A dictionary subclass that holds translation results with attribute access.""" | ||
@dataclass | ||
class TranslatedObject: | ||
"""A dataclass that holds translation results.""" | ||
raw: Dict[str, Any] | ||
orig: str | ||
text: str | ||
orig_raw: List[str] | ||
text_raw: List[str] | ||
lang: str | ||
|
||
def __getattr__(self, attr: str) -> Union['TranslatedObject', List['TranslatedObject'], Any]: | ||
"""Get attributes allowing dot notation access. | ||
def __str__(self) -> str: | ||
"""Get string representation with the translated text. | ||
Args: | ||
attr: The attribute name to access | ||
Returns: | ||
The attribute value, wrapped in TranslatedObject if it's a dict | ||
str: The translated text | ||
""" | ||
if isinstance(self, list): | ||
return [TranslatedObject(elem) for elem in self] | ||
|
||
value = dict.get(self, attr) | ||
if isinstance(value, dict): | ||
return TranslatedObject(value) | ||
return value | ||
return self.text | ||
|
||
def __str__(self) -> str: | ||
"""Get string representation, truncating long values. | ||
@classmethod | ||
def from_raw_response(cls, raw: Dict[str, Any]) -> "TranslatedObject": | ||
"""Create TranslatedObject from raw API response. | ||
Args: | ||
raw: Raw response dictionary from the translation API | ||
Returns: | ||
str: JSON formatted string with truncated values | ||
TranslatedObject: Parsed translation result | ||
""" | ||
return json.dumps( | ||
{k: v if len(str(v)) < 200 else "..." for k, v in self.items()}, | ||
indent=4 | ||
return cls( | ||
raw=raw, | ||
orig=" ".join(s["orig"] for s in raw["sentences"] if "orig" in s), | ||
text=" ".join(s["trans"] for s in raw["sentences"] if "trans" in s), | ||
orig_raw=[s["orig"] for s in raw["sentences"] if "orig" in s], | ||
text_raw=[s["trans"] for s in raw["sentences"] if "trans" in s], | ||
lang=raw["src"] | ||
) | ||
|
||
# Maintain dict-like attribute access | ||
__setattr__ = dict.__setitem__ # type: ignore | ||
__delattr__ = dict.__delitem__ # type: ignore |