💡 add comment to describe template formatter usage

This commit is contained in:
Mix
2021-08-27 19:03:46 +08:00
parent 7cfdc2dd37
commit 1c73a9adfa
2 changed files with 28 additions and 0 deletions

View File

@ -11,12 +11,18 @@ TM = TypeVar("TM", bound="Message")
class MessageFormatter(Formatter, Generic[TM]):
"""消息模板格式化实现类"""
def __init__(self, factory: Type[TM], template: str) -> None:
self.template = template
self.factory = factory
def format(self, *args: Any, **kwargs: Any) -> TM:
"""
:说明:
根据模板和参数生成消息对象
"""
msg = self.vformat(self.template, args, kwargs)
return msg if isinstance(msg, self.factory) else self.factory(msg)