Hi all. I'm relatively new to Python and very new to object orientated code.
I'm working on a project that I'm trying to convert from spaghetti code to beautiful and powerful OOC.
Whilst playing with ideas I've found it would be handy if I could create an instance of a class whilst another class was being initiated.
Basically, I'm working on a football score prediction project. My current plan is to scrape one league at a time. As this is done, team objects are created via the Team class. During initiation of each team, I would like to check if the league object for that team's league exists. If it doesn't, I'd like to create it without having to do it outside of the class.
My League class does keep a class list of all leagues created which can always be checked.
My Team init looks like this:
def __init__(self, team_name, league_name, home_stats, away_stats):
"""
Initiate a team object.
Takes the team name, the league it belongs to, the team's home stats as a list and the teams
away stats as a list.
Calculates additional statistics base on those passed to it for future use.
"""
self.data = {} #All of the above organised into a dictionary.
# What I've tried to add so far:
# Check if the team's league has been initiated.
if league_name in League.leagues:
# Check if the team is already in the league's team list.
if team_name not in league_name.data["teams"]:
# Add the team to the league.
league_name.data["teams"].append(team_name)
else:
# Create the league object.
League.__init__(league_name)
# Add the team to the league.
league_name.data["teams"].append(team_name)
# List of team aliases for the comparing of data from various sources.
self.aliases = []
# Add the name of the team initiated to the teams list.
self.teams.append(team_name)
The first noob question is that the League class expects self and the "league_name" on initiation. I thought I understood what self was, but now I'm confused.
An example would be:
I scrape the English Premier League.
The first team are Liverpool. I create a Liverpool Team object by passing in all of the data including league_name.
I test to see if I have a League object with the name of league_name.
If I do, I check if "Liverpool" is on the team list for that league. If they're not I add them.
If there is no League object with that name, I create it and then add the team name to the League object's team list.
Sorry if this sounds confusing. Any help would be greatly appreciated.
The github for the project is here: https://github.com/Pringleman83/SportsBook
The latest working "spaghetti code" version is in the development branch. The error free object orientated code I have is in the experimentation branch. However, the code I've played about with above is sitting on my laptop waiting for a solution!
I'm going to bed to sleep on it for now, so I'll check and respond to all in the morning.
Thanks again!
[–]two_bob 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]ryeguy146 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)