This repository has been archived on 2025-02-10. You can view files and clone it, but cannot push or open issues or pull requests.
Files
beopen/server/modules/algorithms/univ.py
2025-02-10 12:37:33 +00:00

48 lines
1.3 KiB
Python

# checks a string for illegal characters
# string = string to be checked
# allow_chars = allowed characters should be passed as a string
def char_check(string, allow_chars):
# default allow_chars value
if allow_chars == None:
allow_chars = ascii_letters + digits
#allowed_char = ascii_letters + digits + "_" + "-"
if set(string).difference(allow_chars):
return True
else:
return False
def dict_key_verify(dictionary, keys, mode="and", *args, **kwargs):
# checks if the dictionary exists, if the key exists as a field and if that fields value is not none
# can be used to check if multiple keys exist
if mode != "and" and mode != "or":
mode = "and"
if type(keys) != list:
keys = [keys]
verified = []
if type(keys) != list:
keys = [keys]
for key in keys:
if type(dictionary) != dict or key not in dictionary or not dictionary[key]:
verified.append(False)
else:
verified.append(True)
if mode == "and":
if all(verified) == True:
return True
if mode == "or":
if True in verified:
return True
return False
if __name__ == "__main__":
data = {'name': "joe", 'job': "cuck", 'age': "69"}
answer = dict_key_verify(data, ['job', 'names'], "and")
print(answer)