Utils.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. import json
  2. import random
  3. import string
  4. class Utils:
  5. def __init(self):
  6. pass
  7. def normalize_email(self, email):
  8. replacements = {
  9. "ä": "ae", "ö": "oe", "ü": "ue", "ß": "ss",
  10. "Ä": "Ae", "Ö": "Oe", "Ü": "Ue"
  11. }
  12. for orig, repl in replacements.items():
  13. email = email.replace(orig, repl)
  14. return email
  15. def generate_password(self, length=8):
  16. chars = string.ascii_letters + string.digits
  17. return ''.join(random.choices(chars, k=length))
  18. def pprint(self, data=""):
  19. """
  20. Pretty print a dictionary, list, or text.
  21. If data is a text containing JSON, it will be printed in a formatted way.
  22. """
  23. if isinstance(data, (dict, list)):
  24. print(json.dumps(data, indent=2, ensure_ascii=False))
  25. elif isinstance(data, str):
  26. try:
  27. json_data = json.loads(data)
  28. print(json.dumps(json_data, indent=2, ensure_ascii=False))
  29. except json.JSONDecodeError:
  30. print(data)
  31. else:
  32. print(data)