general.py 1.0 KB

12345678910111213141516171819202122232425262728293031323334
  1. import os
  2. import re
  3. from pathlib import Path
  4. def is_prod() -> bool:
  5. """判断当前环境是否为生产环境"""
  6. return os.getenv('ENV', 'test') == 'prod'
  7. def get_root_dir() -> str:
  8. """获取项目根目录的绝对路径"""
  9. current_path = Path(__file__).resolve()
  10. root_path = current_path.parent if not current_path.is_dir() else current_path
  11. while True:
  12. if 'requirements.txt' in os.listdir(root_path):
  13. return str(root_path.absolute())
  14. root_path = root_path.parent
  15. def get_abs_path(relative_path: str) -> str:
  16. """获取目标文件或目录的相对路径在系统中的绝对路径"""
  17. return os.path.join(get_root_dir(), relative_path)
  18. def pascal_to_snake(pascal_str: str) -> str:
  19. """将Pascal字符串转为蛇形字符串"""
  20. snake_str = re.sub(r'([a-z])([A-Z])', r'\1_\2', pascal_str)
  21. return snake_str.lower()
  22. def snake_to_pascal(snake_str: str) -> str:
  23. """将蛇形字符串转为Pascal字符串"""
  24. return ''.join([item.title() for item in snake_str.split('_')])