1
I am learning to integrate Python and Mongodb and, because of that, I proposed a challenge: create a small program to register football players.
My program has two modules, info_player and info_team. My intention is to run the program interactively (python -i). The first module receives information about the players and the second, information about the team, in addition to talking to the bank.
I structured the bank as follows, database is called championship, Collections are the teams and Documents are the players. That is, there is a one-to-Many relationship between teams and players.
My doubts are: I need to convert the generated objects to Json (I thought to create a method to_json
)? How to save in the bank and make the desired queries?
info_player:
class Player:
def __init__(self, name, age, nationality="brazilian", team):
"""
initializating Jogador class
"""
self.personal(name, age, country)
self.professional(team)
def personal(self, name, age, nationality, dominancy, height):
"""
personal data about players
"""
self.name = name
self.age = age
self.nationality = nationality
self.height = height
self.dominancy = dominancy # righty, lefty or ambidextrous
def profissional(self, position, number, team, primary):
"""
professional data about players
"""
self.position = position
self.number = number
self.team = team
self.primary = False # is he a regular member of a team?
def to_Json():
info_team:
from pymongo import MongoClient
from info_player import Player
class TeamDB:
def __init___(self, nome, fundacao, federacao):
self.name = name
self.foundationData = foundationData
self.federation = federation
def initializeDB():
client = MongoClient('localhost', 27017)
global base
base = client.league
def toMongo():
"""
receive a player object and save it
"""
def playersByPosition():
"""
query players by position
"""
def lineup():
"""
receive a team and return its starting line-up, players with primary = true
"""