Search code examples
pythonpython-dataclasses

How I can implement *asdict() or *asdict() using @dataclass


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))

Solution

  • 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)