-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
fd50091
commit 41537e2
Showing
4 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import json | ||
|
||
with open('Z://projects//kc_sim_task_docker//output.json', 'r') as f: | ||
file = json.load(f) | ||
# print(file) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import json | ||
import re | ||
import sqlite3 | ||
|
||
import fire | ||
import numpy as np | ||
import pandas as pd | ||
|
||
|
||
def valid_email(email: str) -> bool: | ||
"""Checks if an email address is valid. | ||
Args: | ||
email (str): The email address to validate. | ||
Returns: | ||
bool: True if the email address is valid, False otherwise. | ||
""" | ||
valid_email_regex = r"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$" | ||
return bool(re.fullmatch(valid_email_regex, email)) | ||
|
||
|
||
def filter_db(db_path: str) -> str: | ||
"""Filters a SQLite database and returns the user IDs with invalid email addresses. | ||
Args: | ||
db_path (str): The path to the SQLite database. | ||
Returns: | ||
str: json string with the user IDs with invalid email addresses. | ||
e.g: {"user_ids": ["1", "2", "3", "4", "5", ...]} | ||
""" | ||
conn = sqlite3.connect(db_path) | ||
df = pd.read_sql_query("SELECT * FROM users", conn) | ||
df["email_valid"] = np.vectorize(valid_email)(df["email"]) | ||
invalid_emails = df[~df["email_valid"]]["user_id"] | ||
json_str = json.dumps({"user_ids": invalid_emails.tolist()}) | ||
return json_str | ||
|
||
|
||
if __name__ == "__main__": | ||
fire.Fire(filter_db) |
Binary file not shown.