I'm new to python web development (and OOP in general). I have a view in my data base that consists of 7 "parts", and I wanted to divide them into their own classes (though the data is "joined" in my database). The parts/classes would be TrainerData, Pokemon1, Pokemon2, etc...
Here's a more simple version of what i'm going for. I know the issue and error, I just don't have the knowledge to solve this one. Here's what I have so far.
import psycopg2
class Trainer:
def __init__(self):
self.trainerdata = self.TrainerData()
class TrainerData:
def __init__(self, trainername, battle, winnings):
self.trainername = trainername
self.battle = battle
self.winnings = winnings
@classmethod
def load_from_db_by_tname(cls, tname):
with psycopg2._connect(user='postgres', password='samfurdissamrea', database='ODS', host='localhost') as connection:
with connection.cursor() as cursor:
cursor.execute('SELECT trainer_name, battle, winnings from public.poke_data_web WHERE lower(trainer_name) = lower(%s)', (tname,))
trainer_data = cursor.fetchone()
return cls(trainername=trainer_data[0], battle=trainer_data[1], winnings=trainer_data[2])
a = Trainer()
print(a.trainerdata.load_from_db_by_tname('angus'))
The error returned:
Traceback (most recent call last):
File "C:/Users/ageeray/Documents/Python Scripts/PokeTrainerWeb/trainer.py", line 22, in <module>
a = Trainer()
File "C:/Users/ageeray/Documents/Python Scripts/PokeTrainerWeb/trainer.py", line 5, in __init__
self.trainerdata = self.TrainerData()
TypeError: __init__() missing 3 required positional arguments: 'trainername', 'battle', and 'winnings'
I can get this to work if I don't have the TrainerData as a nested class, but otherwise, I've hit a wall. All input is appreciated; thank you for your time.
a = Trainer(Trainer.TrainerData.load_from_db_by_tname('angus')), but I find it rather strange that you use nested classes anyway, why is this useful here?self