reduce variables

This commit is contained in:
gowthaman.b 2023-06-19 17:35:40 +05:30
parent 6082809715
commit a527afeb80

15
main.py
View File

@ -26,8 +26,7 @@ class DBConn:
self.con = None self.con = None
self.counter = 0 self.counter = 0
self.ts = '' self.ts = ''
self.table_col_map = {} self.table_mapping = {}
self.child_tables = {}
self.init_db() self.init_db()
def init_db(self): def init_db(self):
@ -44,7 +43,6 @@ class DBConn:
if args.debug: if args.debug:
print(f"create sql = {create_tbl_sql}") print(f"create sql = {create_tbl_sql}")
self.cur.execute(create_tbl_sql) self.cur.execute(create_tbl_sql)
self.table_col_map[tbl_name] = cols
def write_to_database(self, tbl, cols): def write_to_database(self, tbl, cols):
keys = cols.keys() # OrderedDict keys = cols.keys() # OrderedDict
@ -60,7 +58,7 @@ class DBConn:
# there might be a scenario that new cols might appear at a later stage, if so, # there might be a scenario that new cols might appear at a later stage, if so,
# we shall accommodate by adding it to the database # we shall accommodate by adding it to the database
tbl_cols = self.table_col_map[tbl] tbl_cols = self.table_mapping[tbl]
not_found = [] not_found = []
for col in cols: for col in cols:
if col not in tbl_cols: if col not in tbl_cols:
@ -71,7 +69,7 @@ class DBConn:
self.cur.execute(f"alter table \"{tbl}\" add column \"{new_col}\" text") self.cur.execute(f"alter table \"{tbl}\" add column \"{new_col}\" text")
print(f"added {not_found} cols to {tbl}") print(f"added {not_found} cols to {tbl}")
self.table_col_map[tbl] = cols self.table_mapping[tbl] = cols
sql = f"insert into \"{tbl}\" ({col_names}) values({value_placeholders})" sql = f"insert into \"{tbl}\" ({col_names}) values({value_placeholders})"
self.cur.execute(sql, values) self.cur.execute(sql, values)
@ -119,7 +117,7 @@ class DBConn:
if child_header not in attributes: if child_header not in attributes:
attributes.append(child_header) attributes.append(child_header)
self.child_tables[f"{prev_step}{current_level}"] = attributes self.table_mapping[f"{prev_step}{current_level}"] = attributes
return {"flat_attributes": flat_attributes, "children": children, "attributes": attributes} return {"flat_attributes": flat_attributes, "children": children, "attributes": attributes}
def extract_child_value(self, item, k, ext, prev_step, join_col_value): def extract_child_value(self, item, k, ext, prev_step, join_col_value):
@ -139,7 +137,7 @@ class DBConn:
dbConn.write_to_database(k_, child_value) dbConn.write_to_database(k_, child_value)
def make_tables(self): def make_tables(self):
for (mhKey, mhVal) in self.child_tables.items(): for (mhKey, mhVal) in self.table_mapping.items():
self.make_table(mhKey, mhVal) self.make_table(mhKey, mhVal)
def parse_json(self): def parse_json(self):
@ -194,8 +192,7 @@ class DBConn:
"children": children "children": children
}, indent=2) }, indent=2)
) )
self.child_tables[top_level] = attributes self.table_mapping[top_level] = attributes
self.make_tables() self.make_tables()
children_names = children.keys() children_names = children.keys()