common_utils.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. #!/usr/bin/env /usr/bin/python3
  2. # -*- coding:utf-8 -*-
  3. import random
  4. import string
  5. from typing import Dict, List, Set, Union
  6. def contains_all(some_dict: Union[Dict, List, Set], keys: List[str]):
  7. """
  8. 判断`config`中是否存在`keys`中的任意一个
  9. Args:
  10. some_dict:
  11. keys:
  12. Returns:
  13. """
  14. for key in keys:
  15. if not some_dict.__contains__(key):
  16. return False
  17. return True
  18. def exist(some_collection: Union[Dict, List, Set], keys: List[str]):
  19. """
  20. 判断`config`中是否存在`keys`中的任意一个
  21. Args:
  22. some_collection:
  23. keys:
  24. Returns:
  25. """
  26. for key in keys:
  27. if some_collection.__contains__(key):
  28. return True
  29. return False
  30. def generate_random_string(length):
  31. """
  32. 生成给定长度的随机字符串
  33. Args:
  34. length: 指定长度
  35. Returns: 生成的随机字符串
  36. """
  37. letters_and_digits = string.ascii_letters + string.digits
  38. return ''.join(random.choice(letters_and_digits) for i in range(length))