Created
February 13, 2019 06:44
-
-
Save noize-e/01192cad4d7c0a45e9af775061721c6b to your computer and use it in GitHub Desktop.
Normalize a given string to fit the data it contains, like simple test, or email to a password
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
def normalize(obj, dict_key=None, regx="text"): | |
""" This function cleans and normalize a given string. It performs three | |
main operations: Sets to lower case, strips any trailing whitespace and | |
finally substracts any character that isn't allowed in the string type | |
regex, the character is replaced with an empty string. | |
:param obj: this object can be 'str' or 'dict' instance type. | |
:type obj:str It performs a directly normalization | |
:type obj:dict It expects a key ( from 'dict_key' arg ) string | |
to extract the string to be normalize, otherwise returns None. | |
:type dict_key:str | |
:param dict_key: The dictionary key to extract a string. | |
:type regx:str | |
:param regx: it defines the type of substraction to be performed | |
in the string. By default it is set the 'text' type. | |
... string types ( Regex ) | |
- text: alphanumerical ( ascii ) & non-ascii ( á é í ó ú ñ ) | |
- usrname: alphanumerical ( ascii ) & special ( _ ) | |
- email: alphanumerical ( ascii ) & special ( @ - . _ ) | |
- pwd: alphanumerical ( ascii ) & special ( - . _ $ & # ) | |
- num: only numerical ( 0 - 9 ) | |
:return the normalized string, otherwise returns None | |
""" | |
regxs = { | |
"text": ("[^A-Za-z0-9áéíóúñ]+", " "), | |
"usrname": ("[^A-Za-z0-9_]+", ""), | |
"email": ("[^A-Za-z0-9@\-_.]+", ""), | |
"pwd": ("[^A-Za-z0-9@$&#\-_.]+", ""), | |
"num": ("[^0-9]+", "") | |
} | |
try: | |
rkey = regxs[regx][0] | |
rsub = regxs[regx][1] | |
if isdictionary(value): | |
obj = obj.get(dict_key) | |
obj = text_type(obj).lower().strip() | |
obj = re.sub(re.compile(rkey), rsub, obj) | |
except: | |
obj = None | |
return obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
it is buggy!