1

I have been trying to find an effective way to store and call data into my program that is a NBA simulation type of game. I have tried to just save the data in lists that are in separate files and then I just use an import and call the teams list of players when I use it.

Example:

#                                           Player Stats and Skills
#      [player name     / height / strength / speed / jump / blocks / steals / dribble / pass / reb / reb aggr]

uth = [["Trey Burke",         27,        12,     82,    58,      30,      57,       79,    75,   14,      0.10],
       ["Alec Burks",         48,        27,     79,    52,      35,      39,       56,    23,   24,      0.25],
       ["Gordon Hayward",     52,        29,     58,    54,      28,      36,       52,    47,   27,      0.45],
       ["Derrick Favors",     74,        36,     37,    66,      86,      30,       29,    25,   74,      0.75],
       ["Rudy Gobert",        90,        38,     29,    36,      76,      37,       15,    29,   62,      1.00]]

lac = [["Chris Paul",         36,        40,     86,    59,      23,      98,       98,    98,   37,      0.10],
       ["J.J. Redick",        22,        14,     73,    40,      17,      35,       67,    47,   25,      0.20],
       ["Jamal Crawford",     48,        32,     76,    59,      22,      54,       91,    52,   31,      0.30],
       ["Blake Griffin",      88,        88,     75,    57,      76,      56,       43,    43,   92,      0.60],
       ["DeAndre Jordan",     96,        96,     40,    94,      96,      40,       32,    36,   96,      1.00]]

The problem is I am going to be storing quite a bit of data.

I then tried to use Sqlite3 for python but cant figure out how to store sections of data into teams and then save them as lists so I can call the data in my code.

Example of the database I created:

import sqlite3

conn = sqlite3.connect("player_info.db")
c = conn.cursor()

def createTable():
    c.execute(''' CREATE TABLE Player_Info
                  (id INTEGER PRIMARY KEY, First TEXT, Last TEXT,
                   Position TEXT, Height TEXT, Weight TEXT, Years INTEGER, College TEXT)
              ''')
    conn.commit()

def celticsPlayerInfo():
    players = [("Brandon", "Bass", "C/PF", "6'8", "250lbs", 9, "Louisiana State"),
               ("Avery", "Bradley", "SG/PG", "6'2", "180lbs", 4, "Texas"),
               ("Jae", "Crowder", "SF", "6'6", "235lbs", 2, "Marquette"),
               ("Luigi", "Datome", "SF/PF", "6'8", "215lbs", 1, "Italy"),
               ("Jonas", "Jerebko", "PF", "6'10", "231lbs", 4, "Sweden"),
               ("Kelly", "Olynyk", "C/PF", "7'0", "238lbs", 1, "Gonzaga"),
               ("Phil", "Pressey", "PG/SG", "5'11", "175lbs", 1, "Missouri"),
               ("Shavlik", "Randolph", "PF", "6'10", "236lbs", 7, "Duke"),
               ("Marcus", "Smart", "PG/SG", "6'4", "220lbs", 0, "Oklahoma State"),
               ("Jared", "Sullinger", "PF", "6'9", "260lbs", 2, "Ohio State"),
               ("Isaiah", "Thomas", "PG", "5'9", "185lbs", 3, "Washington"),
               ("Evan", "Turner", "SG/SF", "6'7", "220lbs", 4, "Ohio State"),
               ("Gerald", "Wallace", "SF", "6'7", "220lbs", 13, "Alabama"),
               ("James", "Young", "SG/SF", "6'6", "215lbs", 0, "Kentucky"),
               ("Tyler", "Zeller", "C/PF", "7'0", "253lbs", 2, "North Carolina")]

    c.executemany(''' INSERT INTO Player_Info(First, Last, Position, Height, Weight, Years, College)
                  VALUES(?,?,?,?,?,?,?)''', players)
    conn.commit()

def netsPlayerInfo():
    players = [("Alan", "Anderson", "SG/SF", "6'6", "220lbs", 5, "Michigan State"),
               ("Bojan", "Bogdanovic", "SG/SF", "6'8", "216lbs", 0, "Croatia"),
               ("Markel", "Brown", "SG", "6'3", "190lbs", 0, "Oklahoma State"),
               ("Jarrett", "Jack", "PG", "6'3", "200lbs", 9, "Georgia Tech"),
               ("Cory", "Jefferson", "SF", "6'9", "218lbs", 0, "Baylor"),
               ("Joe", "Johnson", "SF/SG", "6'7", "240lbs", 13, "Arkansas"),
               ("Jerome", "Jordan", "C", "7'0", "253lbs", 1, "Tulsa"),
               ("Sergey", "Karasev", "SG/SF", "6'7", "208lbs", 1, "Russia"),
               ("Brook", "Lopez", "C", "7'0", "275lbs", 6, "Stanford"),
               ("Darius", "Morris", "SG", "6'4", "195lbs", 3, "Michigan"),
               ("Mason", "Plumlee", "C/PF", "6'11", "235lbs", 1, "Duke"),
               ("Mirza", "Teletovic", "PF", "6'9", "242lbs", 2, "Bosnia"),
               ("Deron", "Williams", "PG", "6'3", "200lbs", 9, "Illinois"),
               ("Thaddeus", "Young", "SF/PF", "6'8", "221lbs", 7, "Georgia Tech")]

    c.executemany(''' INSERT INTO Player_Info(First, Last, Position, Height, Weight, Years, College)
                  VALUES(?,?,?,?,?,?,?)''', players)
    conn.commit()

And then I would call the data and put it in a list like this:

import sqlite3

conn = sqlite3.connect("player_info.db")
cursor = conn.cursor()

teams = cursor.execute("SELECT * FROM Player_Info")
teams = teams.fetchall()

test = [list(i) for i in teams]

print test

The problem is I cant figure out how to separate those 2 teams into separate lists directly from the database so that I can manipulate the data in my game.

Should I use Sqlite3 or another method to store the data?

  • Have a look at this question here, I would recommend you to use a ORM system to manage your data. http://stackoverflow.com/q/28977921/640916 – djangonaut Mar 12 '15 at 00:11

1 Answers1

0

I'm not familiar with sqlite, but have you considered using objects and dictionaries? Give each Player object a name, height, etc., then put the players into a dictionary representing their team, then put each team into a dictionary representing the league. This should run in Python 2.7 or 3.x.

class Player(object):
    def __init__(self, first, last, position, height_feet, height_inches, weight, jump, origin):
        self.first = first
        self.last = last
        self.position = position
        self.height = height_feet*12 + height_inches
        self.weight = weight
        self.jump = jump
        self.origin = origin

all_teams = {}

all_teams['Celtics'] = {"Brandon Bass":Player('Brandon', 'Bass', 'C/PF', 6, 8, 250, 9, 'Louisiana State'),
"Avery Bradley":Player('Avery', 'Bradley', 'SG/PG', 6, 2, 180, 4, 'Texas')} # and so on

Then, to access Brandon Bass's weight, for example, you would do something like this:

>>> print(all_teams.get('Celtics').get('Brandon Bass').weight)
250

To see how much taller Brandon Bass is compared to Avery Bradley:

>>> print(all_teams.get('Celtics').get('Brandon Bass').height - all_teams.get('Celtics').get('Avery Bradley').height)
6

To get Brandon Bass's height back in feet and inches:

>>> divmod(all_teams.get('Celtics').get('Brandon Bass').height, 12)
(6, 8)

I hope that gives you some ideas.

TigerhawkT3
  • 48,464
  • 6
  • 60
  • 97