-2

I can't implement asdisct. I have read information but I still have errors when using asdict.

@dataclass
class InfoMessage():
    """Информационное сообщение o тренировке."""
    def __init__(self,
                 training_type: str,  # Тип тренировки.
                 duration: float,  # Продолжительность тренировки.
                 distance: float,  # Продолжительность тренировки.
                 speed: float,  # Дистанция, выполняемая на тренировке.
                 calories: float) -> None:  # Калории, сжигаемые на тренировке.
        self.training_type = training_type    # имя класса тренировки.
        self.duration = duration    # длительность тренировки в часах.
        # дистанция в километрах, которую преодолел пользователь
        # за время тренировки.
        self.distance = distance
        # средняя скорость, с которой двигался пользователь.
        self.speed = speed
        # количество килокалорий, которое израсходовал пользователь
        # за время тренировки.
        self.calories = calories
        self.message = ('Тип тренировки: {self.training_type}_1d;'
                        'Длительность: {self.duration:.3f}_1d ч.; '
                        'Дистанция: {self.distance:.3f}_1d км; '
                        'Ср. скорость: {self.speed:.3f}_1d км/ч; '
                        'Потрачено ккал: {self.calories:.3f}_1d.')

    def get_message(self) -> str:
        """функция для отображения окончательного сообщения."""
        return self.message.format(*asdict(self))
sahasrara62
  • 10,069
  • 3
  • 29
  • 44
  • 6
    You just wrote a regular class and slapped `@dataclass` on it. That's not how `@dataclass` works. You need to go read the dataclasses documentation. – user2357112 Apr 13 '22 at 17:28
  • This is mistake: (venv) PS C:\Dev\hw_python_oop> & c:/Dev/hw_python_oop/venv/Scripts/python.exe c:/Dev/hw_python_oop/homework.py Traceback (most recent call last): File "c:\Dev\hw_python_oop\homework.py", line 228, in main(training) File "c:\Dev\hw_python_oop\homework.py", line 212, in main info_print = InfoMessage.get_message(info) File "c:\Dev\hw_python_oop\homework.py", line 62, in get_message return self.message.format(*asdict(self)) KeyError: 'self' (venv) PS C:\Dev\hw_python_oop> – Andres_Parra Apr 13 '22 at 17:28
  • `dict((field.name, getattr(self, field.name)) for field in fields(self))` – sahasrara62 Apr 13 '22 at 17:28

1 Answers1

0

I don't think you are making use of the @dataclass decorator here, but you also don't need to to fix your problem. If you pass self to your string template it should format nicely.

    def get_message(self) -> str:
        return self.message.format(self=self)

However, I think you are on the right track with a dataclass as this could make your code a lot simpler:

@dataclass
class InfoMessage:
    training_type: str  
    duration: float 
    distance: float 
    speed: float
    calories: float
    
    _MESSAGE_TEMPLATE = (
        'Тип тренировки: {self.training_type}_1d;'
        'Длительность: {self.duration:.3f}_1d ч.; '
        'Дистанция: {self.distance:.3f}_1d км; '
        'Ср. скорость: {self.speed:.3f}_1d км/ч; '
        'Потрачено ккал: {self.calories:.3f}_1d.'
    )
        
    def get_message(self) -> str:
        return self._MESSAGE_TEMPLATE.format(self=self)

If you only use this for debugging purposes, then you might find the natural stringification from dataclass is good enough:

print(InfoMessage(training_type="training type", duration=1, distance=2, speed=3.0, calories=4.0))

# InfoMessage(training_type='training type', duration=1, distance=2, speed=3.0, calories=4.0)
Jon Betts
  • 3,053
  • 1
  • 14
  • 12
  • Thanks @Jon Betts. In this example (code) How can I implement asdict? I read information about that but I dont get it. – Andres_Parra Apr 17 '22 at 19:12
  • Don't use `{self.`} in your template and pass in the values raw `.format(**asdict(self))`, but I'm not sure why you would as it would just be slower? – Jon Betts Apr 19 '22 at 13:25