Merge commit 'eef89c9ebedf030caccba6f7016ecb379cc8f7b0' as 'utils_v2'

This commit is contained in:
2024-11-22 18:48:25 +05:30
87 changed files with 15233 additions and 0 deletions
+135
View File
@@ -0,0 +1,135 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Thursday, 25th Jul., 2024
OBJECTIVE:
To provide a set of data cleaning functions for inputs like phone numbers, emails, etc.
REFERENCES:
N/A
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# My utils:
from utils_v2.string import regex
# For random strings and tokens:
import string
import random
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
def file_name(input_string: str):
"""
Cleans up the string to allow it to safely become a filename.
:param input_string: The string that you want to make safe for using as a filename.
:return: The string that can safely be used as a filename.
"""
return regex.replace(
text = input_string.replace("\n", " "),
pattern = r"[^a-zA-Z0-9 \-_\.]",
substitute_text = ""
)
# ---------------------------------------------------------------------------------------------------------------------
def for_mongo(input_document):
"""
Sanitizes and disarms any JSON-like input that could be used for NoSQL-injection attacks.
:param input_document: The list or dict to be sanitized.
:return: The sanitized list or dict.
"""
# A special function that disarms any input string by dealing with special characters
# that Mongo may consider to be instructions:
def disarm(input_string):
input_string = regex.replace(
text = input_string,
pattern = r"[^a-zA-Z0-9,_\-\.\\\/:;'\(\) ]",
substitute_text = ""
)
return input_string
# Initially we assign the value of the input to the output:
sanitized_document = input_document
# Handle the case where the input is an array:
if isinstance(input_document, list):
sanitized_document = [for_mongo(document) for document in input_document]
# Handle the case when the input is a document:
elif isinstance(input_document, dict):
sanitized_document = {}
for k, v in input_document.items():
sanitized_document[disarm(k)] = v if type(v) not in [list, dict] else for_mongo(v)
# Done here:
return sanitized_document
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
pass