spam-detector/src/transport/train_dto.py
2024-11-01 23:35:45 +03:00

24 lines
588 B
Python

import json
from typing import TypeVar, Type
T = TypeVar('T', bound='TrainDto')
class TrainDto:
def __init__(self, is_spam: bool, text: str):
self.is_spam = is_spam
self.text = text
def to_json(self) -> str:
return json.dumps({
'is_spam': self.is_spam,
'text': self.text
})
@classmethod
def from_json(cls: Type[T], s: str) -> T:
j = json.loads(s)
if not 'is_spam' in j or not 'text' in j:
raise Exception("Wrong format")
return TrainDto(is_spam=j['is_spam'], text=j['text'])