Cordial saludo tengo una duda en python.
Tengo un objeto el cual utiliza el siguiente método para retornar un Json de sus atributos:
def get_json(self):
return self.__dict__
pero tengo un problema cuando trato de añadir un objeto dentro de esta clase... el objeto deja de trabajar:
Este es el objeto que yo quiero imprimir en Json:
# app/models/person.py
from app.models.statistics import Statistics
class Person:
def __init__(self, id):
self._id = id
self._first_name = ""
self._middle_name = ""
self._last_name = ""
self._second_last_name = ""
self._sex = ""
self._age = 0
self._location = ""
self._plausible_death = ""
self.statistics = Statistics()
@property
def id(self):
return self._id
@property
def first_name(self):
return self._first_name
@first_name.setter
def first_name(self, value):
self._first_name = value
@property
def middle_name(self):
return self._middle_name
@middle_name.setter
def middle_name(self, value):
self._middle_name = value
@property
def last_name(self):
return self._last_name
@last_name.setter
def last_name(self, value):
self._last_name = value
@property
def second_last_name(self):
return self._second_last_name
@second_last_name.setter
def second_last_name(self, value):
self._second_last_name = value
@property
def sex(self):
return self._sex
@sex.setter
def sex(self, value):
self._sex = value
@property
def age(self):
return self._age
@age.setter
def age(self, value):
self._age = value
@property
def location(self):
return self._location
@location.setter
def location(self, value):
self._location = value
@property
def plausible_death(self):
return self._plausible_death
@location.setter
def plausible_death(self, value):
self._plausible_death = value
def get_json(self):
return self.__dict__
def __repr__(self):
attributes = ', '.join(f'{key}={value}' for key, value in self.__dict__.items())
return f'Person({attributes})'
Y este es el objeto que yo quiero añadir:
# app/models/statistics.py
class Statistics:
def __init__(self):
self.energy = 100 # 100 is full, 0 is exhausted
self.hunger = 100 # 100 is full, 0 is starving
self.intelligence = 50
self.strength = 50
self.mental_health = 70 # 100 is optimal, 0 is critical
self.physical_health = 70 # 100 is optimal, 0 is critical
self.social_skills = 50
self.job_performance = 50
def update_stat(self, stat_name, value):
if hasattr(self, stat_name):
new_value = getattr(self, stat_name) + value
setattr(self, stat_name, max(0, min(new_value, 100))) # Keeps stats within 0 to 100 range
else:
raise ValueError(f"Statistic '{stat_name}' does not exist.")
def get_json(self):
return self.__dict__
def __repr__(self):
return str(self.get_json())
Alguien podria decirme que puedo hacer en el método get_json de person para poder retornar un Json interno de todo lo que hay en statistics.py
El problema consiste en que viene por parte de una respuesta de una API: Este es el link del controlador:
https://github.com/felipedelosh/API-LifeInstructions/blob/main/app/controllers/player_controller.py
Lo que estoy tratando de hacer es imprimir el player que es un hijo de Persona:
# app/models/player.py
from app.models.person import Person
class Player(Person):
def __init__(self, id):
super().__init__(id)
self._father = None
self._mother = None
@property
def father(self):
return self._father
@father.setter
def father(self, value):
self._father = value
@property
def mother(self):
return self._mother
@mother.setter
def mother(self, value):
self._mother = value
def get_json(self):
return self.__dict__
def __repr__(self):
attributes = ', '.join(f'{key}={value}' for key, value in self.__dict__.items())
return f'Player({attributes})'