JayCo741
JayCo741

Reputation: 603

Python: Iterating Through a List of Strings and Associating Them With Class Objects

I created a class called Tables that has name, ddl, and cols attributes that I use to create tables in MySQL.

I have a list of all Table objects called mylist.

I have a list of all Table object names called tables.

I have a list of stripped filenames for csv's that I will use to load data to the tables called list_files.

If an item in list_files matches a Table object's name in mylist, I want to insert the data from the columns listed in the Table object's cols into the associated MySQL table - in other words, I want to use the list item as the Table object with the matching name.

I can build the INSERT statements once I figure out how to use the string in list_files as the name of an existing Table object.

Here's my code so far:

from pathlib import Path

mylist = []

class Table:

    table_list = mylist

    def __init__(self, name, ddl):
        self.name = name
        self.ddl = ddl
        self.cols = []
        self.skip_id = False  # indicates whether to skip the AUTO_INCREMENT '_id'
        # column on import/update

    def add_to_list(self, table_list):
        table_list.append(self)

    def add_cols(self):
        text = self.ddl.split('`')
        self.cols = text[3::2]

    def skip_auto_inc_id(self):
        if 'AUTO_INCREMENT' in self.ddl:
            self.skip_id = True


# 2 example Table objects
sub_leagues = Table('sub_leagues', '''CREATE TABLE IF NOT EXISTS `sub_leagues` (
  `subl_id` INT PRIMARY KEY,
  `league_id` INT,
  `sub_league_id` INT,
  `name` VARCHAR(50),
  )
   ENGINE=InnoDB DEFAULT CHARSET=latin1''')
sub_leagues.add_to_list(mylist)

divisions = Table('divisions', '''CREATE TABLE IF NOT EXISTS `divisions` (
  `d_id` INT AUTO_INCREMENT PRIMARY KEY,
  `league_id` INT,
  `sub_league_id` INT,
  `division_id` INT,
  `name` VARCHAR(50),
  `gender` INT
  )
   ENGINE=InnoDB DEFAULT CHARSET=latin1''')
divisions.add_to_list(mylist)

tables = []
for table in mylist:
    table.add_cols()
    table.skip_auto_inc_id()
    if table.skip_id:
        table.cols = table.cols[1:]
    tables.append(table.name)

files = ['sub_leagues.csv', 'divisions.csv']

list_files = []

for file in files:
    filename = Path(file).stem
    list_files.append(filename)

#  This is where I'm stumped
for filename in list_files:
    if filename in tables:
        print(filename.cols)

This obviously doesn't work, but I want to treat filename as the Table object with the same name and am not sure the right approach.

Upvotes: 0

Views: 40

Answers (1)

antont
antont

Reputation: 2756

You could make a dictionary to map the filenames to the Table objects:

tables = {}
for table in mylist:
    table.add_cols()
    table.skip_auto_inc_id()
    if table.skip_id:
        table.cols = table.cols[1:]
    tables[table.name] = table

for filename in list_files:
    if filename in tables:
        table = tables[filename]

Upvotes: 1

Related Questions