-
Notifications
You must be signed in to change notification settings - Fork 1
Sourcery refactored main branch #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,16 +19,15 @@ | |
| class exceltoDBtable: | ||
| # Available for sql server and mysql now | ||
| def __init__(self,filePath,hostORip=False,usrID =False,pwd=False,database=False,save2tableName=False): | ||
| if not any([hostORip,database,usrID,pwd]): | ||
| if not any([hostORip, database, usrID, pwd]): | ||
| raise Exception("Partially inputs, please check your inputs...") | ||
| else: | ||
| self.filePath = filePath | ||
| self.hostORip = hostORip | ||
| self.database=database | ||
| self.usrID = usrID | ||
| self.pwd = pwd | ||
| self.save2tableName = save2tableName | ||
|
|
||
| self.filePath = filePath | ||
| self.hostORip = hostORip | ||
| self.database=database | ||
| self.usrID = usrID | ||
| self.pwd = pwd | ||
| self.save2tableName = save2tableName | ||
|
|
||
| self.dbType = ["sqlserver"] | ||
| self.readData() | ||
| self.connect2DB() | ||
|
|
@@ -46,27 +45,29 @@ def connect2DB(self) -> "Connect to Database Server": | |
| for i in sqlserverDriver: | ||
| driveString = i.replace(" ","+").replace("{","").replace("}","") | ||
| # print(driveString) | ||
|
|
||
|
|
||
| try: | ||
| self.engine = create_engine("mssql+pyodbc://%s:%s@%s/%s?driver=%s?"%(self.usrID,self.pwd,self.hostORip,self.database,driveString)) | ||
| self.engine = create_engine( | ||
| f"mssql+pyodbc://{self.usrID}:{self.pwd}@{self.hostORip}/{self.database}?driver={driveString}?" | ||
| ) | ||
|
|
||
| print("Seccessfully connected to SQL Server...") | ||
|
|
||
| if self.save2tableName: | ||
| tableName = self.save2tableName | ||
| elif "/" in self.filePath: | ||
| tableName = self.filePath.split("/")[-1].split(".")[0] | ||
| else: | ||
| if "/" in self.filePath: | ||
| tableName = self.filePath.split("/")[-1].split(".")[0] | ||
| else: | ||
| tableName = self.filePath.split(".")[0] | ||
|
|
||
| tableName = self.filePath.split(".")[0] | ||
|
|
||
| self.file_data.to_sql(tableName,con=self.engine) | ||
| print("Successfully saved %s into SQL Server..."%tableName) | ||
| return None | ||
| print(f"Successfully saved {tableName} into SQL Server...") | ||
| return None | ||
| except: | ||
| self.engine = False | ||
| continue | ||
|
|
||
|
Comment on lines
-49
to
+70
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| raise Exception("Can not save table to sql server, please check your inputs.") | ||
|
|
||
| def readData(self) -> "DataFrame": | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,16 +19,15 @@ | |
| class exceltoDBtable: | ||
| # Available for sql server and mysql now | ||
| def __init__(self,filePath,hostORip=False,usrID =False,pwd=False,database=False,save2tableName=False): | ||
| if not any([hostORip,database,usrID,pwd]): | ||
| if not any([hostORip, database, usrID, pwd]): | ||
| raise Exception("Partially inputs, please check your inputs...") | ||
| else: | ||
| self.filePath = filePath | ||
| self.hostORip = hostORip | ||
| self.database=database | ||
| self.usrID = usrID | ||
| self.pwd = pwd | ||
| self.save2tableName = save2tableName | ||
|
|
||
| self.filePath = filePath | ||
| self.hostORip = hostORip | ||
| self.database=database | ||
| self.usrID = usrID | ||
| self.pwd = pwd | ||
| self.save2tableName = save2tableName | ||
|
|
||
|
Comment on lines
-22
to
+30
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| self.dbType = ["sqlserver"] | ||
| self.readData() | ||
| self.connect2DB() | ||
|
|
@@ -46,27 +45,29 @@ def connect2DB(self) -> "Connect to Database Server": | |
| for i in sqlserverDriver: | ||
| driveString = i.replace(" ","+").replace("{","").replace("}","") | ||
| # print(driveString) | ||
|
|
||
|
|
||
| try: | ||
| self.engine = create_engine("mssql+pyodbc://%s:%s@%s/%s?driver=%s?"%(self.usrID,self.pwd,self.hostORip,self.database,driveString)) | ||
| self.engine = create_engine( | ||
| f"mssql+pyodbc://{self.usrID}:{self.pwd}@{self.hostORip}/{self.database}?driver={driveString}?" | ||
| ) | ||
|
|
||
| print("Seccessfully connected to SQL Server...") | ||
|
|
||
| if self.save2tableName: | ||
| tableName = self.save2tableName | ||
| elif "/" in self.filePath: | ||
| tableName = self.filePath.split("/")[-1].split(".")[0] | ||
| else: | ||
| if "/" in self.filePath: | ||
| tableName = self.filePath.split("/")[-1].split(".")[0] | ||
| else: | ||
| tableName = self.filePath.split(".")[0] | ||
|
|
||
| tableName = self.filePath.split(".")[0] | ||
|
|
||
| self.file_data.to_sql(tableName,con=self.engine) | ||
| print("Successfully saved %s into SQL Server..."%tableName) | ||
| return None | ||
| print(f"Successfully saved {tableName} into SQL Server...") | ||
| return None | ||
| except: | ||
| self.engine = False | ||
| continue | ||
|
|
||
|
Comment on lines
-49
to
+70
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Function
|
||
| raise Exception("Can not save table to sql server, please check your inputs.") | ||
|
|
||
| def readData(self) -> "DataFrame": | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Function
exceltoDBtable.__init__refactored with the following changes:swap-if-else-branches)remove-unnecessary-else)