Normalizing data in Python parent table and children table input for database, fastest way

Viewed 17

Still learning Python, so not really a knowledgebase of the million pip programs and native language construct, but learning. Wrote this class for normalizing data from feilds and data of a file 'company_tickers_exchange.json' from the SEC(securities exchange commission) it takes about a 6 seconds to run. If anyone has any suggestions, to speed it up, it would be greatly appreciated. Thanks

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep  7 16:36:55 2022

@author: yourusernamehere
"""

class Normalize_data():
    def __init__(self, fields, data):
        self.fields = fields
        self.data = data
        self.datalist = [dict(zip(fields, i)) for i in data]
        self.parent_table = []
        self.children = {key: [] for key in fields}
        self.set_children()
        self.set_parent_rows()

    def set_unique_child(self, index, value):
        if value not in self.children[index]:
            self.children[index].append(value)

    def get_child(self, index, value):
        if value in index:
            return value

    def get_child_index(self, child, value):
        if value in self.children[child]:
            return self.children[child].index(value)

    def sort_children(self):
        for p in self.children.values():
            p.sort()

    def set_children(self):
        for i in self.datalist:
            for k, v in i.items():
                self.set_unique_child(k, v)
        self.sort_children()

    def set_parent_row(self, value):
        p = [self.get_child_index(k, v) for k, v in value.items()]
        self.parent_table.append(p)
        
    def set_parent_rows(self):
        for i in self.datalist:
            self.set_parent_row(i)
    
    def get_value_of_parent_row(self, values):
        return [self.children[self.fields[values.index(i)]][i] for i in values]
            
    
    def get_values_of_parent_rows(self):        
        return [self.get_value_of_parent_row(i) for i in self.parent_table]
0 Answers
Related