models.pokemon
Módulo pokemon.py
[PT-BR] Define a estrutura de dados imutável para representar um Pokémon.
Utiliza @dataclass
com frozen=True
para garantir imutabilidade.
[EN] Defines an immutable data structure to represent a Pokémon.
Uses @dataclass
with frozen=True
to ensure immutability.
A classe oferece um método to_dict()
que transforma a instância
em um dicionário formatado para exportação em CSV ou exibição.
The class provides a to_dict()
method that converts the instance
into a dictionary formatted for CSV export or display.
Uso / Usage: p = Pokemon(number="025", name="Pikachu", types=["Electric"], image="...", extra_attributes={}) d = p.to_dict()
1""" 2Módulo pokemon.py 3=================== 4 5[PT-BR] Define a estrutura de dados imutável para representar um Pokémon. 6Utiliza ``@dataclass`` com ``frozen=True`` para garantir imutabilidade. 7 8[EN] Defines an immutable data structure to represent a Pokémon. 9Uses ``@dataclass`` with ``frozen=True`` to ensure immutability. 10 11A classe oferece um método ``to_dict()`` que transforma a instância 12em um dicionário formatado para exportação em CSV ou exibição. 13 14The class provides a ``to_dict()`` method that converts the instance 15into a dictionary formatted for CSV export or display. 16 17Uso / Usage: 18 p = Pokemon(number="025", name="Pikachu", types=["Electric"], image="...", extra_attributes={}) 19 d = p.to_dict() 20""" 21from dataclasses import dataclass, field 22from typing import List, Optional, Dict 23 24@dataclass(frozen=True) 25class Pokemon: 26 """ 27 [PT-BR] Representa um Pokémon com atributos fixos e extras. Imutável. 28 [EN] Represents a Pokémon with fixed and extra attributes. Immutable. 29 """ 30 number: str 31 name: str 32 types: List[str] 33 image: Optional[str] = None 34 extra_attributes: Dict[str, str] = field(default_factory=dict) 35 36 def to_dict(self, type_sep: str = "/") -> dict[str, str]: 37 """ 38 [PT-BR] Converte o Pokémon em dicionário para exportação. 39 [EN] Converts the Pokémon into a dictionary for export. 40 41 Parâmetros: 42 type_sep (str): Separador dos tipos no campo "Tipo". 43 44 Retorna: 45 dict[str, str]: Dicionário formatado. 46 """ 47 data: dict[str, str] = { 48 "Nº": self.number, 49 "Nome": self.name, 50 "Tipo": type_sep.join(self.types), 51 "Imagem": self.image or "" 52 } 53 data.update(self.extra_attributes) 54 return data 55 56 def __str__(self) -> str: 57 """ 58 [PT-BR] Representação legível do objeto. 59 [EN] Human-readable object representation. 60 """ 61 tipo = "/".join(self.types) 62 return f"Pokemon[{self.number}] {self.name} ({tipo})"
25@dataclass(frozen=True) 26class Pokemon: 27 """ 28 [PT-BR] Representa um Pokémon com atributos fixos e extras. Imutável. 29 [EN] Represents a Pokémon with fixed and extra attributes. Immutable. 30 """ 31 number: str 32 name: str 33 types: List[str] 34 image: Optional[str] = None 35 extra_attributes: Dict[str, str] = field(default_factory=dict) 36 37 def to_dict(self, type_sep: str = "/") -> dict[str, str]: 38 """ 39 [PT-BR] Converte o Pokémon em dicionário para exportação. 40 [EN] Converts the Pokémon into a dictionary for export. 41 42 Parâmetros: 43 type_sep (str): Separador dos tipos no campo "Tipo". 44 45 Retorna: 46 dict[str, str]: Dicionário formatado. 47 """ 48 data: dict[str, str] = { 49 "Nº": self.number, 50 "Nome": self.name, 51 "Tipo": type_sep.join(self.types), 52 "Imagem": self.image or "" 53 } 54 data.update(self.extra_attributes) 55 return data 56 57 def __str__(self) -> str: 58 """ 59 [PT-BR] Representação legível do objeto. 60 [EN] Human-readable object representation. 61 """ 62 tipo = "/".join(self.types) 63 return f"Pokemon[{self.number}] {self.name} ({tipo})"
[PT-BR] Representa um Pokémon com atributos fixos e extras. Imutável. [EN] Represents a Pokémon with fixed and extra attributes. Immutable.
37 def to_dict(self, type_sep: str = "/") -> dict[str, str]: 38 """ 39 [PT-BR] Converte o Pokémon em dicionário para exportação. 40 [EN] Converts the Pokémon into a dictionary for export. 41 42 Parâmetros: 43 type_sep (str): Separador dos tipos no campo "Tipo". 44 45 Retorna: 46 dict[str, str]: Dicionário formatado. 47 """ 48 data: dict[str, str] = { 49 "Nº": self.number, 50 "Nome": self.name, 51 "Tipo": type_sep.join(self.types), 52 "Imagem": self.image or "" 53 } 54 data.update(self.extra_attributes) 55 return data
[PT-BR] Converte o Pokémon em dicionário para exportação. [EN] Converts the Pokémon into a dictionary for export.
Parâmetros: type_sep (str): Separador dos tipos no campo "Tipo".
Retorna: dict[str, str]: Dicionário formatado.