reduce variables
This commit is contained in:
parent
6082809715
commit
a527afeb80
15
main.py
15
main.py
@ -26,8 +26,7 @@ class DBConn:
|
||||
self.con = None
|
||||
self.counter = 0
|
||||
self.ts = ''
|
||||
self.table_col_map = {}
|
||||
self.child_tables = {}
|
||||
self.table_mapping = {}
|
||||
self.init_db()
|
||||
|
||||
def init_db(self):
|
||||
@ -44,7 +43,6 @@ class DBConn:
|
||||
if args.debug:
|
||||
print(f"create sql = {create_tbl_sql}")
|
||||
self.cur.execute(create_tbl_sql)
|
||||
self.table_col_map[tbl_name] = cols
|
||||
|
||||
def write_to_database(self, tbl, cols):
|
||||
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,
|
||||
# we shall accommodate by adding it to the database
|
||||
tbl_cols = self.table_col_map[tbl]
|
||||
tbl_cols = self.table_mapping[tbl]
|
||||
not_found = []
|
||||
for col in 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")
|
||||
|
||||
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})"
|
||||
self.cur.execute(sql, values)
|
||||
@ -119,7 +117,7 @@ class DBConn:
|
||||
if child_header not in attributes:
|
||||
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}
|
||||
|
||||
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)
|
||||
|
||||
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)
|
||||
|
||||
def parse_json(self):
|
||||
@ -194,8 +192,7 @@ class DBConn:
|
||||
"children": children
|
||||
}, indent=2)
|
||||
)
|
||||
self.child_tables[top_level] = attributes
|
||||
|
||||
self.table_mapping[top_level] = attributes
|
||||
self.make_tables()
|
||||
|
||||
children_names = children.keys()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user