Created
November 9, 2024 00:19
-
-
Save donald95/dab13b4b498431a411e8612c807b1466 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Connexion to SQL Server 2019 | |
""" | |
import sys | |
import json | |
import pyodbc | |
from rapidfuzz import distance | |
def main(): | |
if len(sys.argv) > 1: | |
_, target_string, registered_by = sys.argv | |
records = get_research(int(registered_by)) | |
print(process_data(target_string, records)) | |
else: | |
print("Script Must Have 2 Args") | |
def get_connection(): | |
SERVER = "127.0.0.1" | |
PORT = "1433" | |
DATABASE = "DATABASE_NAME" | |
USERNAME = "USERNAME" | |
PASSWORD = "PASSWORD" | |
connection_string = f"DRIVER={{ODBC Driver 17 for SQL Server}};SERVER={SERVER};PORT={PORT};DATABASE={DATABASE};UID={USERNAME};PWD={PASSWORD}" | |
return pyodbc.connect(connection_string) | |
def get_research(user_id: int): | |
SQL_QUERY = f"EXEC dbo.usp_research_approximate_matches {user_id}" | |
cursor = get_connection().cursor() | |
cursor.execute(SQL_QUERY) | |
return cursor.fetchall() | |
def process_data(target_string: str, records: list): | |
result_list = [] | |
for record in records: | |
jaro_winkler = distance.JaroWinkler.similarity( | |
target_string.encode("utf-8"), record.title.encode("utf-8") | |
) | |
if jaro_winkler >= 0.8: | |
result_list.append( | |
ResearchMatchingProcess( | |
record.research_id, record.title, round(jaro_winkler, 6) | |
) | |
) | |
return json.dumps([result.__dict__ for result in result_list]) | |
class ResearchMatchingProcess: | |
def __init__(self, research_id, title, result) -> None: | |
self.research_id = research_id | |
self.title = title | |
self.result = result | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment