from pathlib import Path from typing import Union, List, Optional class GenericText: def __init__(self, content: Union[List[str], Path]): if isinstance(content, List): self._content: Optional[List[str]] = content self._path = None else: self._content: Optional[List[str]] = None self._path = content @property def text(self) -> List[str]: if self._content is None: try: with open(self._path, 'r') as file: self._content = file.readlines() except FileNotFoundError: raise NotImplementedError except: raise NotImplementedError return self._content @text.setter def text(self, content: Union[List[str], Path]) -> None: if isinstance(content, List): self._content = content self._path = None else: self._content = None self._path = content def format(self, **kwargs) -> str: for line in self._content: try: line = line.format(**kwargs) except ValueError: raise NotImplementedError return '\n'.join(self._content) def __add__(self, other): if isinstance(other, GenericText): return GenericText(self.text + other.text) else: return GenericText(self.text + other) def __iadd__(self, other): self.text = self + other