-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.py
63 lines (51 loc) · 2.27 KB
/
keys.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class keys:
def __init__(self, All_dfs):
self.All_dfs = All_dfs
self.pk = {}
self.fk = {}
self.ref_in = {}
self.fkandrefinSetter()
# find each table's primary key
def pk_finder(self,name, df):
for columnNumber in range(len(df.columns)):
col = df.iloc[:,columnNumber]
#if(col.size == col.drop_duplicates().size):
if(col.nunique() == col.size):
self.pk[name] = df.columns[columnNumber]
break
# find each table's foreign key
def fk_finder(self,tableName, df):
for columnNumber in range(len(df.columns)):
col_name = df.columns[columnNumber]
col = df[col_name]
for pkTable in self.pk: #loops on the key j = table names
if pkTable != tableName:
primary_key_column = self.All_dfs[pkTable][(self.pk[pkTable])]
if primary_key_column.dtype == col.dtype:
isReferenced = col.isin(primary_key_column) #what is the status
if isReferenced[isReferenced == False].size == 0:
self.fk[tableName][col_name] = pkTable
self.ref_in[pkTable].add(tableName)
break
elif (isinstance(col[0],list) and type(primary_key_column[0]) == type(col[0][0])):
isReferenced = col.explode().reset_index(drop=True).isin(primary_key_column)
if isReferenced[isReferenced == False].size == 0:
self.fk[tableName][col_name] = pkTable
self.ref_in[pkTable].add(tableName)
break
#Initialize fk and refin
def fkandrefinSetter(self):
table_name = list(self.All_dfs.keys())
for table_name in self.All_dfs:
self.fk[table_name] = {}
self.ref_in[table_name] = set()
# Primary key Getter
def primaryKeyGetter(self):
for t in self.All_dfs:
self.pk_finder(t,self.All_dfs[t])
return self.pk
# Foreign key Getter
def foreignKeyGetter(self):
for t in self.All_dfs:
self.fk_finder(t,self.All_dfs[t])
return self.fk